Purescript library to handle the formatting of dates and times.
at main 239 lines 7.2 kB view raw
1// SPDX-License-Identifier: MIT 2// Copyright (C) 2022 Roland Csaszar 3// 4// Project: purescript-datetimeformat 5// File: DateTimeFormat.js 6// Date: 12.Feb.2022 7// 8// ============================================================================== 9/* eslint-disable max-params */ 10/* eslint-disable no-undef */ 11/* eslint-disable no-undefined */ 12/* eslint-disable max-lines-per-function */ 13/* eslint-disable max-statements */ 14/* eslint-disable complexity */ 15 16// eslint-disable-next-line strict 17"use strict" 18 19exports.defaultDateTimeFormat = defaultDateTimeFormat 20exports.isoDateTimeNow = isoDateTimeNow 21exports.isoDateTimeJS = isoDateTimeJS 22exports.formatDateTimeNow = formatDateTimeNow 23exports.formatDateJS = formatDateJS 24exports.formatDateTimeJS = formatDateTimeJS 25exports.getDateTimeFormatJS = getDateTimeFormatJS 26 27/** 28 * Return the default `DateTimeFormat` object of the current locale. 29 * 30 * @returns The default `DateTimeFormat` object of the current locale. 31 */ 32function defaultDateTimeFormat() { 33 return function () { 34 return new Intl.DateTimeFormat() 35 } 36} 37 38/** 39 * Return the formatter with the given locale and options. 40 * 41 * @param {string[]} locales - The array of locales. 42 * @param {DateTimeFormatOptionsJS} options - The options of 43 * `Intl.DateTimeFormat` 44 * @returns The formatter with the given options and locale. 45 */ 46 47function getDateTimeFormatJS( 48 locales, 49 { 50 dateStyle, 51 timeStyle, 52 calendar, 53 dayPeriod, 54 numberingSystem, 55 localeMatcher, 56 timeZone, 57 hour12, 58 hourCycle, 59 formatMatcher, 60 weekDay, 61 era, 62 year, 63 month, 64 day, 65 hour, 66 minute, 67 second, 68 fractionalSecondDigits, 69 timeZoneNameStyle, 70 } 71) { 72 let options = {} 73 if (dateStyle !== undefined) { 74 const optionsAdd = { dateStyle } 75 options = Object.assign(options, optionsAdd) 76 } 77 if (timeStyle !== undefined) { 78 const optionsAdd = { timeStyle } 79 options = Object.assign(options, optionsAdd) 80 } 81 if (calendar !== undefined) { 82 const optionsAdd = { calendar } 83 options = Object.assign(options, optionsAdd) 84 } 85 if (dayPeriod !== undefined) { 86 const optionsAdd = { dayPeriod } 87 options = Object.assign(options, optionsAdd) 88 } 89 if (numberingSystem !== undefined) { 90 const optionsAdd = { numberingSystem } 91 options = Object.assign(options, optionsAdd) 92 } 93 if (localeMatcher !== undefined) { 94 const optionsAdd = { localeMatcher } 95 options = Object.assign(options, optionsAdd) 96 } 97 if (timeZone !== undefined) { 98 const optionsAdd = { timeZone } 99 options = Object.assign(options, optionsAdd) 100 } 101 if (hour12 !== undefined) { 102 const optionsAdd = { hour12 } 103 options = Object.assign(options, optionsAdd) 104 } 105 if (hourCycle !== undefined) { 106 const optionsAdd = { hourCycle } 107 options = Object.assign(options, optionsAdd) 108 } 109 if (formatMatcher !== undefined) { 110 const optionsAdd = { formatMatcher } 111 options = Object.assign(options, optionsAdd) 112 } 113 if (weekDay !== undefined) { 114 const optionsAdd = { weekDay } 115 options = Object.assign(options, optionsAdd) 116 } 117 if (era !== undefined) { 118 const optionsAdd = { era } 119 options = Object.assign(options, optionsAdd) 120 } 121 if (year !== undefined) { 122 const optionsAdd = { year } 123 options = Object.assign(options, optionsAdd) 124 } 125 if (month !== undefined) { 126 const optionsAdd = { month } 127 options = Object.assign(options, optionsAdd) 128 } 129 if (day !== undefined) { 130 const optionsAdd = { day } 131 options = Object.assign(options, optionsAdd) 132 } 133 if (hour !== undefined) { 134 const optionsAdd = { hour } 135 options = Object.assign(options, optionsAdd) 136 } 137 if (minute !== undefined) { 138 const optionsAdd = { minute } 139 options = Object.assign(options, optionsAdd) 140 } 141 if (second !== undefined) { 142 const optionsAdd = { second } 143 options = Object.assign(options, optionsAdd) 144 } 145 if (fractionalSecondDigits !== undefined) { 146 const optionsAdd = { fractionalSecondDigits } 147 options = Object.assign(options, optionsAdd) 148 } 149 if (timeZoneNameStyle !== undefined) { 150 const optionsAdd = { timeZoneNameStyle } 151 options = Object.assign(options, optionsAdd) 152 } 153 154 return new Intl.DateTimeFormat(locales, options) 155} 156 157/** 158 * Return the current date and time as ISO ISO 8601 string as UTC. 159 * 160 * @returns The current date and time as ISO ISO 8601 string as UTC. 161 */ 162function isoDateTimeNow() { 163 return function () { 164 const today = new Date() 165 166 return today.toISOString() 167 } 168} 169 170/** 171 * Return the given date and time as ISO ISO 8601 string as UTC. 172 * @param {number} year - The Year 173 * @param {number} monthIdx - The index of the month, starting at 0 (January) 174 * @param {number} day - The day of the month (starting at 1) 175 * @param {number} hour - The hour 176 * @param {number} minutes - The minutes 177 * @param {number} seconds - The seconds 178 * @returns The given date and time as ISO ISO 8601 string as UTC. 179 */ 180function isoDateTimeJS(year, monthIdx, day, hour, minutes, seconds) { 181 const date = new Date(year, monthIdx, day, hour, minutes, seconds) 182 return date.toISOString() 183} 184 185/** 186 * Return a string of the current date and time formatted using the given 187 * formatter object. 188 * 189 * @param {Intl.DateTimeFormat} formatter - The format to use to format the 190 * output. 191 * @returns A string of the current date and time formatted using the given 192 * formatter object. 193 */ 194function formatDateTimeNow(formatter) { 195 return function () { 196 const today = new Date() 197 198 return formatter.format(today) 199 } 200} 201 202/** 203 * Return the given local date formatted by the given formatter. 204 * 205 * @param {Intl.DateTimeFormat} formatter - The formatter to use 206 * @param {number} year - The Year 207 * @param {number} monthIdx - The index of the month, starting at 0 (January) 208 * @param {number} day - The day of the month (starting at 1) 209 * @returns The given date formatted by the given formatter. 210 */ 211function formatDateJS(formatter, year, monthIdx, day) { 212 const date = new Date(year, monthIdx, day) 213 return formatter.format(date) 214} 215 216/** 217 * Return the given local date and time formatted by the given formatter. 218 * 219 * @param {Intl.DateTimeFormat} formatter - The formatter to use 220 * @param {number} year - The Year 221 * @param {number} monthIdx - The index of the month, starting at 0 (January) 222 * @param {number} day - The day of the month (starting at 1) 223 * @param {number} hour - The hour 224 * @param {number} minutes - The minutes 225 * @param {number} seconds - The seconds 226 * @returns The given date and time formatted by the given formatter. 227 */ 228function formatDateTimeJS( 229 formatter, 230 year, 231 monthIdx, 232 day, 233 hour, 234 minutes, 235 seconds 236) { 237 const date = new Date(year, monthIdx, day, hour, minutes, seconds) 238 return formatter.format(date) 239}