|
34 | 34 |
|
35 | 35 | /**
|
36 | 36 | * @typedef {Object} TimePoint
|
37 |
| - * @property {Number} year - 1970-- |
38 |
| - * @property {Number} month - 1-12 |
39 |
| - * @property {Number} day - 1-31 |
40 |
| - * @property {Number} hour - 0-24 |
41 |
| - * @property {Number} minute - 0-60 |
42 |
| - * @property {Number} second - 0-60 |
43 |
| - * @property {string} timezone - Time zone in IANA database format 'Europe/Stockholm' |
| 37 | + * @property {Number} y - 1970-- |
| 38 | + * @property {Number} m - 1-12 |
| 39 | + * @property {Number} d - 1-31 |
| 40 | + * @property {Number} h - 0-24 |
| 41 | + * @property {Number} i - 0-60 Minute |
| 42 | + * @property {Number} s - 0-60 |
| 43 | + * @property {string} tz - Time zone in IANA database format 'Europe/Stockholm' |
44 | 44 | */
|
45 | 45 |
|
46 | 46 | /**
|
|
50 | 50 | *
|
51 | 51 | * @constructor
|
52 | 52 | *
|
53 |
| - * @param {Number} year - 1970-- |
54 |
| - * @param {Number} month - 1-12 |
55 |
| - * @param {Number} day - 1-31 |
56 |
| - * @param {Number} hour - 0-24 |
57 |
| - * @param {Number} minute - 0-60 |
58 |
| - * @param {Number} second - 0-60 |
59 |
| - * @param {string} timezone - Time zone in IANA database format 'Europe/Stockholm' |
60 |
| - * @param {boolean} [throwOnInvalidTime] - Default is to return the adjusted time if the call happens during a Daylight-Saving-Time switch. |
| 53 | + * @param {Number} y - 1970-- |
| 54 | + * @param {Number} m - 1-12 |
| 55 | + * @param {Number} d - 1-31 |
| 56 | + * @param {Number} h - 0-24 |
| 57 | + * @param {Number} i - 0-60 Minute |
| 58 | + * @param {Number} s - 0-60 |
| 59 | + * @param {string} tz - Time zone in IANA database format 'Europe/Stockholm' |
| 60 | + * @param {boolean} [throwOnInvalid] - Default is to return the adjusted time if the call happens during a Daylight-Saving-Time switch. |
61 | 61 | * E.g. Value "01:01:01" is returned if input time is 00:01:01 while one hour got actually
|
62 | 62 | * skipped, going from 23:59:59 to 01:00:00. Setting this flag makes the library throw an exception instead.
|
63 | 63 | * @returns {date} - Normal date object with correct UTC and system local time
|
64 | 64 | *
|
65 | 65 | */
|
66 |
| - function minitz(year, month, day, hour, minute, second, timezone, throwOnInvalidTime) { |
67 |
| - return minitz.fromTZ(minitz.tp(year, month, day, hour, minute, second, timezone), throwOnInvalidTime); |
| 66 | + function minitz(y, m, d, h, i, s, tz, throwOnInvalid) { |
| 67 | + return minitz.fromTZ(minitz.tp(y, m, d, h, i, s, tz), throwOnInvalid); |
68 | 68 | }
|
69 | 69 |
|
70 | 70 | /**
|
|
73 | 73 | * @public
|
74 | 74 | * @static
|
75 | 75 | *
|
76 |
| - * @param {string} localTimeString - ISO8601 formatted local time string, non UTC |
77 |
| - * @param {string} timezone - Time zone in IANA database format 'Europe/Stockholm' |
78 |
| - * @param {boolean} [throwOnInvalidTime] - Default is to return the adjusted time if the call happens during a Daylight-Saving-Time switch. |
| 76 | + * @param {string} localTimeStr - ISO8601 formatted local time string, non UTC |
| 77 | + * @param {string} tz - Time zone in IANA database format 'Europe/Stockholm' |
| 78 | + * @param {boolean} [throwOnInvalid] - Default is to return the adjusted time if the call happens during a Daylight-Saving-Time switch. |
79 | 79 | * E.g. Value "01:01:01" is returned if input time is 00:01:01 while one hour got actually
|
80 | 80 | * skipped, going from 23:59:59 to 01:00:00. Setting this flag makes the library throw an exception instead.
|
81 | 81 | * @return {date} - Normal date object
|
82 | 82 | *
|
83 | 83 | */
|
84 |
| - minitz.fromTZISO = (localTimeString, timezone, throwOnInvalidTime) => { |
85 |
| - return minitz.fromTZ(parseISOLocal(localTimeString, timezone), throwOnInvalidTime); |
| 84 | + minitz.fromTZISO = (localTimeStr, tz, throwOnInvalid) => { |
| 85 | + return minitz.fromTZ(parseISOLocal(localTimeStr, tz), throwOnInvalid); |
86 | 86 | };
|
87 | 87 |
|
88 | 88 | /**
|
|
91 | 91 | * @public
|
92 | 92 | * @static
|
93 | 93 | *
|
94 |
| - * @param {TimePoint} date - Object with specified timezone |
95 |
| - * @param {boolean} [throwOnInvalidTime] - Default is to return the adjusted time if the call happens during a Daylight-Saving-Time switch. |
| 94 | + * @param {TimePoint} tp - Object with specified timezone |
| 95 | + * @param {boolean} [throwOnInvalid] - Default is to return the adjusted time if the call happens during a Daylight-Saving-Time switch. |
96 | 96 | * E.g. Value "01:01:01" is returned if input time is 00:01:01 while one hour got actually
|
97 | 97 | * skipped, going from 23:59:59 to 01:00:00. Setting this flag makes the library throw an exception instead.
|
98 | 98 | * @returns {date} - Normal date object
|
99 | 99 | */
|
100 |
| - minitz.fromTZ = function(timePoint, throwOnInvalidTime) { |
| 100 | + minitz.fromTZ = function(tp, throwOnInvalid) { |
101 | 101 |
|
102 | 102 | const
|
103 | 103 |
|
104 | 104 | // Construct a fake Date object with UTC date/time set to local date/time in source timezone
|
105 |
| - inputDate = new Date(Date.UTC( |
106 |
| - timePoint.year, |
107 |
| - timePoint.month - 1, |
108 |
| - timePoint.day, |
109 |
| - timePoint.hour, |
110 |
| - timePoint.minute, |
111 |
| - timePoint.second |
| 105 | + inDate = new Date(Date.UTC( |
| 106 | + tp.y, |
| 107 | + tp.m - 1, |
| 108 | + tp.d, |
| 109 | + tp.h, |
| 110 | + tp.i, |
| 111 | + tp.s |
112 | 112 | )),
|
113 | 113 |
|
114 | 114 | // Get offset between UTC and source timezone
|
115 |
| - offset = getTimezoneOffset(timePoint.timezone, inputDate), |
| 115 | + offset = getTimezoneOffset(tp.tz, inDate), |
116 | 116 |
|
117 |
| - // Remove offset from inputDate to hopefully get a true date object |
118 |
| - guessedLocalDate = new Date(inputDate.getTime() - offset), |
| 117 | + // Remove offset from inDate to hopefully get a true date object |
| 118 | + dateGuess = new Date(inDate.getTime() - offset), |
119 | 119 |
|
120 | 120 | // Get offset between UTC and guessed time in target timezone
|
121 |
| - guessedInputDateOffset = getTimezoneOffset(timePoint.timezone, guessedLocalDate); |
| 121 | + dateOffsGuess = getTimezoneOffset(tp.tz, dateGuess); |
122 | 122 |
|
123 | 123 | // If offset between guessed true date object and UTC matches initial calculation, the guess
|
124 | 124 | // was spot on
|
125 |
| - if ((guessedInputDateOffset - offset) === 0) { |
126 |
| - return guessedLocalDate; |
| 125 | + if ((dateOffsGuess - offset) === 0) { |
| 126 | + return dateGuess; |
127 | 127 | } else {
|
128 | 128 | // Not quite there yet, make a second try on guessing the local time, adjust by the offset indicated by the previous guess
|
129 | 129 | // Try recreating input time again
|
130 | 130 | // Then calculate and check the offset again
|
131 | 131 | const
|
132 |
| - guessedLocalDate2 = new Date(inputDate.getTime() - guessedInputDateOffset), |
133 |
| - guessedInputDateOffset2 = getTimezoneOffset(timePoint.timezone, guessedLocalDate2); |
134 |
| - if ((guessedInputDateOffset2 - guessedInputDateOffset) === 0) { |
| 132 | + dateGuess2 = new Date(inDate.getTime() - dateOffsGuess), |
| 133 | + dateOffsGuess2 = getTimezoneOffset(tp.tz, dateGuess2); |
| 134 | + if ((dateOffsGuess2 - dateOffsGuess) === 0) { |
135 | 135 | // All good, return local time
|
136 |
| - return guessedLocalDate2; |
137 |
| - } else if (!throwOnInvalidTime) { |
| 136 | + return dateGuess2; |
| 137 | + } else if (!throwOnInvalid) { |
138 | 138 | // This guess wasn't spot on either, we're most probably dealing with a DST transition
|
139 | 139 | // - return the local time adjusted by _initial_ offset
|
140 |
| - return guessedLocalDate; |
| 140 | + return dateGuess; |
141 | 141 | } else {
|
142 | 142 | // Input time is invalid, and the library is instructed to throw, so let's do it
|
143 | 143 | throw new Error("Invalid date passed to fromTZ()");
|
|
155 | 155 | * @public
|
156 | 156 | * @static
|
157 | 157 | *
|
158 |
| - * @param {date} date - Input date |
159 |
| - * @param {string} [tzString] - Timezone string in Europe/Stockholm format |
| 158 | + * @param {d} date - Input date |
| 159 | + * @param {string} [tzStr] - Timezone string in Europe/Stockholm format |
160 | 160 | *
|
161 | 161 | * @returns {TimePoint}
|
162 | 162 | *
|
|
167 | 167 | *
|
168 | 168 | * // Will result in the following object:
|
169 | 169 | * // {
|
170 |
| - * // year: 2022, |
171 |
| - * // month: 9, |
172 |
| - * // day: 28, |
173 |
| - * // hour: 13, |
174 |
| - * // minute: 28, |
175 |
| - * // second: 28, |
176 |
| - * // timezone: "America/New_York" |
| 170 | + * // y: 2022, |
| 171 | + * // m: 9, |
| 172 | + * // d: 28, |
| 173 | + * // h: 13, |
| 174 | + * // i: 28, |
| 175 | + * // s: 28, |
| 176 | + * // tz: "America/New_York" |
177 | 177 | * // }
|
178 | 178 | *
|
179 | 179 | * @example <caption>Example using vanilla js:</caption>
|
|
183 | 183 | * );
|
184 | 184 | *
|
185 | 185 | */
|
186 |
| - minitz.toTZ = function (date, tzString) { |
187 |
| - const target = new Date(date.toLocaleString("sv-SE", {timeZone: tzString})); |
| 186 | + minitz.toTZ = function (d, tzStr) { |
| 187 | + const td = new Date(d.toLocaleString("sv-SE", {timeZone: tzStr})); |
188 | 188 | return {
|
189 |
| - year: target.getFullYear(), |
190 |
| - month: target.getMonth() + 1, |
191 |
| - day: target.getDate(), |
192 |
| - hour: target.getHours(), |
193 |
| - minute: target.getMinutes(), |
194 |
| - second: target.getSeconds(), |
195 |
| - timezone: tzString |
| 189 | + y: td.getFullYear(), |
| 190 | + m: td.getMonth() + 1, |
| 191 | + d: td.getDate(), |
| 192 | + h: td.getHours(), |
| 193 | + i: td.getMinutes(), |
| 194 | + s: td.getSeconds(), |
| 195 | + tz: tzStr |
196 | 196 | };
|
197 | 197 | };
|
198 | 198 |
|
|
202 | 202 | * @public
|
203 | 203 | * @static
|
204 | 204 | *
|
205 |
| - * @param {Number} year - 1970-- |
206 |
| - * @param {Number} month - 1-12 |
207 |
| - * @param {Number} day - 1-31 |
208 |
| - * @param {Number} hour - 0-24 |
209 |
| - * @param {Number} minute - 0-60 |
210 |
| - * @param {Number} second - 0-60 |
211 |
| - * @param {string} timezone - Time zone in format 'Europe/Stockholm' |
| 205 | + * @param {Number} y - 1970-- |
| 206 | + * @param {Number} m - 1-12 |
| 207 | + * @param {Number} d - 1-31 |
| 208 | + * @param {Number} h - 0-24 |
| 209 | + * @param {Number} i - 0-60 Minute |
| 210 | + * @param {Number} s - 0-60 |
| 211 | + * @param {string} tz - Time zone in format 'Europe/Stockholm' |
212 | 212 | *
|
213 | 213 | * @returns {TimePoint}
|
214 | 214 | *
|
215 | 215 | */
|
216 |
| - minitz.tp = (y,m,d,h,i,s,t) => { return { year: y, month: m, day: d, hour: h, minute: i, second: s, timezone: t }; }; |
| 216 | + minitz.tp = (y,m,d,h,i,s,tz) => { return { y, m, d, h, i, s, tz: tz }; }; |
217 | 217 |
|
218 | 218 | /**
|
219 | 219 | * Helper function that returns the current UTC offset (in ms) for a specific timezone at a specific point in time
|
|
238 | 238 | *
|
239 | 239 | * @private
|
240 | 240 | *
|
241 |
| - * @param {string} dateTimeString - an ISO 8601 format date and time string |
| 241 | + * @param {string} dtStr - an ISO 8601 format date and time string |
242 | 242 | * with all components, e.g. 2015-11-24T19:40:00
|
243 | 243 | * @returns {TimePoint} - TimePoint instance from parsing the string
|
244 | 244 | */
|
245 |
| - function parseISOLocal(dateTimeString, timezone) { |
| 245 | + function parseISOLocal(dtStr, tz) { |
246 | 246 |
|
247 | 247 | // Parse date using built in Date.parse
|
248 |
| - const parsed = new Date(Date.parse(dateTimeString)); |
| 248 | + const pd = new Date(Date.parse(dtStr)); |
249 | 249 |
|
250 | 250 | // Check for completeness
|
251 |
| - if (isNaN(parsed)) { |
| 251 | + if (isNaN(pd)) { |
252 | 252 | throw new Error("minitz: Invalid ISO8601 passed to parser.");
|
253 | 253 | }
|
254 | 254 |
|
255 | 255 | // If
|
256 | 256 | // * date/time is specified in UTC (Z-flag included)
|
257 | 257 | // * or UTC offset is specified (+ or - included after character 9 (20200101 or 2020-01-0))
|
258 | 258 | // Return time in utc, else return local time and include timezone identifier
|
259 |
| - const stringEnd = dateTimeString.substring(9); |
260 |
| - if (dateTimeString.includes("Z") || stringEnd.includes("-") || stringEnd.includes("+")) { |
261 |
| - return minitz.tp(parsed.getUTCFullYear(), parsed.getUTCMonth()+1, parsed.getUTCDate(),parsed.getUTCHours(), parsed.getUTCMinutes(),parsed.getUTCSeconds(), "Etc/UTC"); |
| 259 | + const stringEnd = dtStr.substring(9); |
| 260 | + if (dtStr.includes("Z") || stringEnd.includes("-") || stringEnd.includes("+")) { |
| 261 | + return minitz.tp(pd.getUTCFullYear(), pd.getUTCMonth()+1, pd.getUTCDate(),pd.getUTCHours(), pd.getUTCMinutes(),pd.getUTCSeconds(), "Etc/UTC"); |
262 | 262 | } else {
|
263 |
| - return minitz.tp(parsed.getFullYear(), parsed.getMonth()+1, parsed.getDate(),parsed.getHours(), parsed.getMinutes(),parsed.getSeconds(), timezone); |
| 263 | + return minitz.tp(pd.getFullYear(), pd.getMonth()+1, pd.getDate(),pd.getHours(), pd.getMinutes(),pd.getSeconds(), tz); |
264 | 264 | }
|
265 | 265 | // Treat date as local time, in target timezone
|
266 | 266 |
|
|
0 commit comments