Skip to content

Commit 4fadb3c

Browse files
Merge pull request #95 from knowledgecode/develop
Develop
2 parents cc5f6a1 + 1f84db9 commit 4fadb3c

File tree

10 files changed

+375
-336
lines changed

10 files changed

+375
-336
lines changed

PLUGINS.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,20 @@ date.transformTZ('2021-03-14T03:00:00 UTC-0700', 'YYYY-MM-DD[T]HH:mm:ss [UTC]Z',
381381

382382
#### Start of DST (Daylight Saving Time)
383383

384-
For example, in the US, when local standard time is about to reach Sunday, 14 March 2021, `02:00:00` clocks are turned `forward` 1 hour to Sunday, 14 March 2021, `03:00:00` local daylight time instead. Thus there is no `02:00:00` to `02:59:59` on 14 March 2021. In such edge cases, `parseTZ()` will parse like this:
384+
For example, in the US, when local standard time is about to reach `02:00:00` on Sunday, 14 March 2021, the clocks are set `forward` by 1 hour to `03:00:00` local daylight time instead. As a result, the time from `02:00:00` to `02:59:59` on 14 March 2021 does not exist. In such edge cases, `parseTZ()` will handle the case in the following way:
385385

386386
```javascript
387387
date.parseTZ('Mar 14 2021 1:59:59', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-03-14T09:59:59Z
388-
date.parseTZ('Mar 14 2021 2:00:00', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => NaN
389-
date.parseTZ('Mar 14 2021 2:59:59', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => NaN
390388
date.parseTZ('Mar 14 2021 3:00:00', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-03-14T10:00:00Z
389+
390+
// These times don't actually exist, but parseTZ() will handle as follows:
391+
date.parseTZ('Mar 14 2021 2:00:00', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-03-14T10:00:00Z
392+
date.parseTZ('Mar 14 2021 2:59:59', 'MMM D YYYY H:mm:ss', 'America/Los_Angeles'); // => 2021-03-14T10:59:59Z
391393
```
392394

393395
#### End of DST
394396

395-
Also, when local daylight time is about to reach Sunday, 7 November 2021, `02:00:00` clocks are turned `backward` 1 hour to Sunday, 7 November 2021, `01:00:00` local standard time instead. Thus `01:00:00` to `01:59:59` on November 7 2021 is repeated twice. Since there are two possible times here, `parseTZ()` assumes that the time is the former (DST) in order to make the result unique:
397+
Also, when local daylight time is about to reach `02:00:00` on Sunday, 7 November 2021, the clocks are set `back` by 1 hour to `01:00:00` local standard time instead. As a result, the time from `01:00:00` to `01:59:59` on 7 November 2021 occurs twice. Because this time period happens twice, `parseTZ()` assumes that the time is the earlier one (during DST) in order to make the result unique:
396398

397399
```javascript
398400
// This time is DST or PST? The parseTZ() always assumes that it is DST.

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ npm i date-and-time
2525

2626
## Recent Changes
2727

28+
- 3.6.0
29+
- In `parseTZ()`, enabled parsing of the missing hour during the transition from standard time to daylight saving time into a Date type.
30+
- In `format()` with the `z` token, fixed an issue where some short time zone names were incorrect.
31+
2832
- 3.5.0
2933
- Added `addYearsTZ()`, `addMonthsTZ()`, and `addDaysTZ()` to the `timezone` plugin.
3034
- Revised the approach to adding time and removed the third parameter from `addHours()`, `addMinutes()`, `addSeconds()`, and `addMilliseconds()`.
3135

3236
- 3.4.1
3337
- Fixed an issue where `formatTZ()` would output 0:00 as 24:00 in 24-hour format in Node.js.
3438

35-
- 3.4.0
36-
- Added `zz` (time zone name) and `z` (time zone name abbreviation) tokens to the `timezone` plugin.
37-
- Fixed an issue where token extensions by other plugins were not reflected in functions provided by the `timezone` plugin.
38-
3939
## Usage
4040

4141
- ES Modules:
@@ -69,6 +69,16 @@ import date from '/path/to/date-and-time.es.min.js';
6969
### Note
7070

7171
- If you want to use ES Modules in Node.js without the transpiler, you need to add `"type": "module"` in your `package.json` or change your file extension from `.js` to `.mjs`.
72+
- If you are using TypeScript and having trouble building, please ensure that the following settings in the `compilerOptions` of your `tsconfig.json` are set to `true`.
73+
74+
```json
75+
{
76+
"compilerOptions": {
77+
"allowSyntheticDefaultImports": true,
78+
"esModuleInterop": true
79+
}
80+
}
81+
```
7282

7383
## API
7484

esm/plugin/timezone.es.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ var plugin = function (proto, date) {
455455
'Alaska Daylight Time': 'AKDT', 'Alaska Standard Time': 'AKST', 'Almaty Standard Time': 'ALMT',
456456
'Almaty Summer Time': 'ALMST', 'Amazon Standard Time': 'AMT', 'Amazon Summer Time': 'AMST',
457457
'Anadyr Standard Time': 'ANAT', 'Anadyr Summer Time': 'ANAST', 'Apia Daylight Time': 'WSDT',
458-
'Apia Standard Time': 'WSST', 'Aqtau Standard Time': 'AQTT', 'Aqtau Summer Time': 'AQTT',
458+
'Apia Standard Time': 'WSST', 'Aqtau Standard Time': 'AQTT', 'Aqtau Summer Time': 'AQTST',
459459
'Aqtobe Standard Time': 'AQTT', 'Aqtobe Summer Time': 'AQST', 'Arabian Daylight Time': 'ADT',
460460
'Arabian Standard Time': 'AST', 'Argentina Standard Time': 'ART', 'Argentina Summer Time': 'ARST',
461461
'Armenia Standard Time': 'AMT', 'Armenia Summer Time': 'AMST', 'Atlantic Daylight Time': 'ADT',
@@ -471,19 +471,18 @@ var plugin = function (proto, date) {
471471
'Central Indonesia Time': 'WITA', 'Central Standard Time': 'CST', 'Chamorro Standard Time': 'ChST',
472472
'Chatham Daylight Time': 'CHADT', 'Chatham Standard Time': 'CHAST', 'Chile Standard Time': 'CLT',
473473
'Chile Summer Time': 'CLST', 'China Daylight Time': 'CDT', 'China Standard Time': 'CST',
474-
'Choibalsan Standard Time': 'CHOT', 'Choibalsan Summer Time': 'CHOST', 'Christmas Island Time': 'CXT',
475-
'Chuuk Time': 'CHUT', 'Cocos Islands Time': 'CCT', 'Colombia Standard Time': 'COT',
476-
'Colombia Summer Time': 'COST', 'Cook Islands Half Summer Time': 'CKHST', 'Cook Islands Standard Time': 'CKT',
477-
'Coordinated Universal Time': 'UTC', 'Cuba Daylight Time': 'CDT', 'Cuba Standard Time': 'CST',
478-
'Davis Time': 'DAVT', 'Dumont-d’Urville Time': 'DDUT', 'East Africa Time': 'EAT',
479-
'East Greenland Standard Time': 'EGST', 'East Greenland Summer Time': 'EGST', 'East Kazakhstan Time': 'ALMT',
480-
'East Timor Time': 'TLT', 'Easter Island Standard Time': 'EAST', 'Easter Island Summer Time': 'EASST',
481-
'Eastern Daylight Time': 'EDT', 'Eastern European Standard Time': 'EET', 'Eastern European Summer Time': 'EEST',
482-
'Eastern Indonesia Time': 'WIT', 'Eastern Standard Time': 'EST', 'Ecuador Time': 'ECT',
483-
'Falkland Islands Standard Time': 'FKST', 'Falkland Islands Summer Time': 'FKDT', 'Fernando de Noronha Standard Time': 'FNT',
484-
'Fernando de Noronha Summer Time': 'FNST', 'Fiji Standard Time': 'FJT', 'Fiji Summer Time': 'FJST',
485-
'French Guiana Time': 'GFT', 'French Southern & Antarctic Time': 'TFT', 'Further-eastern European Time': 'FET',
486-
'GMT': 'GMT', 'Galapagos Time': 'GALT', 'Gambier Time': 'GAMT',
474+
'Christmas Island Time': 'CXT', 'Chuuk Time': 'CHUT', 'Cocos Islands Time': 'CCT',
475+
'Colombia Standard Time': 'COT', 'Colombia Summer Time': 'COST', 'Cook Islands Half Summer Time': 'CKHST',
476+
'Cook Islands Standard Time': 'CKT', 'Coordinated Universal Time': 'UTC', 'Cuba Daylight Time': 'CDT',
477+
'Cuba Standard Time': 'CST', 'Davis Time': 'DAVT', 'Dumont-d’Urville Time': 'DDUT',
478+
'East Africa Time': 'EAT', 'East Greenland Standard Time': 'EGT', 'East Greenland Summer Time': 'EGST',
479+
'East Kazakhstan Time': 'ALMT', 'East Timor Time': 'TLT', 'Easter Island Standard Time': 'EAST',
480+
'Easter Island Summer Time': 'EASST', 'Eastern Daylight Time': 'EDT', 'Eastern European Standard Time': 'EET',
481+
'Eastern European Summer Time': 'EEST', 'Eastern Indonesia Time': 'WIT', 'Eastern Standard Time': 'EST',
482+
'Ecuador Time': 'ECT', 'Falkland Islands Standard Time': 'FKST', 'Falkland Islands Summer Time': 'FKDT',
483+
'Fernando de Noronha Standard Time': 'FNT', 'Fernando de Noronha Summer Time': 'FNST', 'Fiji Standard Time': 'FJT',
484+
'Fiji Summer Time': 'FJST', 'French Guiana Time': 'GFT', 'French Southern & Antarctic Time': 'TFT',
485+
'Further-eastern European Time': 'FET', 'Galapagos Time': 'GALT', 'Gambier Time': 'GAMT',
487486
'Georgia Standard Time': 'GET', 'Georgia Summer Time': 'GEST', 'Gilbert Islands Time': 'GILT',
488487
'Greenwich Mean Time': 'GMT', 'Guam Standard Time': 'ChST', 'Gulf Standard Time': 'GST',
489488
'Guyana Time': 'GYT', 'Hawaii-Aleutian Daylight Time': 'HADT', 'Hawaii-Aleutian Standard Time': 'HAST',
@@ -495,7 +494,7 @@ var plugin = function (proto, date) {
495494
'Korean Daylight Time': 'KDT', 'Korean Standard Time': 'KST', 'Kosrae Time': 'KOST',
496495
'Krasnoyarsk Standard Time': 'KRAT', 'Krasnoyarsk Summer Time': 'KRAST', 'Kyrgyzstan Time': 'KGT',
497496
'Lanka Time': 'LKT', 'Line Islands Time': 'LINT', 'Lord Howe Daylight Time': 'LHDT',
498-
'Lord Howe Standard Time': 'LHST', 'Macao Standard Time': 'CST', 'Macao Summer Time': 'CDST',
497+
'Lord Howe Standard Time': 'LHST', 'Macao Standard Time': 'CST', 'Macao Summer Time': 'CDT',
499498
'Magadan Standard Time': 'MAGT', 'Magadan Summer Time': 'MAGST', 'Malaysia Time': 'MYT',
500499
'Maldives Time': 'MVT', 'Marquesas Time': 'MART', 'Marshall Islands Time': 'MHT',
501500
'Mauritius Standard Time': 'MUT', 'Mauritius Summer Time': 'MUST', 'Mawson Time': 'MAWT',
@@ -508,7 +507,7 @@ var plugin = function (proto, date) {
508507
'North Mariana Islands Time': 'ChST', 'Novosibirsk Standard Time': 'NOVT', 'Novosibirsk Summer Time': 'NOVST',
509508
'Omsk Standard Time': 'OMST', 'Omsk Summer Time': 'OMSST', 'Pacific Daylight Time': 'PDT',
510509
'Pacific Standard Time': 'PST', 'Pakistan Standard Time': 'PKT', 'Pakistan Summer Time': 'PKST',
511-
'Palau Time': 'PWT', 'Papua New Guinea Time': 'PGT', 'Paraguay Standard Time': 'PYST',
510+
'Palau Time': 'PWT', 'Papua New Guinea Time': 'PGT', 'Paraguay Standard Time': 'PYT',
512511
'Paraguay Summer Time': 'PYST', 'Peru Standard Time': 'PET', 'Peru Summer Time': 'PEST',
513512
'Petropavlovsk-Kamchatski Standard Time': 'PETT', 'Petropavlovsk-Kamchatski Summer Time': 'PETST', 'Philippine Standard Time': 'PST',
514513
'Philippine Summer Time': 'PHST', 'Phoenix Islands Time': 'PHOT', 'Pitcairn Time': 'PIT',
@@ -519,7 +518,7 @@ var plugin = function (proto, date) {
519518
'Singapore Standard Time': 'SGT', 'Solomon Islands Time': 'SBT', 'South Africa Standard Time': 'SAST',
520519
'South Georgia Time': 'GST', 'St. Pierre & Miquelon Daylight Time': 'PMDT', 'St. Pierre & Miquelon Standard Time': 'PMST',
521520
'Suriname Time': 'SRT', 'Syowa Time': 'SYOT', 'Tahiti Time': 'TAHT',
522-
'Taipei Daylight Time': 'TDT', 'Taipei Standard Time': 'CST', 'Tajikistan Time': 'TJT',
521+
'Taipei Daylight Time': 'CDT', 'Taipei Standard Time': 'CST', 'Tajikistan Time': 'TJT',
523522
'Tokelau Time': 'TKT', 'Tonga Standard Time': 'TOT', 'Tonga Summer Time': 'TOST',
524523
'Turkmenistan Standard Time': 'TMT', 'Tuvalu Time': 'TVT', 'Ulaanbaatar Standard Time': 'ULAT',
525524
'Ulaanbaatar Summer Time': 'ULAST', 'Uruguay Standard Time': 'UYT', 'Uruguay Summer Time': 'UYST',
@@ -607,7 +606,7 @@ var plugin = function (proto, date) {
607606
};
608607
var parseTZ = function (arg1, arg2, timeZone) {
609608
var pattern = typeof arg2 === 'string' ? date.compile(arg2) : arg2;
610-
var dateObj = typeof arg1 === 'string' ? date.parse(arg1, pattern, !!timeZone) : arg1;
609+
var time = typeof arg1 === 'string' ? date.parse(arg1, pattern, !!timeZone).getTime() : arg1;
611610
var hasZ = function (array) {
612611
for (var i = 1, len = array.length; i < len; i++) {
613612
if (!array[i].indexOf('Z')) {
@@ -617,8 +616,8 @@ var plugin = function (proto, date) {
617616
return false;
618617
};
619618

620-
if (!timeZone || hasZ(pattern) || timeZone.toUpperCase() === 'UTC') {
621-
return dateObj;
619+
if (!timeZone || hasZ(pattern) || timeZone.toUpperCase() === 'UTC' || isNaN(time)) {
620+
return new Date(time);
622621
}
623622

624623
var getOffset = function (timeZoneName) {
@@ -631,17 +630,17 @@ var plugin = function (proto, date) {
631630
return Array.isArray(value) ? value : [];
632631
};
633632

634-
var dateString2 = getDateTimeFormat('UTC').format(dateObj);
635-
var dateTimeFormat = getDateTimeFormat(timeZone);
633+
var utc = getDateTimeFormat('UTC');
634+
var tz = getDateTimeFormat(timeZone);
636635
var offset = getOffset(timeZone);
637-
var comparer = function (d) {
638-
return dateString2 === dateTimeFormat.format(d);
639-
};
640636

641-
for (var j = 0, len2 = offset.length; j < len2; j++) {
642-
var d = dateObj.getTime() - offset[j] * 1000;
643-
if (comparer(d)) {
644-
return new Date(d);
637+
for (var i = 0; i < 2; i++) {
638+
var targetString = utc.format(time - i * 24 * 60 * 60 * 1000);
639+
640+
for (var j = 0, len = offset.length; j < len; j++) {
641+
if (tz.format(time - (offset[j] + i * 24 * 60 * 60) * 1000) === targetString) {
642+
return new Date(time - offset[j] * 1000);
643+
}
645644
}
646645
}
647646
return new Date(NaN);
@@ -670,15 +669,15 @@ var plugin = function (proto, date) {
670669
var parts = formatToParts(getDateTimeFormat(timeZone), dateObj);
671670

672671
parts.month += months;
673-
var dateObj2 = parseTZ(new Date(getTimeFromParts(normalizeDateParts(parts, true))), [], timeZone);
672+
var dateObj2 = parseTZ(getTimeFromParts(normalizeDateParts(parts, true)), [], timeZone);
674673

675674
return isNaN(dateObj2.getTime()) ? date.addMonths(dateObj, months, true) : dateObj2;
676675
};
677676
var addDaysTZ = function (dateObj, days, timeZone) {
678677
var parts = formatToParts(getDateTimeFormat(timeZone), dateObj);
679678

680679
parts.day += days;
681-
var dateObj2 = parseTZ(new Date(getTimeFromParts(normalizeDateParts(parts, false))), [], timeZone);
680+
var dateObj2 = parseTZ(getTimeFromParts(normalizeDateParts(parts, false)), [], timeZone);
682681

683682
return isNaN(dateObj2.getTime()) ? date.addDays(dateObj, days, true) : dateObj2;
684683
};

0 commit comments

Comments
 (0)