esday 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +17 -9
- package/chunks/{DuP2qVZn.d.ts → BB5OVV-y.d.ts} +1 -1
- package/chunks/{Dc43cziu.d.ts → CBlQG2yl.d.ts} +1 -2
- package/chunks/CStrb5bT.js +454 -0
- package/chunks/DE-bYjyv.js +473 -0
- package/chunks/{CNIy0PdR.js → DIG_UBsE.js} +2 -1
- package/chunks/{D2Xh96oz.js → oiGWnahC.js} +6 -5
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/locales/af.d.ts +0 -1
- package/locales/am.d.ts +0 -1
- package/locales/ar-dz.d.ts +0 -1
- package/locales/ar-iq.d.ts +0 -1
- package/locales/ar-kw.d.ts +0 -1
- package/locales/ar-ly.d.ts +0 -1
- package/locales/ar-ma.d.ts +0 -1
- package/locales/ar-sa.d.ts +0 -1
- package/locales/ar-tn.d.ts +0 -1
- package/locales/ar.d.ts +0 -1
- package/locales/az.d.ts +0 -1
- package/locales/be.d.ts +0 -1
- package/locales/bg.d.ts +0 -1
- package/locales/bi.d.ts +0 -1
- package/locales/bm.d.ts +0 -1
- package/locales/bn-bd.d.ts +0 -1
- package/locales/bn.d.ts +0 -1
- package/locales/bo.d.ts +0 -1
- package/locales/br.d.ts +0 -1
- package/locales/bs.d.ts +0 -1
- package/locales/ca.d.ts +0 -1
- package/locales/cs.d.ts +0 -1
- package/locales/cv.d.ts +0 -1
- package/locales/cy.d.ts +0 -1
- package/locales/da.d.ts +0 -1
- package/locales/de-at.d.ts +0 -1
- package/locales/de-ch.d.ts +0 -1
- package/locales/de.d.ts +0 -1
- package/locales/dv.d.ts +0 -1
- package/locales/el.d.ts +0 -1
- package/locales/en-au.d.ts +0 -1
- package/locales/en-ca.d.ts +0 -1
- package/locales/en-gb.d.ts +0 -1
- package/locales/en-ie.d.ts +0 -1
- package/locales/en-il.d.ts +0 -1
- package/locales/en-in.d.ts +0 -1
- package/locales/en-nz.d.ts +0 -1
- package/locales/en-sg.d.ts +0 -1
- package/locales/en-tt.d.ts +0 -1
- package/locales/en-us.d.ts +0 -1
- package/locales/en.d.ts +0 -1
- package/locales/eo.d.ts +0 -1
- package/locales/es-do.d.ts +0 -1
- package/locales/es-mx.d.ts +0 -1
- package/locales/es-pr.d.ts +0 -1
- package/locales/es-us.d.ts +0 -1
- package/locales/es.d.ts +0 -1
- package/locales/et.d.ts +0 -1
- package/locales/eu.d.ts +0 -1
- package/locales/fa.d.ts +0 -1
- package/locales/fi.d.ts +0 -1
- package/locales/fil.d.ts +0 -1
- package/locales/fo.d.ts +0 -1
- package/locales/fr-ca.d.ts +0 -1
- package/locales/fr-ch.d.ts +0 -1
- package/locales/fr.d.ts +0 -1
- package/locales/fy.d.ts +0 -1
- package/locales/ga.d.ts +0 -1
- package/locales/gd.d.ts +0 -1
- package/locales/gl.d.ts +0 -1
- package/locales/gom-deva.d.ts +0 -1
- package/locales/gom-latn.d.ts +0 -1
- package/locales/gu.d.ts +0 -1
- package/locales/he.d.ts +0 -1
- package/locales/he.js +2 -1
- package/locales/hi.d.ts +0 -1
- package/locales/hr.d.ts +0 -1
- package/locales/ht.d.ts +0 -1
- package/locales/hu.d.ts +0 -1
- package/locales/hy-am.d.ts +0 -1
- package/locales/id.d.ts +0 -1
- package/locales/is.d.ts +0 -1
- package/locales/it-ch.d.ts +0 -1
- package/locales/it.d.ts +0 -1
- package/locales/ja.d.ts +0 -1
- package/locales/jv.d.ts +0 -1
- package/locales/ka.d.ts +0 -1
- package/locales/kk.d.ts +0 -1
- package/locales/km.d.ts +0 -1
- package/locales/kn.d.ts +0 -1
- package/locales/ko.d.ts +0 -1
- package/locales/ku.d.ts +0 -1
- package/locales/ky.d.ts +0 -1
- package/locales/lb.d.ts +0 -1
- package/locales/lo.d.ts +0 -1
- package/locales/lt.d.ts +0 -1
- package/locales/lv.d.ts +0 -1
- package/locales/me.d.ts +0 -1
- package/locales/mi.d.ts +0 -1
- package/locales/mk.d.ts +0 -1
- package/locales/ml.d.ts +0 -1
- package/locales/mn.d.ts +0 -1
- package/locales/mr.d.ts +0 -1
- package/locales/ms-my.d.ts +0 -1
- package/locales/ms.d.ts +0 -1
- package/locales/mt.d.ts +0 -1
- package/locales/my.d.ts +0 -1
- package/locales/nb.d.ts +0 -1
- package/locales/ne.d.ts +0 -1
- package/locales/nl-be.d.ts +0 -1
- package/locales/nl.d.ts +0 -1
- package/locales/nn.d.ts +0 -1
- package/locales/oc-lnc.d.ts +0 -1
- package/locales/pa-in.d.ts +0 -1
- package/locales/pl.d.ts +0 -1
- package/locales/pt-br.d.ts +0 -1
- package/locales/pt.d.ts +0 -1
- package/locales/rn.d.ts +0 -1
- package/locales/ro.d.ts +0 -1
- package/locales/ru.d.ts +0 -1
- package/locales/rw.d.ts +0 -1
- package/locales/sd.d.ts +0 -1
- package/locales/se.d.ts +0 -1
- package/locales/si.d.ts +0 -1
- package/locales/sk.d.ts +0 -1
- package/locales/sl.d.ts +0 -1
- package/locales/sq.d.ts +0 -1
- package/locales/sr-cyrl.d.ts +0 -1
- package/locales/sr.d.ts +0 -1
- package/locales/ss.d.ts +0 -1
- package/locales/sv-fi.d.ts +0 -1
- package/locales/sv.d.ts +0 -1
- package/locales/sw.d.ts +0 -1
- package/locales/ta.d.ts +0 -1
- package/locales/te.d.ts +0 -1
- package/locales/tet.d.ts +0 -1
- package/locales/tg.d.ts +0 -1
- package/locales/th.d.ts +0 -1
- package/locales/tk.d.ts +0 -1
- package/locales/tl-ph.d.ts +0 -1
- package/locales/tlh.d.ts +0 -1
- package/locales/tr.d.ts +0 -1
- package/locales/tzl.d.ts +0 -1
- package/locales/tzm-latn.d.ts +0 -1
- package/locales/tzm.d.ts +0 -1
- package/locales/ug-cn.d.ts +0 -1
- package/locales/uk.d.ts +0 -1
- package/locales/ur.d.ts +0 -1
- package/locales/uz-latn.d.ts +0 -1
- package/locales/uz.d.ts +0 -1
- package/locales/vi.d.ts +0 -1
- package/locales/x-pseudo.d.ts +0 -1
- package/locales/yo.d.ts +0 -1
- package/locales/zh-cn.d.ts +0 -1
- package/locales/zh-hk.d.ts +0 -1
- package/locales/zh-mo.d.ts +0 -1
- package/locales/zh-tw.d.ts +0 -1
- package/locales/zh.d.ts +0 -1
- package/package.json +22 -18
- package/plugins/advancedFormat/index.d.ts +1 -2
- package/plugins/advancedParse/index.d.ts +1 -2
- package/plugins/calendar/index.d.ts +1 -2
- package/plugins/dayOfYear/index.d.ts +1 -2
- package/plugins/duration/index.d.ts +1 -2
- package/plugins/duration/index.js +1 -471
- package/plugins/index.d.ts +28 -0
- package/plugins/index.js +28 -0
- package/plugins/isBetween/index.d.ts +1 -2
- package/plugins/isLeapYear/index.d.ts +1 -2
- package/plugins/isSameOrAfter/index.d.ts +1 -2
- package/plugins/isSameOrBefore/index.d.ts +1 -2
- package/plugins/isToday/index.d.ts +1 -2
- package/plugins/isTomorrow/index.d.ts +1 -2
- package/plugins/isYesterday/index.d.ts +1 -2
- package/plugins/isoWeek/index.d.ts +1 -2
- package/plugins/locale/index.d.ts +1 -2
- package/plugins/locale/index.js +4 -2
- package/plugins/localeData/index.d.ts +1 -2
- package/plugins/localeData/index.js +5 -3
- package/plugins/localizedFormat/index.d.ts +1 -2
- package/plugins/localizedParse/index.d.ts +1 -2
- package/plugins/localizedParse/index.js +2 -1
- package/plugins/minMax/index.d.ts +1 -2
- package/plugins/objectSupport/index.d.ts +1 -2
- package/plugins/pluralGetSet/index.d.ts +1 -2
- package/plugins/quarterOfYear/index.d.ts +1 -2
- package/plugins/relativeTime/index.d.ts +1 -2
- package/plugins/timezone/index.d.ts +1 -2
- package/plugins/timezone/index.js +1 -453
- package/plugins/toArray/index.d.ts +1 -2
- package/plugins/toObject/index.d.ts +1 -2
- package/plugins/utc/index.d.ts +1 -2
- package/plugins/utc/index.js +1 -1
- package/plugins/week/index.d.ts +1 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [1.1.0](https://github.com/BePo65/esday/compare/v1.0.1...v1.1.0) (2026-08-01)
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
* add index file to plugins folder for destructuring imports ([2e2f097](https://github.com/BePo65/esday/commit/2e2f097d6c1334dc7c891ded5b4ce6d859d8ae5c))
|
|
10
|
+
## [1.0.1](https://github.com/BePo65/esday/compare/v1.0.0...v1.0.1) (2026-07-30)
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* allow other types beside EsDay as first argument to diff() ([198f9da](https://github.com/BePo65/esday/commit/198f9dab0453ce6e60ec9071629197f209f36fd9))
|
|
5
15
|
## [1.0.0](https://github.com/BePo65/esday/compare/v0.6.3...v1.0.0) (2026-06-04)
|
|
6
16
|
|
|
7
17
|
## [0.6.3](https://github.com/BePo65/esday/compare/v0.6.2...v0.6.3) (2026-03-08)
|
package/README.md
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
# EsDay
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/esday)
|
|
4
4
|
[](https://github.com/kessler/license-report/blob/master/LICENSE)
|
|
5
5
|
[](https://github.com/BePo65/esday?tab=readme-ov-file#contribute)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Repository changing ownership
|
|
8
8
|
|
|
9
9
|
On the 6th of January 2026 this repository changed the ownership to one of it's maintainers, [BePo65](https://github.com/BePo65) to reduce the workload on [g-mero](https://github.com/g-mero).
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Concepts
|
|
12
|
+
|
|
13
|
+
EsDay is a JavaScript library inspired by [Day.js](https://github.com/iamkun/dayjs).
|
|
14
|
+
|
|
15
|
+
Esday has an API largely similar to Day.js (v1.11.13), but it is written in TypeScript and fully supports es modules.
|
|
16
|
+
|
|
17
|
+
Being compatible to Day.js, EsDay is also a good replacement for Moment.js.
|
|
18
|
+
|
|
19
|
+
**Highlights**
|
|
20
|
+
|
|
21
|
+
- **Modern API**: Provides the same API as Day.js, with some features moved to plugins for a simpler core.
|
|
22
|
+
- **Immutable**: esday is immutable, which means that all operations will return a new instance of esday.
|
|
23
|
+
|
|
24
|
+
## Basic use
|
|
12
25
|
|
|
13
|
-
```
|
|
26
|
+
```typescript
|
|
14
27
|
import { esday } from 'esday'
|
|
15
28
|
import { isTodayPlugin } from 'esday/plugins/isToday'
|
|
16
29
|
|
|
@@ -21,11 +34,6 @@ esday('2024-12-10').set('year', 2025).add(1, 'month').isToday()
|
|
|
21
34
|
|
|
22
35
|
For detailed information see the [full documentation](https://bepo65.github.io/esday/).
|
|
23
36
|
|
|
24
|
-
## Concepts
|
|
25
|
-
|
|
26
|
-
- **Modern API**: Provides the same API as Day.js, with some features moved to plugins for a simpler core.
|
|
27
|
-
- **Immutable**: esday is immutable, which means that all operations will return a new instance of esday.
|
|
28
|
-
|
|
29
37
|
## Differences to Day.js
|
|
30
38
|
|
|
31
39
|
- **Locale is a Plugin**: no default locale!
|
|
@@ -201,7 +201,7 @@ declare class EsDay {
|
|
|
201
201
|
add(value: UnitsObjectTypeAddSub): EsDay;
|
|
202
202
|
subtract(value: number, unit: UnitTypeAddSub): EsDay;
|
|
203
203
|
subtract(value: UnitsObjectTypeAddSub): EsDay;
|
|
204
|
-
diff(date:
|
|
204
|
+
diff(date: DateType, units?: UnitTypeAddSub, asFloat?: boolean): number;
|
|
205
205
|
/**
|
|
206
206
|
* Get the utcOffset of date in minutes.
|
|
207
207
|
* "Proxy" implementation (getter only); the full functionality including
|
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import { f as constants_default } from "./CeKusvtV.js";
|
|
2
|
+
import { n as isEmptyObject, r as isObject } from "./DuRKUq5K.js";
|
|
3
|
+
import { n as normalizeUnitWithPlurals } from "./DMdr1ZL1.js";
|
|
4
|
+
import { t as esday } from "./oiGWnahC.js";
|
|
5
|
+
//#region src/plugins/timezone/timezone-util.ts
|
|
6
|
+
/**
|
|
7
|
+
* Cache for Intl.DateTimeFormat instances for formatting date and time
|
|
8
|
+
* used to improve performance.
|
|
9
|
+
* The key is a combination of timezone and timeZoneName.
|
|
10
|
+
*/
|
|
11
|
+
const dtfCacheDateTime = {};
|
|
12
|
+
const dtfCacheTimezoneName = {};
|
|
13
|
+
const TzDefaultTimezoneLocale = "en-US";
|
|
14
|
+
/**
|
|
15
|
+
* Get a cached or newly created Intl.DateTimeFormat instance for date/time formats.
|
|
16
|
+
*
|
|
17
|
+
* This function caches Intl.DateTimeFormat objects since their creation
|
|
18
|
+
* is relatively slow. It returns a formatter configured for date and time values
|
|
19
|
+
* using the given timezone.
|
|
20
|
+
*
|
|
21
|
+
* @param timezone - The IANA timezone identifier (e.g., "Asia/Shanghai").
|
|
22
|
+
* @returns A cached or new Intl.DateTimeFormat instance.
|
|
23
|
+
*/
|
|
24
|
+
function getDateTimeFormat(timezone) {
|
|
25
|
+
const formatOptions = {
|
|
26
|
+
hour12: false,
|
|
27
|
+
hourCycle: "h23",
|
|
28
|
+
year: "numeric",
|
|
29
|
+
month: "2-digit",
|
|
30
|
+
day: "2-digit",
|
|
31
|
+
hour: "2-digit",
|
|
32
|
+
minute: "2-digit",
|
|
33
|
+
second: "2-digit",
|
|
34
|
+
weekday: "short"
|
|
35
|
+
};
|
|
36
|
+
if (timezone.length > 0) formatOptions["timeZone"] = timezone;
|
|
37
|
+
const key = `${timezone}|formatDateTime`;
|
|
38
|
+
let dtf = dtfCacheDateTime[key];
|
|
39
|
+
if (!dtf) {
|
|
40
|
+
dtf = new Intl.DateTimeFormat("en-US", formatOptions);
|
|
41
|
+
dtfCacheDateTime[key] = dtf;
|
|
42
|
+
}
|
|
43
|
+
return dtf;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get a cached or newly created Intl.DateTimeFormat instance for timezone name formats.
|
|
47
|
+
*
|
|
48
|
+
* This function caches Intl.DateTimeFormat objects since their creation
|
|
49
|
+
* is relatively slow. It returns a formatter configured for timezone names using
|
|
50
|
+
* the given timezone and locale name.
|
|
51
|
+
*
|
|
52
|
+
* @param timezone - The IANA timezone identifier (e.g., "Asia/Shanghai").
|
|
53
|
+
* @param localeName - Optional settings for the name of the locale to use.
|
|
54
|
+
* @returns A cached or new Intl.DateTimeFormat instance.
|
|
55
|
+
*/
|
|
56
|
+
function getTimezoneNameFormat(timezone, localeName = TzDefaultTimezoneLocale) {
|
|
57
|
+
const formatOptions = {
|
|
58
|
+
year: "numeric",
|
|
59
|
+
month: "2-digit",
|
|
60
|
+
timeZoneName: "short"
|
|
61
|
+
};
|
|
62
|
+
if (timezone.length > 0) formatOptions["timeZone"] = timezone;
|
|
63
|
+
const key = `${timezone}|localeName|timezoneName`;
|
|
64
|
+
let dtf = dtfCacheTimezoneName[key];
|
|
65
|
+
if (!dtf) {
|
|
66
|
+
dtf = new Intl.DateTimeFormat(localeName, formatOptions);
|
|
67
|
+
dtfCacheTimezoneName[key] = dtf;
|
|
68
|
+
}
|
|
69
|
+
return dtf;
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/plugins/timezone/index.ts
|
|
73
|
+
/**
|
|
74
|
+
* timezone plugin
|
|
75
|
+
*
|
|
76
|
+
* This plugin adds the 'tz' method to the EsDay class.
|
|
77
|
+
* This plugin adds the 'tz' method/object to the esday class with the methods 'setDefault', 'getDefault' and 'guess'.
|
|
78
|
+
*
|
|
79
|
+
* This plugin adds the formatting tokens 'z' and 'Z' to EsDay.
|
|
80
|
+
*
|
|
81
|
+
* This plugin requires the 'utc' plugin.
|
|
82
|
+
* When parsing with a locale then the 'advancedParse' plugin, the 'localizedParse' plugin
|
|
83
|
+
* and at least one registered locale.
|
|
84
|
+
* This plugin can handle the 'locale' plugin.
|
|
85
|
+
*
|
|
86
|
+
* esday parameters in '$conf' defined in timezone plugin:
|
|
87
|
+
* timezone name of the timezone of this instance
|
|
88
|
+
*/
|
|
89
|
+
const UNIT_ARRAY_MAP = {
|
|
90
|
+
year: 0,
|
|
91
|
+
month: 1,
|
|
92
|
+
date: 2,
|
|
93
|
+
hour: 3,
|
|
94
|
+
minute: 4,
|
|
95
|
+
second: 5
|
|
96
|
+
};
|
|
97
|
+
const maxDstDiff = 7200 * 1e3;
|
|
98
|
+
const typeToPos = {
|
|
99
|
+
year: 0,
|
|
100
|
+
month: 1,
|
|
101
|
+
day: 2,
|
|
102
|
+
hour: 3,
|
|
103
|
+
minute: 4,
|
|
104
|
+
second: 5
|
|
105
|
+
};
|
|
106
|
+
const matchContainsOffset = /^(\d{4})[-/](\d{1,2})[-/](\d{0,2}).*(([+-]\d\d:?(\d\d)?)$|Z$)/;
|
|
107
|
+
/**
|
|
108
|
+
* Generate formatted date parts for a given timestamp and timezone.
|
|
109
|
+
*
|
|
110
|
+
* This function creates a Date object from the given timestamp,
|
|
111
|
+
* retrieves a cached or new Intl.DateTimeFormat instance using
|
|
112
|
+
* the provided timezone and returns the formatted parts
|
|
113
|
+
* (year, month, day-of-month, hour, minute, second, weekday)
|
|
114
|
+
*
|
|
115
|
+
* @param timestamp - The timestamp in milliseconds since the Unix epoch.
|
|
116
|
+
* @param timezone - The IANA timezone identifier (e.g., "Asia/Shanghai").
|
|
117
|
+
* @returns An array of formatted date parts.
|
|
118
|
+
*/
|
|
119
|
+
function makeFormatPartsDateTime(timestamp, timezone) {
|
|
120
|
+
const dtf = getDateTimeFormat(timezone);
|
|
121
|
+
const date = new Date(timestamp);
|
|
122
|
+
return dtf.formatToParts(date);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Do we have to convert parsed instance to another timezone?
|
|
126
|
+
* @param input - input value to be parsed by plugin timezone
|
|
127
|
+
* @returns true: result of parsing of input value must be converted to timezone
|
|
128
|
+
*/
|
|
129
|
+
function convertRequired(input) {
|
|
130
|
+
const requiresObjectSupport = isObject(input) && !(input instanceof Date) && input !== null && !isEmptyObject(input);
|
|
131
|
+
return typeof input !== "string" && !Array.isArray(input) && !requiresObjectSupport;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Convert en-US short weekday names to a number using values like Date.getDay() does.
|
|
135
|
+
* @param shortWeekdayName - short weekday name (in locale en-US)
|
|
136
|
+
* @returns - number indicating the weekday (0=Sunday, 1=Monday, ...)
|
|
137
|
+
*/
|
|
138
|
+
function weekdayNameToNumber(shortWeekdayName) {
|
|
139
|
+
switch (shortWeekdayName) {
|
|
140
|
+
case "Sun": return 0;
|
|
141
|
+
case "Mon": return 1;
|
|
142
|
+
case "Tue": return 2;
|
|
143
|
+
case "Wed": return 3;
|
|
144
|
+
case "Thu": return 4;
|
|
145
|
+
case "Fri": return 5;
|
|
146
|
+
case "Sat": return 6;
|
|
147
|
+
/* v8 ignore next -- @preserve */
|
|
148
|
+
default: return -1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get the date components (year, month, day ...) of the given timestamp,
|
|
153
|
+
* if formatted in the given timezone.
|
|
154
|
+
* The function returns an array with the values for
|
|
155
|
+
* [year, month, day-of-month, hour, minute, second, weekday].
|
|
156
|
+
* 'month' is 1..12; 'day-of-month' is 1..31, 'weekday' is 0..6.
|
|
157
|
+
* @param timestamp - timestamp to convert
|
|
158
|
+
* @param timezone - timezone to use for formatting
|
|
159
|
+
* @returns array of numbers with the date components of the timestamp in the timezone
|
|
160
|
+
*/
|
|
161
|
+
function timestampToDateTimeComponents(timestamp, timezone) {
|
|
162
|
+
const formatResult = makeFormatPartsDateTime(timestamp, timezone);
|
|
163
|
+
const filled = [];
|
|
164
|
+
let weekdayNumber = -1;
|
|
165
|
+
for (let i = 0; i < formatResult.length; i += 1) {
|
|
166
|
+
const { type, value } = formatResult[i];
|
|
167
|
+
if (type === "weekday") {
|
|
168
|
+
weekdayNumber = weekdayNameToNumber(value);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const pos = typeToPos[type];
|
|
172
|
+
if (pos >= 0) filled[pos] = Number.parseInt(value, 10);
|
|
173
|
+
filled[6] = weekdayNumber;
|
|
174
|
+
}
|
|
175
|
+
return filled;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Get the formatted timezone name of the given timestamp, if formatted in
|
|
179
|
+
* the given timezone.
|
|
180
|
+
* The function returns the short name of the timezone using the given locale.
|
|
181
|
+
* 'month' is 1..12; 'day-of-month' is 1..31, 'weekday' is 0..6.
|
|
182
|
+
* @param timestamp - timestamp to convert
|
|
183
|
+
* @param timezone - timezone to use for conversion
|
|
184
|
+
* @param timezone - timezone to use for conversion
|
|
185
|
+
* @returns array of numbers with the date components of the timestamp in the timezone
|
|
186
|
+
*/
|
|
187
|
+
function timestampToTimezoneName(timestamp, timezone, locale) {
|
|
188
|
+
const dtf = getTimezoneNameFormat(timezone, locale);
|
|
189
|
+
const date = new Date(timestamp);
|
|
190
|
+
const formatResult = dtf.formatToParts(date);
|
|
191
|
+
let timezoneNameShort = timezone;
|
|
192
|
+
/* istanbul ignore else -- @preserve */
|
|
193
|
+
if (formatResult.length >= 4 && formatResult[4].type === "timeZoneName") timezoneNameShort = formatResult[4].value;
|
|
194
|
+
return timezoneNameShort;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Calculate the new basic values for year, month etc. when using the "set" method.
|
|
198
|
+
* These values must be adjusted to the current timezone.
|
|
199
|
+
* @param dateComponents - array with the components of the formatted timestamp (year, month, ...)
|
|
200
|
+
* @param currentMs - milliseconds of the current timestamp (0..999)
|
|
201
|
+
* @param unit - normalized unit to set
|
|
202
|
+
* @param values - value to set (or values, if applicable e.g. for year)
|
|
203
|
+
* @returns literal object with year, month etc. after setting the new value(s)
|
|
204
|
+
*/
|
|
205
|
+
function calculateBaseComponentsForSet(dateComponents, currentMs, unit, values) {
|
|
206
|
+
let year = dateComponents[0];
|
|
207
|
+
let month = dateComponents[1] - 1;
|
|
208
|
+
let day = dateComponents[2];
|
|
209
|
+
let hour = dateComponents[3];
|
|
210
|
+
let minute = dateComponents[4];
|
|
211
|
+
let second = dateComponents[5];
|
|
212
|
+
let ms = currentMs;
|
|
213
|
+
const mainValue = values[0];
|
|
214
|
+
if (unit === constants_default.YEAR) {
|
|
215
|
+
year = mainValue;
|
|
216
|
+
if (values.length > 1) month = values[1];
|
|
217
|
+
if (values.length > 2) day = values[2];
|
|
218
|
+
} else if (unit === constants_default.MONTH) {
|
|
219
|
+
month = mainValue;
|
|
220
|
+
if (values.length > 1) day = values[1];
|
|
221
|
+
} else if (unit === constants_default.DAY) {
|
|
222
|
+
const currentWeekday = dateComponents[6];
|
|
223
|
+
day += mainValue - currentWeekday;
|
|
224
|
+
} else if (unit === constants_default.DAY_OF_MONTH) day = mainValue;
|
|
225
|
+
else if (unit === constants_default.HOUR) hour = mainValue;
|
|
226
|
+
else if (unit === constants_default.MIN) minute = mainValue;
|
|
227
|
+
else if (unit === constants_default.SECOND) second = mainValue;
|
|
228
|
+
else if (unit === constants_default.MS) ms = mainValue;
|
|
229
|
+
if (unit === constants_default.YEAR || unit === constants_default.MONTH) {
|
|
230
|
+
const lastDay = new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
|
|
231
|
+
if (day > lastDay) day = lastDay;
|
|
232
|
+
}
|
|
233
|
+
let valuesIndex = 1;
|
|
234
|
+
if (unit === constants_default.HOUR && values.length > valuesIndex) minute = values[valuesIndex++];
|
|
235
|
+
if ((unit === constants_default.HOUR || unit === constants_default.MIN) && values.length > valuesIndex) second = values[valuesIndex++];
|
|
236
|
+
if ((unit === constants_default.HOUR || unit === constants_default.MIN || unit === constants_default.SECOND) && values.length > valuesIndex) ms = values[valuesIndex++];
|
|
237
|
+
return {
|
|
238
|
+
year,
|
|
239
|
+
month,
|
|
240
|
+
day,
|
|
241
|
+
hour,
|
|
242
|
+
minute,
|
|
243
|
+
second,
|
|
244
|
+
ms
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Gets the timezone of the given date formatted in the short form (e.g. 'PST')
|
|
249
|
+
* @param sourceDate - date to format the timezone for
|
|
250
|
+
* @returns the short form of the timezone of the given sourceDate
|
|
251
|
+
*/
|
|
252
|
+
function formatTimezoneShort(sourceDate) {
|
|
253
|
+
const timezone = sourceDate.tz();
|
|
254
|
+
const localeName = sourceDate.locale?.() ?? esday.locale?.();
|
|
255
|
+
return timestampToTimezoneName(sourceDate.valueOf(), timezone, localeName);
|
|
256
|
+
}
|
|
257
|
+
const timezonePLugin = (_, dayClass, esdayFactory) => {
|
|
258
|
+
const noTimezone = "";
|
|
259
|
+
let defaultTimezone;
|
|
260
|
+
/**
|
|
261
|
+
* Get timezone offset for timestamp in given timezone
|
|
262
|
+
* @param timestamp - timestamp to use
|
|
263
|
+
* @param timezone - timezone to calculate offset for
|
|
264
|
+
* @returns utc offset for timestamp in given timezone
|
|
265
|
+
*/
|
|
266
|
+
const tzOffset = (timestamp, timezone) => {
|
|
267
|
+
const filled = timestampToDateTimeComponents(timestamp, timezone);
|
|
268
|
+
const utcString = `${filled[0]}-${filled[1]}-${filled[2]} ${filled[3]}:${filled[4]}:${filled[5]}:000`;
|
|
269
|
+
const utcTs = esdayFactory.utc(utcString).valueOf();
|
|
270
|
+
let timestampAsEpoch = timestamp.valueOf();
|
|
271
|
+
const msOfTimestamp = timestampAsEpoch % 1e3;
|
|
272
|
+
timestampAsEpoch -= msOfTimestamp;
|
|
273
|
+
return (utcTs - timestampAsEpoch) / (60 * 1e3);
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* Move to the earlier date in an DST overlap, if we are in an overlap. Otherwise return input date.
|
|
277
|
+
* The return value is an literal object in the form of {timestampFixed, tzOffsetFixed, isInOverlap}.
|
|
278
|
+
* @param timestampOriginal - timestamp to be fixed
|
|
279
|
+
* @param tzOffsetOriginal - tzOffset of timestampOriginal
|
|
280
|
+
* @param timezone - timezone to use
|
|
281
|
+
* @returns literal object containing fixed date (or original date, if we are not in an overlap)
|
|
282
|
+
*/
|
|
283
|
+
const fixOverlap = (timestampOriginal, tzOffsetOriginal, timezone) => {
|
|
284
|
+
let timestampFixed = timestampOriginal;
|
|
285
|
+
let tzOffsetFixed = tzOffsetOriginal;
|
|
286
|
+
let isInOverlap = false;
|
|
287
|
+
const tzOffsetPreDst = tzOffset(timestampOriginal - maxDstDiff, timezone);
|
|
288
|
+
if (tzOffsetPreDst > tzOffsetOriginal) {
|
|
289
|
+
timestampFixed = timestampOriginal - (tzOffsetPreDst - tzOffsetOriginal) * 60 * 1e3;
|
|
290
|
+
tzOffsetFixed = tzOffset(timestampFixed, timezone);
|
|
291
|
+
if (tzOffsetFixed === tzOffsetOriginal) {
|
|
292
|
+
timestampFixed = timestampOriginal;
|
|
293
|
+
tzOffsetFixed = tzOffsetOriginal;
|
|
294
|
+
} else isInOverlap = true;
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
timestampFixed,
|
|
298
|
+
tzOffsetFixed,
|
|
299
|
+
isInOverlap
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
/**
|
|
303
|
+
* Change the timestamp and timezone offset of a timestamp to change the timezone while keeping the time value.
|
|
304
|
+
* @param timestampOriginal - timestamp to fix the offset for, if necessary
|
|
305
|
+
* @param tzOffsetSource - tzOffset of the originalTimestamp in the source timezone
|
|
306
|
+
* @param tzOffsetTarget - tzOffset of the originalTimestamp in the target timezone
|
|
307
|
+
* @param tzTarget - timezone to use
|
|
308
|
+
* @returns array with updated timestamp and fixed offset
|
|
309
|
+
*/
|
|
310
|
+
const fixOffsetConvert = (timestampOriginal, tzOffsetSource, tzOffsetTarget, tzTarget) => {
|
|
311
|
+
const timestampGuess1 = timestampOriginal - (tzOffsetTarget - tzOffsetSource) * 60 * 1e3;
|
|
312
|
+
const tzOffsetGuess1 = tzOffset(timestampGuess1, tzTarget);
|
|
313
|
+
if (tzOffsetGuess1 === tzOffsetTarget) {
|
|
314
|
+
const { timestampFixed, tzOffsetFixed } = fixOverlap(timestampGuess1, tzOffsetGuess1, tzTarget);
|
|
315
|
+
return [timestampFixed, tzOffsetFixed];
|
|
316
|
+
}
|
|
317
|
+
const timestampGuess2 = timestampGuess1 - (tzOffsetGuess1 - tzOffsetTarget) * 60 * 1e3;
|
|
318
|
+
const tzOffsetGuess2 = tzOffset(timestampGuess2, tzTarget);
|
|
319
|
+
if (tzOffsetGuess2 === tzOffsetGuess1) return [timestampGuess2, tzOffsetGuess2];
|
|
320
|
+
else if (tzOffsetGuess1 < tzOffsetTarget) return [timestampGuess2, tzOffsetGuess2];
|
|
321
|
+
else return [timestampGuess1, tzOffsetGuess1];
|
|
322
|
+
};
|
|
323
|
+
esdayFactory.tz = (input, ...others) => {
|
|
324
|
+
let timezone = defaultTimezone;
|
|
325
|
+
if (others.length > 0) timezone = others.pop();
|
|
326
|
+
const parsedInput = esdayFactory.utc(input, ...others);
|
|
327
|
+
if (!parsedInput.isValid()) return parsedInput;
|
|
328
|
+
const offsetParsedInput = tzOffset(parsedInput.valueOf(), timezone ?? noTimezone);
|
|
329
|
+
if (convertRequired(input) || typeof input === "string" && matchContainsOffset.test(input)) return parsedInput.tz(timezone);
|
|
330
|
+
const [targetTimestamp, targetOffset] = fixOffsetConvert(parsedInput.valueOf(), 0, offsetParsedInput, timezone ?? noTimezone);
|
|
331
|
+
const result = esdayFactory.utc(targetTimestamp, ...others);
|
|
332
|
+
result["$conf"].utcOffset = targetOffset;
|
|
333
|
+
result["$conf"].localOffset = -1 * result.toDate().getTimezoneOffset();
|
|
334
|
+
result["$conf"].utc = targetOffset === 0;
|
|
335
|
+
if (result["$conf"].localeName !== void 0 && parsedInput["$conf"].localeName !== void 0) result["$conf"].localeName = parsedInput["$conf"].localeName;
|
|
336
|
+
result["$conf"].timezone = timezone;
|
|
337
|
+
return result;
|
|
338
|
+
};
|
|
339
|
+
esdayFactory.tz.guess = () => Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
340
|
+
esdayFactory.tz.setDefault = (timezone) => {
|
|
341
|
+
defaultTimezone = timezone;
|
|
342
|
+
};
|
|
343
|
+
esdayFactory.tz.getDefault = () => {
|
|
344
|
+
return defaultTimezone;
|
|
345
|
+
};
|
|
346
|
+
const proto = dayClass.prototype;
|
|
347
|
+
proto.tz = function(timezone, keepLocalTime) {
|
|
348
|
+
if (timezone === void 0) return this["$conf"].timezone;
|
|
349
|
+
const result = this.clone();
|
|
350
|
+
const tzOffsetTarget = tzOffset(this.valueOf(), timezone);
|
|
351
|
+
const localOffset = -1 * this.toDate().getTimezoneOffset();
|
|
352
|
+
result["$conf"].localOffset = localOffset;
|
|
353
|
+
result["$conf"].utcOffset = tzOffsetTarget;
|
|
354
|
+
result["$conf"].utc = tzOffsetTarget === 0;
|
|
355
|
+
result["$conf"].timezone = timezone;
|
|
356
|
+
if (keepLocalTime) {
|
|
357
|
+
const timestampSource = result.valueOf();
|
|
358
|
+
let tzOffsetSource = 0;
|
|
359
|
+
if (!this["$conf"].utc) tzOffsetSource = this["$conf"].timezone ? Number(this["$conf"].utcOffset) : localOffset;
|
|
360
|
+
const [timestampFixed, tzOffsetFixed] = fixOffsetConvert(timestampSource, tzOffsetSource, tzOffsetTarget, timezone);
|
|
361
|
+
result["$conf"].utcOffset = tzOffsetFixed;
|
|
362
|
+
result["$conf"].utc = tzOffsetFixed === 0;
|
|
363
|
+
if (timestampSource !== timestampFixed) result.$d = new Date(timestampFixed);
|
|
364
|
+
}
|
|
365
|
+
return result;
|
|
366
|
+
};
|
|
367
|
+
const oldValueOf = proto.valueOf;
|
|
368
|
+
proto.valueOf = function() {
|
|
369
|
+
if (this["$conf"]?.timezone) return this.$d.valueOf();
|
|
370
|
+
return oldValueOf.call(this);
|
|
371
|
+
};
|
|
372
|
+
const oldGet = proto.get;
|
|
373
|
+
proto.get = function(unit) {
|
|
374
|
+
const normalizedUnit = normalizeUnitWithPlurals(unit);
|
|
375
|
+
if (normalizedUnit === constants_default.QUARTER || normalizedUnit === constants_default.WEEK || normalizedUnit === constants_default.ISOWEEK || this["$conf"]?.utc) return oldGet.call(this, unit);
|
|
376
|
+
if (this["$conf"]?.timezone) {
|
|
377
|
+
const timezone = this["$conf"].timezone;
|
|
378
|
+
const dateComponents = timestampToDateTimeComponents(this.valueOf(), timezone);
|
|
379
|
+
if (normalizedUnit === constants_default.MS) return this["$d"].getUTCMilliseconds();
|
|
380
|
+
else if (normalizedUnit === constants_default.MONTH) return dateComponents[1] - 1;
|
|
381
|
+
else if (normalizedUnit === constants_default.DAY) return dateComponents[6];
|
|
382
|
+
else return dateComponents[UNIT_ARRAY_MAP[normalizedUnit]];
|
|
383
|
+
}
|
|
384
|
+
return oldGet.call(this, unit);
|
|
385
|
+
};
|
|
386
|
+
const old$set = proto["$set"];
|
|
387
|
+
proto["$set"] = function(unit, values) {
|
|
388
|
+
if (isObject(unit)) return old$set.call(this, unit, values);
|
|
389
|
+
const timezone = this["$conf"]?.timezone;
|
|
390
|
+
if (!timezone) return old$set.call(this, unit, values);
|
|
391
|
+
const normalizedUnit = normalizeUnitWithPlurals(unit);
|
|
392
|
+
const { year, month, day, hour, minute, second, ms } = calculateBaseComponentsForSet(timestampToDateTimeComponents(this.valueOf(), timezone), this.valueOf() % 1e3, normalizedUnit, values);
|
|
393
|
+
const utcAsLocal = Date.UTC(year, month, day, hour, minute, second, ms);
|
|
394
|
+
const [timestampFixed, tzOffsetFixed] = fixOffsetConvert(utcAsLocal, 0, tzOffset(utcAsLocal, timezone), timezone);
|
|
395
|
+
this.$d = new Date(timestampFixed);
|
|
396
|
+
this["$conf"].utcOffset = tzOffsetFixed;
|
|
397
|
+
this["$conf"].localOffset = -1 * this.$d.getTimezoneOffset();
|
|
398
|
+
this["$conf"].utc = tzOffsetFixed === 0;
|
|
399
|
+
return this;
|
|
400
|
+
};
|
|
401
|
+
const oldStartOf = dayClass.prototype.startOf;
|
|
402
|
+
proto.startOf = function(units) {
|
|
403
|
+
if (!this["$conf"]?.["timezone"]) return oldStartOf.call(this, units);
|
|
404
|
+
const withoutTz = esdayFactory(this.format("YYYY-MM-DD HH:mm:ss:SSS"));
|
|
405
|
+
return oldStartOf.call(withoutTz, units).tz(this["$conf"]["timezone"], true);
|
|
406
|
+
};
|
|
407
|
+
const oldEndOf = dayClass.prototype.endOf;
|
|
408
|
+
proto.endOf = function(units) {
|
|
409
|
+
if (!this["$conf"]?.["timezone"]) return oldEndOf.call(this, units);
|
|
410
|
+
const withoutTz = esdayFactory(this.format("YYYY-MM-DD HH:mm:ss:SSS"));
|
|
411
|
+
return oldEndOf.call(withoutTz, units).tz(this["$conf"]["timezone"], true);
|
|
412
|
+
};
|
|
413
|
+
const oldAdd = proto.add;
|
|
414
|
+
proto.add = function(value, unit) {
|
|
415
|
+
if (isObject(value) || unit === void 0) return oldAdd.call(this, value, unit);
|
|
416
|
+
const newValue = oldAdd.call(this, value, unit);
|
|
417
|
+
if (!this["$conf"].timezone) return newValue;
|
|
418
|
+
const timezone = this["$conf"]["timezone"];
|
|
419
|
+
const timestamp = newValue.valueOf();
|
|
420
|
+
const tzOldOffset = tzOffset(timestamp, timezone);
|
|
421
|
+
const normalizedUnit = normalizeUnitWithPlurals(unit);
|
|
422
|
+
const timeUnits = [
|
|
423
|
+
constants_default.HOUR,
|
|
424
|
+
constants_default.MIN,
|
|
425
|
+
constants_default.SECOND,
|
|
426
|
+
constants_default.MS
|
|
427
|
+
];
|
|
428
|
+
const { isInOverlap } = fixOverlap(timestamp, tzOldOffset, timezone);
|
|
429
|
+
if (timeUnits.includes(normalizedUnit) && isInOverlap) {
|
|
430
|
+
newValue["$conf"].utcOffset = tzOldOffset;
|
|
431
|
+
newValue["$conf"].utc = tzOldOffset === 0;
|
|
432
|
+
return newValue;
|
|
433
|
+
}
|
|
434
|
+
const [timestampFixed, tzOffsetFixed] = fixOffsetConvert(timestamp, tzOldOffset, tzOffset(timestamp, timezone), timezone);
|
|
435
|
+
newValue.$d = new Date(timestampFixed);
|
|
436
|
+
newValue["$conf"].utcOffset = tzOffsetFixed;
|
|
437
|
+
newValue["$conf"].localOffset = -1 * newValue.$d.getTimezoneOffset();
|
|
438
|
+
newValue["$conf"].utc = tzOffsetFixed === 0;
|
|
439
|
+
return newValue;
|
|
440
|
+
};
|
|
441
|
+
const oldSubtract = proto.subtract;
|
|
442
|
+
proto.subtract = function(value, unit) {
|
|
443
|
+
if (isObject(value) || unit === void 0) return oldSubtract.call(this, value, unit);
|
|
444
|
+
if (this["$conf"].timezone) return proto.add.call(this, -value, unit);
|
|
445
|
+
return oldSubtract.call(this, value, unit);
|
|
446
|
+
};
|
|
447
|
+
const additionalTokens = {
|
|
448
|
+
z: formatTimezoneShort,
|
|
449
|
+
zz: formatTimezoneShort
|
|
450
|
+
};
|
|
451
|
+
esdayFactory.addFormatTokenDefinitions(additionalTokens);
|
|
452
|
+
};
|
|
453
|
+
//#endregion
|
|
454
|
+
export { timezonePLugin as t };
|