Skip to content

Commit e281eb0

Browse files
committed
Prepare 4.0.0
1 parent 3d1b9bb commit e281eb0

13 files changed

+405
-405
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Convert dates between any timezone supported by the system.
1010
* Parses ISO8601 time strings.
1111
* MIT licensed, use the library any way you want. For real.
12-
* Minimal, no dependencies. Relies on JavaScript Intl and current best practices.
12+
* Minimal (less than 2 KB minified), no dependencies. Relies on JavaScript Intl and current best practices.
1313
* Works in Node.js >=14.0 (both require and import).
1414
* Works in Deno >=1.8.
1515
* Works in browsers as standalone, UMD or ES-module.
@@ -81,15 +81,15 @@ import minitz from "minitz";
8181
JavaScript
8282

8383
```javascript
84-
import minitz from "https://deno.land/x/minitz@3.0.1/src/minitz.js";
84+
import minitz from "https://deno.land/x/minitz@4.0.0/src/minitz.js";
8585

8686
// ...
8787
```
8888

8989
TypeScript
9090

9191
```typescript
92-
import { minitz } from "https://deno.land/x/minitz@3.0.1/src/minitz.js";
92+
import { minitz } from "https://deno.land/x/minitz@4.0.0/src/minitz.js";
9393

9494
// ...
9595
```
@@ -162,13 +162,13 @@ If you need to use the result in any other way, it's better to use minitz to con
162162
console.log("Time in New York: ", minitz.toTZ(new Date(), "America/New_York"));
163163
// -> Time in New York:
164164
// {
165-
// year: 2022,
166-
// month: 9,
167-
// day: 14,
168-
// hour: 17,
169-
// minute: 29,
170-
// second: 42,
171-
// timezone: 'America/New_York'
165+
// y: 2022,
166+
// m: 9,
167+
// d: 14,
168+
// h: 17,
169+
// i: 29,
170+
// s: 42,
171+
// tz: 'America/New_York'
172172
// }
173173
```
174174

dist/minitz.cjs

+78-78
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434

3535
/**
3636
* @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'
4444
*/
4545

4646
/**
@@ -50,21 +50,21 @@
5050
*
5151
* @constructor
5252
*
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.
6161
* E.g. Value "01:01:01" is returned if input time is 00:01:01 while one hour got actually
6262
* skipped, going from 23:59:59 to 01:00:00. Setting this flag makes the library throw an exception instead.
6363
* @returns {date} - Normal date object with correct UTC and system local time
6464
*
6565
*/
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);
6868
}
6969

7070
/**
@@ -73,16 +73,16 @@
7373
* @public
7474
* @static
7575
*
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.
7979
* E.g. Value "01:01:01" is returned if input time is 00:01:01 while one hour got actually
8080
* skipped, going from 23:59:59 to 01:00:00. Setting this flag makes the library throw an exception instead.
8181
* @return {date} - Normal date object
8282
*
8383
*/
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);
8686
};
8787

8888
/**
@@ -91,53 +91,53 @@
9191
* @public
9292
* @static
9393
*
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.
9696
* E.g. Value "01:01:01" is returned if input time is 00:01:01 while one hour got actually
9797
* skipped, going from 23:59:59 to 01:00:00. Setting this flag makes the library throw an exception instead.
9898
* @returns {date} - Normal date object
9999
*/
100-
minitz.fromTZ = function(timePoint, throwOnInvalidTime) {
100+
minitz.fromTZ = function(tp, throwOnInvalid) {
101101

102102
const
103103

104104
// 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
112112
)),
113113

114114
// Get offset between UTC and source timezone
115-
offset = getTimezoneOffset(timePoint.timezone, inputDate),
115+
offset = getTimezoneOffset(tp.tz, inDate),
116116

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),
119119

120120
// Get offset between UTC and guessed time in target timezone
121-
guessedInputDateOffset = getTimezoneOffset(timePoint.timezone, guessedLocalDate);
121+
dateOffsGuess = getTimezoneOffset(tp.tz, dateGuess);
122122

123123
// If offset between guessed true date object and UTC matches initial calculation, the guess
124124
// was spot on
125-
if ((guessedInputDateOffset - offset) === 0) {
126-
return guessedLocalDate;
125+
if ((dateOffsGuess - offset) === 0) {
126+
return dateGuess;
127127
} else {
128128
// Not quite there yet, make a second try on guessing the local time, adjust by the offset indicated by the previous guess
129129
// Try recreating input time again
130130
// Then calculate and check the offset again
131131
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) {
135135
// All good, return local time
136-
return guessedLocalDate2;
137-
} else if (!throwOnInvalidTime) {
136+
return dateGuess2;
137+
} else if (!throwOnInvalid) {
138138
// This guess wasn't spot on either, we're most probably dealing with a DST transition
139139
// - return the local time adjusted by _initial_ offset
140-
return guessedLocalDate;
140+
return dateGuess;
141141
} else {
142142
// Input time is invalid, and the library is instructed to throw, so let's do it
143143
throw new Error("Invalid date passed to fromTZ()");
@@ -155,8 +155,8 @@
155155
* @public
156156
* @static
157157
*
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
160160
*
161161
* @returns {TimePoint}
162162
*
@@ -167,13 +167,13 @@
167167
*
168168
* // Will result in the following object:
169169
* // {
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"
177177
* // }
178178
*
179179
* @example <caption>Example using vanilla js:</caption>
@@ -183,16 +183,16 @@
183183
* );
184184
*
185185
*/
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}));
188188
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
196196
};
197197
};
198198

@@ -202,18 +202,18 @@
202202
* @public
203203
* @static
204204
*
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'
212212
*
213213
* @returns {TimePoint}
214214
*
215215
*/
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 }; };
217217

218218
/**
219219
* Helper function that returns the current UTC offset (in ms) for a specific timezone at a specific point in time
@@ -238,29 +238,29 @@
238238
*
239239
* @private
240240
*
241-
* @param {string} dateTimeString - an ISO 8601 format date and time string
241+
* @param {string} dtStr - an ISO 8601 format date and time string
242242
* with all components, e.g. 2015-11-24T19:40:00
243243
* @returns {TimePoint} - TimePoint instance from parsing the string
244244
*/
245-
function parseISOLocal(dateTimeString, timezone) {
245+
function parseISOLocal(dtStr, tz) {
246246

247247
// Parse date using built in Date.parse
248-
const parsed = new Date(Date.parse(dateTimeString));
248+
const pd = new Date(Date.parse(dtStr));
249249

250250
// Check for completeness
251-
if (isNaN(parsed)) {
251+
if (isNaN(pd)) {
252252
throw new Error("minitz: Invalid ISO8601 passed to parser.");
253253
}
254254

255255
// If
256256
// * date/time is specified in UTC (Z-flag included)
257257
// * or UTC offset is specified (+ or - included after character 9 (20200101 or 2020-01-0))
258258
// 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");
262262
} 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);
264264
}
265265
// Treat date as local time, in target timezone
266266

dist/minitz.min.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.minitz=factory())})(this,function(){"use strict";function minitz(year,month,day,hour,minute,second,timezone,throwOnInvalidTime){return minitz.fromTZ(minitz.tp(year,month,day,hour,minute,second,timezone),throwOnInvalidTime)}minitz.fromTZISO=(localTimeString,timezone,throwOnInvalidTime)=>{return minitz.fromTZ(parseISOLocal(localTimeString,timezone),throwOnInvalidTime)};minitz.fromTZ=function(timePoint,throwOnInvalidTime){const inputDate=new Date(Date.UTC(timePoint.year,timePoint.month-1,timePoint.day,timePoint.hour,timePoint.minute,timePoint.second)),offset=getTimezoneOffset(timePoint.timezone,inputDate),guessedLocalDate=new Date(inputDate.getTime()-offset),guessedInputDateOffset=getTimezoneOffset(timePoint.timezone,guessedLocalDate);if(guessedInputDateOffset-offset===0){return guessedLocalDate}else{const guessedLocalDate2=new Date(inputDate.getTime()-guessedInputDateOffset),guessedInputDateOffset2=getTimezoneOffset(timePoint.timezone,guessedLocalDate2);if(guessedInputDateOffset2-guessedInputDateOffset===0){return guessedLocalDate2}else if(!throwOnInvalidTime){return guessedLocalDate}else{throw new Error("Invalid date passed to fromTZ()")}}};minitz.toTZ=function(date,tzString){const target=new Date(date.toLocaleString("sv-SE",{timeZone:tzString}));return{year:target.getFullYear(),month:target.getMonth()+1,day:target.getDate(),hour:target.getHours(),minute:target.getMinutes(),second:target.getSeconds(),timezone:tzString}};minitz.tp=(y,m,d,h,i,s,t)=>{return{year:y,month:m,day:d,hour:h,minute:i,second:s,timezone:t}};function getTimezoneOffset(timeZone,date=new Date){const tz=date.toLocaleString("en",{timeZone:timeZone,timeStyle:"long"}).split(" ").slice(-1)[0];const dateString=date.toString();return Date.parse(`${dateString} UTC`)-Date.parse(`${dateString} ${tz}`)}function parseISOLocal(dateTimeString,timezone){const parsed=new Date(Date.parse(dateTimeString));if(isNaN(parsed)){throw new Error("minitz: Invalid ISO8601 passed to parser.")}const stringEnd=dateTimeString.substring(9);if(dateTimeString.includes("Z")||stringEnd.includes("-")||stringEnd.includes("+")){return minitz.tp(parsed.getUTCFullYear(),parsed.getUTCMonth()+1,parsed.getUTCDate(),parsed.getUTCHours(),parsed.getUTCMinutes(),parsed.getUTCSeconds(),"Etc/UTC")}else{return minitz.tp(parsed.getFullYear(),parsed.getMonth()+1,parsed.getDate(),parsed.getHours(),parsed.getMinutes(),parsed.getSeconds(),timezone)}}minitz.minitz=minitz;return minitz});
1+
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.minitz=factory())})(this,function(){"use strict";function minitz(y,m,d,h,i,s,tz,throwOnInvalid){return minitz.fromTZ(minitz.tp(y,m,d,h,i,s,tz),throwOnInvalid)}minitz.fromTZISO=(localTimeStr,tz,throwOnInvalid)=>{return minitz.fromTZ(parseISOLocal(localTimeStr,tz),throwOnInvalid)};minitz.fromTZ=function(tp,throwOnInvalid){const inDate=new Date(Date.UTC(tp.y,tp.m-1,tp.d,tp.h,tp.i,tp.s)),offset=getTimezoneOffset(tp.tz,inDate),dateGuess=new Date(inDate.getTime()-offset),dateOffsGuess=getTimezoneOffset(tp.tz,dateGuess);if(dateOffsGuess-offset===0){return dateGuess}else{const dateGuess2=new Date(inDate.getTime()-dateOffsGuess),dateOffsGuess2=getTimezoneOffset(tp.tz,dateGuess2);if(dateOffsGuess2-dateOffsGuess===0){return dateGuess2}else if(!throwOnInvalid){return dateGuess}else{throw new Error("Invalid date passed to fromTZ()")}}};minitz.toTZ=function(d,tzStr){const td=new Date(d.toLocaleString("sv-SE",{timeZone:tzStr}));return{y:td.getFullYear(),m:td.getMonth()+1,d:td.getDate(),h:td.getHours(),i:td.getMinutes(),s:td.getSeconds(),tz:tzStr}};minitz.tp=(y,m,d,h,i,s,tz)=>{return{y:y,m:m,d:d,h:h,i:i,s:s,tz:tz}};function getTimezoneOffset(timeZone,date=new Date){const tz=date.toLocaleString("en",{timeZone:timeZone,timeStyle:"long"}).split(" ").slice(-1)[0];const dateString=date.toString();return Date.parse(`${dateString} UTC`)-Date.parse(`${dateString} ${tz}`)}function parseISOLocal(dtStr,tz){const pd=new Date(Date.parse(dtStr));if(isNaN(pd)){throw new Error("minitz: Invalid ISO8601 passed to parser.")}const stringEnd=dtStr.substring(9);if(dtStr.includes("Z")||stringEnd.includes("-")||stringEnd.includes("+")){return minitz.tp(pd.getUTCFullYear(),pd.getUTCMonth()+1,pd.getUTCDate(),pd.getUTCHours(),pd.getUTCMinutes(),pd.getUTCSeconds(),"Etc/UTC")}else{return minitz.tp(pd.getFullYear(),pd.getMonth()+1,pd.getDate(),pd.getHours(),pd.getMinutes(),pd.getSeconds(),tz)}}minitz.minitz=minitz;return minitz});

0 commit comments

Comments
 (0)