@koine/browser 2.0.0-beta.124 → 2.0.0-beta.126
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/createStorage.cjs.js +0 -1
- package/createStorage.d.ts +4 -4
- package/createStorage.esm.js +0 -1
- package/getZonedDate.cjs.js +2 -652
- package/getZonedDate.esm.js +1 -651
- package/gtagPageview.cjs.js +0 -1
- package/gtagPageview.esm.js +0 -1
- package/index.cjs.js +0 -2
- package/index.esm.js +0 -2
- package/listenUrlSearchParams.cjs.js +0 -2
- package/listenUrlSearchParams.esm.js +0 -2
- package/navigateToMergedParams.cjs.js +0 -1
- package/navigateToMergedParams.esm.js +0 -1
- package/navigateWithoutUrlParam.cjs.js +0 -1
- package/navigateWithoutUrlParam.esm.js +0 -1
- package/package.json +23 -3
- package/storage.cjs.js +0 -1
- package/storage.d.ts +6 -6
- package/storage.esm.js +0 -1
- package/storageClient.d.ts +3 -3
package/createStorage.cjs.js
CHANGED
package/createStorage.d.ts
CHANGED
|
@@ -2,11 +2,11 @@ export type CreateStorageConfig = Record<string, any>;
|
|
|
2
2
|
export declare let createStorage: <T extends CreateStorageConfig>(config: Partial<T>, useSessionStorage?: boolean) => {
|
|
3
3
|
get<TKey extends Extract<keyof T, string>>(key: TKey, defaultValue?: null | T[TKey]): T[TKey] | null;
|
|
4
4
|
getAll(defaultValues?: Partial<T>): T;
|
|
5
|
-
set<
|
|
5
|
+
set<TKey extends Extract<keyof T, string>>(key: TKey, value?: T[TKey]): void;
|
|
6
6
|
setMany(newValues: Partial<T>): void;
|
|
7
|
-
has<
|
|
8
|
-
remove<
|
|
7
|
+
has<TKey extends Extract<keyof T, string>>(key: TKey): any;
|
|
8
|
+
remove<TKey extends Extract<keyof T, string>>(key: TKey): void;
|
|
9
9
|
clear(): void;
|
|
10
|
-
watch: <
|
|
10
|
+
watch: <TKey extends keyof T>(keyToWatch: TKey, onRemoved?: () => void, onAdded?: () => void) => () => void | undefined;
|
|
11
11
|
};
|
|
12
12
|
export default createStorage;
|
package/createStorage.esm.js
CHANGED
package/getZonedDate.cjs.js
CHANGED
|
@@ -2,659 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var dateFnsTz = require('date-fns-tz');
|
|
5
6
|
var utils = require('@koine/utils');
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
-
* Returns the [year, month, day, hour, minute, seconds] tokens of the provided
|
|
9
|
-
* `date` as it will be rendered in the `timeZone`.
|
|
10
|
-
*/ function _instanceof$1(left, right) {
|
|
11
|
-
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
12
|
-
return !!right[Symbol.hasInstance](left);
|
|
13
|
-
} else {
|
|
14
|
-
return left instanceof right;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function tzTokenizeDate(date, timeZone) {
|
|
18
|
-
var dtf = getDateTimeFormat(timeZone);
|
|
19
|
-
return 'formatToParts' in dtf ? partsOffset(dtf, date) : hackyOffset(dtf, date);
|
|
20
|
-
}
|
|
21
|
-
var typeToPos = {
|
|
22
|
-
year: 0,
|
|
23
|
-
month: 1,
|
|
24
|
-
day: 2,
|
|
25
|
-
hour: 3,
|
|
26
|
-
minute: 4,
|
|
27
|
-
second: 5
|
|
28
|
-
};
|
|
29
|
-
function partsOffset(dtf, date) {
|
|
30
|
-
try {
|
|
31
|
-
var formatted = dtf.formatToParts(date);
|
|
32
|
-
var filled = [];
|
|
33
|
-
for(var i = 0; i < formatted.length; i++){
|
|
34
|
-
var pos = typeToPos[formatted[i].type];
|
|
35
|
-
if (pos !== undefined) {
|
|
36
|
-
filled[pos] = parseInt(formatted[i].value, 10);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return filled;
|
|
40
|
-
} catch (error) {
|
|
41
|
-
if (_instanceof$1(error, RangeError)) {
|
|
42
|
-
return [
|
|
43
|
-
NaN
|
|
44
|
-
];
|
|
45
|
-
}
|
|
46
|
-
throw error;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function hackyOffset(dtf, date) {
|
|
50
|
-
var formatted = dtf.format(date);
|
|
51
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
52
|
-
var parsed = /(\d+)\/(\d+)\/(\d+),? (\d+):(\d+):(\d+)/.exec(formatted);
|
|
53
|
-
// const [, fMonth, fDay, fYear, fHour, fMinute, fSecond] = parsed
|
|
54
|
-
// return [fYear, fMonth, fDay, fHour, fMinute, fSecond]
|
|
55
|
-
return [
|
|
56
|
-
parseInt(parsed[3], 10),
|
|
57
|
-
parseInt(parsed[1], 10),
|
|
58
|
-
parseInt(parsed[2], 10),
|
|
59
|
-
parseInt(parsed[4], 10),
|
|
60
|
-
parseInt(parsed[5], 10),
|
|
61
|
-
parseInt(parsed[6], 10)
|
|
62
|
-
];
|
|
63
|
-
}
|
|
64
|
-
// Get a cached Intl.DateTimeFormat instance for the IANA `timeZone`. This can be used
|
|
65
|
-
// to get deterministic local date/time output according to the `en-US` locale which
|
|
66
|
-
// can be used to extract local time parts as necessary.
|
|
67
|
-
var dtfCache = {};
|
|
68
|
-
function getDateTimeFormat(timeZone) {
|
|
69
|
-
if (!dtfCache[timeZone]) {
|
|
70
|
-
// New browsers use `hourCycle`, IE and Chrome <73 does not support it and uses `hour12`
|
|
71
|
-
var testDateFormatted = new Intl.DateTimeFormat('en-US', {
|
|
72
|
-
hourCycle: 'h23',
|
|
73
|
-
timeZone: 'America/New_York',
|
|
74
|
-
year: 'numeric',
|
|
75
|
-
month: '2-digit',
|
|
76
|
-
day: '2-digit',
|
|
77
|
-
hour: '2-digit',
|
|
78
|
-
minute: '2-digit',
|
|
79
|
-
second: '2-digit'
|
|
80
|
-
}).format(new Date('2014-06-25T04:00:00.123Z'));
|
|
81
|
-
var hourCycleSupported = testDateFormatted === '06/25/2014, 00:00:00' || testDateFormatted === '06/25/2014 00:00:00';
|
|
82
|
-
dtfCache[timeZone] = hourCycleSupported ? new Intl.DateTimeFormat('en-US', {
|
|
83
|
-
hourCycle: 'h23',
|
|
84
|
-
timeZone: timeZone,
|
|
85
|
-
year: 'numeric',
|
|
86
|
-
month: 'numeric',
|
|
87
|
-
day: '2-digit',
|
|
88
|
-
hour: '2-digit',
|
|
89
|
-
minute: '2-digit',
|
|
90
|
-
second: '2-digit'
|
|
91
|
-
}) : new Intl.DateTimeFormat('en-US', {
|
|
92
|
-
hour12: false,
|
|
93
|
-
timeZone: timeZone,
|
|
94
|
-
year: 'numeric',
|
|
95
|
-
month: 'numeric',
|
|
96
|
-
day: '2-digit',
|
|
97
|
-
hour: '2-digit',
|
|
98
|
-
minute: '2-digit',
|
|
99
|
-
second: '2-digit'
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
return dtfCache[timeZone];
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Use instead of `new Date(Date.UTC(...))` to support years below 100 which doesn't work
|
|
107
|
-
* otherwise due to the nature of the
|
|
108
|
-
* [`Date` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years.
|
|
109
|
-
*
|
|
110
|
-
* For `Date.UTC(...)`, use `newDateUTC(...).getTime()`.
|
|
111
|
-
*/ function newDateUTC(fullYear, month, day, hour, minute, second, millisecond) {
|
|
112
|
-
var utcDate = new Date(0);
|
|
113
|
-
utcDate.setUTCFullYear(fullYear, month, day);
|
|
114
|
-
utcDate.setUTCHours(hour, minute, second, millisecond);
|
|
115
|
-
return utcDate;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
var MILLISECONDS_IN_HOUR$1 = 3600000;
|
|
119
|
-
var MILLISECONDS_IN_MINUTE$1 = 60000;
|
|
120
|
-
var patterns$1 = {
|
|
121
|
-
timezone: /([Z+-].*)$/,
|
|
122
|
-
timezoneZ: /^(Z)$/,
|
|
123
|
-
timezoneHH: /^([+-]\d{2})$/,
|
|
124
|
-
timezoneHHMM: /^([+-])(\d{2}):?(\d{2})$/
|
|
125
|
-
};
|
|
126
|
-
// Parse constious time zone offset formats to an offset in milliseconds
|
|
127
|
-
function tzParseTimezone(timezoneString, date, isUtcDate) {
|
|
128
|
-
// Empty string
|
|
129
|
-
if (!timezoneString) {
|
|
130
|
-
return 0;
|
|
131
|
-
}
|
|
132
|
-
// Z
|
|
133
|
-
var token = patterns$1.timezoneZ.exec(timezoneString);
|
|
134
|
-
if (token) {
|
|
135
|
-
return 0;
|
|
136
|
-
}
|
|
137
|
-
var hours;
|
|
138
|
-
var absoluteOffset;
|
|
139
|
-
// ±hh
|
|
140
|
-
token = patterns$1.timezoneHH.exec(timezoneString);
|
|
141
|
-
if (token) {
|
|
142
|
-
hours = parseInt(token[1], 10);
|
|
143
|
-
if (!validateTimezone(hours)) {
|
|
144
|
-
return NaN;
|
|
145
|
-
}
|
|
146
|
-
return -(hours * MILLISECONDS_IN_HOUR$1);
|
|
147
|
-
}
|
|
148
|
-
// ±hh:mm or ±hhmm
|
|
149
|
-
token = patterns$1.timezoneHHMM.exec(timezoneString);
|
|
150
|
-
if (token) {
|
|
151
|
-
hours = parseInt(token[2], 10);
|
|
152
|
-
var minutes = parseInt(token[3], 10);
|
|
153
|
-
if (!validateTimezone(hours, minutes)) {
|
|
154
|
-
return NaN;
|
|
155
|
-
}
|
|
156
|
-
absoluteOffset = Math.abs(hours) * MILLISECONDS_IN_HOUR$1 + minutes * MILLISECONDS_IN_MINUTE$1;
|
|
157
|
-
return token[1] === '+' ? -absoluteOffset : absoluteOffset;
|
|
158
|
-
}
|
|
159
|
-
// IANA time zone
|
|
160
|
-
if (isValidTimezoneIANAString(timezoneString)) {
|
|
161
|
-
date = new Date(date || Date.now());
|
|
162
|
-
var utcDate = isUtcDate ? date : toUtcDate(date);
|
|
163
|
-
var offset = calcOffset(utcDate, timezoneString);
|
|
164
|
-
var fixedOffset = isUtcDate ? offset : fixOffset(date, offset, timezoneString);
|
|
165
|
-
return -fixedOffset;
|
|
166
|
-
}
|
|
167
|
-
return NaN;
|
|
168
|
-
}
|
|
169
|
-
function toUtcDate(date) {
|
|
170
|
-
return newDateUTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
|
|
171
|
-
}
|
|
172
|
-
function calcOffset(date, timezoneString) {
|
|
173
|
-
var tokens = tzTokenizeDate(date, timezoneString);
|
|
174
|
-
// ms dropped because it's not provided by tzTokenizeDate
|
|
175
|
-
var asUTC = newDateUTC(tokens[0], tokens[1] - 1, tokens[2], tokens[3] % 24, tokens[4], tokens[5], 0).getTime();
|
|
176
|
-
var asTS = date.getTime();
|
|
177
|
-
var over = asTS % 1000;
|
|
178
|
-
asTS -= over >= 0 ? over : 1000 + over;
|
|
179
|
-
return asUTC - asTS;
|
|
180
|
-
}
|
|
181
|
-
function fixOffset(date, offset, timezoneString) {
|
|
182
|
-
var localTS = date.getTime();
|
|
183
|
-
// Our UTC time is just a guess because our offset is just a guess
|
|
184
|
-
var utcGuess = localTS - offset;
|
|
185
|
-
// Test whether the zone matches the offset for this ts
|
|
186
|
-
var o2 = calcOffset(new Date(utcGuess), timezoneString);
|
|
187
|
-
// If so, offset didn't change, and we're done
|
|
188
|
-
if (offset === o2) {
|
|
189
|
-
return offset;
|
|
190
|
-
}
|
|
191
|
-
// If not, change the ts by the difference in the offset
|
|
192
|
-
utcGuess -= o2 - offset;
|
|
193
|
-
// If that gives us the local time we want, we're done
|
|
194
|
-
var o3 = calcOffset(new Date(utcGuess), timezoneString);
|
|
195
|
-
if (o2 === o3) {
|
|
196
|
-
return o2;
|
|
197
|
-
}
|
|
198
|
-
// If it's different, we're in a hole time. The offset has changed, but we don't adjust the time
|
|
199
|
-
return Math.max(o2, o3);
|
|
200
|
-
}
|
|
201
|
-
function validateTimezone(hours, minutes) {
|
|
202
|
-
return -23 <= hours && hours <= 23 && (minutes == null || 0 <= minutes && minutes <= 59);
|
|
203
|
-
}
|
|
204
|
-
var validIANATimezoneCache = {};
|
|
205
|
-
function isValidTimezoneIANAString(timeZoneString) {
|
|
206
|
-
if (validIANATimezoneCache[timeZoneString]) return true;
|
|
207
|
-
try {
|
|
208
|
-
new Intl.DateTimeFormat(undefined, {
|
|
209
|
-
timeZone: timeZoneString
|
|
210
|
-
});
|
|
211
|
-
validIANATimezoneCache[timeZoneString] = true;
|
|
212
|
-
return true;
|
|
213
|
-
} catch (error) {
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Google Chrome as of 67.0.3396.87 introduced timezones with offset that includes seconds.
|
|
220
|
-
* They usually appear for dates that denote time before the timezones were introduced
|
|
221
|
-
* (e.g. for 'Europe/Prague' timezone the offset is GMT+00:57:44 before 1 October 1891
|
|
222
|
-
* and GMT+01:00:00 after that date)
|
|
223
|
-
*
|
|
224
|
-
* Date#getTimezoneOffset returns the offset in minutes and would return 57 for the example above,
|
|
225
|
-
* which would lead to incorrect calculations.
|
|
226
|
-
*
|
|
227
|
-
* This function returns the timezone offset in milliseconds that takes seconds in account.
|
|
228
|
-
*/ function getTimezoneOffsetInMilliseconds(date) {
|
|
229
|
-
var utcDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()));
|
|
230
|
-
utcDate.setUTCFullYear(date.getFullYear());
|
|
231
|
-
return +date - +utcDate;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/** Regex to identify the presence of a time zone specifier in a date string */ var tzPattern = /(Z|[+-]\d{2}(?::?\d{2})?| UTC| [a-zA-Z]+\/[a-zA-Z_]+(?:\/[a-zA-Z_]+)?)$/;
|
|
235
|
-
|
|
236
|
-
function _instanceof(left, right) {
|
|
237
|
-
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
238
|
-
return !!right[Symbol.hasInstance](left);
|
|
239
|
-
} else {
|
|
240
|
-
return left instanceof right;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
var MILLISECONDS_IN_HOUR = 3600000;
|
|
244
|
-
var MILLISECONDS_IN_MINUTE = 60000;
|
|
245
|
-
var DEFAULT_ADDITIONAL_DIGITS = 2;
|
|
246
|
-
var patterns = {
|
|
247
|
-
dateTimePattern: /^([0-9W+-]+)(T| )(.*)/,
|
|
248
|
-
datePattern: /^([0-9W+-]+)(.*)/,
|
|
249
|
-
plainTime: /:/,
|
|
250
|
-
// year tokens
|
|
251
|
-
YY: /^(\d{2})$/,
|
|
252
|
-
YYY: [
|
|
253
|
-
/^([+-]\d{2})$/,
|
|
254
|
-
/^([+-]\d{3})$/,
|
|
255
|
-
/^([+-]\d{4})$/
|
|
256
|
-
],
|
|
257
|
-
YYYY: /^(\d{4})/,
|
|
258
|
-
YYYYY: [
|
|
259
|
-
/^([+-]\d{4})/,
|
|
260
|
-
/^([+-]\d{5})/,
|
|
261
|
-
/^([+-]\d{6})/
|
|
262
|
-
],
|
|
263
|
-
// date tokens
|
|
264
|
-
MM: /^-(\d{2})$/,
|
|
265
|
-
DDD: /^-?(\d{3})$/,
|
|
266
|
-
MMDD: /^-?(\d{2})-?(\d{2})$/,
|
|
267
|
-
Www: /^-?W(\d{2})$/,
|
|
268
|
-
WwwD: /^-?W(\d{2})-?(\d{1})$/,
|
|
269
|
-
HH: /^(\d{2}([.,]\d*)?)$/,
|
|
270
|
-
HHMM: /^(\d{2}):?(\d{2}([.,]\d*)?)$/,
|
|
271
|
-
HHMMSS: /^(\d{2}):?(\d{2}):?(\d{2}([.,]\d*)?)$/,
|
|
272
|
-
// time zone tokens (to identify the presence of a tz)
|
|
273
|
-
timeZone: tzPattern
|
|
274
|
-
};
|
|
275
|
-
/**
|
|
276
|
-
* @name toDate
|
|
277
|
-
* @category Common Helpers
|
|
278
|
-
* @summary Convert the given argument to an instance of Date.
|
|
279
|
-
*
|
|
280
|
-
* @description
|
|
281
|
-
* Convert the given argument to an instance of Date.
|
|
282
|
-
*
|
|
283
|
-
* If the argument is an instance of Date, the function returns its clone.
|
|
284
|
-
*
|
|
285
|
-
* If the argument is a number, it is treated as a timestamp.
|
|
286
|
-
*
|
|
287
|
-
* If an argument is a string, the function tries to parse it.
|
|
288
|
-
* Function accepts complete ISO 8601 formats as well as partial implementations.
|
|
289
|
-
* ISO 8601: http://en.wikipedia.org/wiki/ISO_8601
|
|
290
|
-
* If the function cannot parse the string or the values are invalid, it returns Invalid Date.
|
|
291
|
-
*
|
|
292
|
-
* If the argument is none of the above, the function returns Invalid Date.
|
|
293
|
-
*
|
|
294
|
-
* **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`.
|
|
295
|
-
* All *date-fns* functions will throw `RangeError` if `options.additionalDigits` is not 0, 1, 2 or undefined.
|
|
296
|
-
*
|
|
297
|
-
* @param argument the value to convert
|
|
298
|
-
* @param options the object with options. See [Options]{@link https://date-fns.org/docs/Options}
|
|
299
|
-
* @param {0|1|2} [options.additionalDigits=2] - the additional number of digits in the extended year format
|
|
300
|
-
* @param {string} [options.timeZone=''] - used to specify the IANA time zone offset of a date String.
|
|
301
|
-
*
|
|
302
|
-
* @returns the parsed date in the local time zone
|
|
303
|
-
* @throws {TypeError} 1 argument required
|
|
304
|
-
* @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2
|
|
305
|
-
*
|
|
306
|
-
* @example
|
|
307
|
-
* // Convert string '2014-02-11T11:30:30' to date:
|
|
308
|
-
* const result = toDate('2014-02-11T11:30:30')
|
|
309
|
-
* //=> Tue Feb 11 2014 11:30:30
|
|
310
|
-
*
|
|
311
|
-
* @example
|
|
312
|
-
* // Convert string '+02014101' to date,
|
|
313
|
-
* // if the additional number of digits in the extended year format is 1:
|
|
314
|
-
* const result = toDate('+02014101', {additionalDigits: 1})
|
|
315
|
-
* //=> Fri Apr 11 2014 00:00:00
|
|
316
|
-
*/ function toDate(argument) {
|
|
317
|
-
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
318
|
-
if (arguments.length < 1) {
|
|
319
|
-
throw new TypeError('1 argument required, but only ' + arguments.length + ' present');
|
|
320
|
-
}
|
|
321
|
-
if (argument === null) {
|
|
322
|
-
return new Date(NaN);
|
|
323
|
-
}
|
|
324
|
-
var additionalDigits = options.additionalDigits == null ? DEFAULT_ADDITIONAL_DIGITS : Number(options.additionalDigits);
|
|
325
|
-
if (additionalDigits !== 2 && additionalDigits !== 1 && additionalDigits !== 0) {
|
|
326
|
-
throw new RangeError('additionalDigits must be 0, 1 or 2');
|
|
327
|
-
}
|
|
328
|
-
// Clone the date
|
|
329
|
-
if (_instanceof(argument, Date) || typeof argument === 'object' && Object.prototype.toString.call(argument) === '[object Date]') {
|
|
330
|
-
// Prevent the date to lose the milliseconds when passed to new Date() in IE10
|
|
331
|
-
return new Date(argument.getTime());
|
|
332
|
-
} else if (typeof argument === 'number' || Object.prototype.toString.call(argument) === '[object Number]') {
|
|
333
|
-
return new Date(argument);
|
|
334
|
-
} else if (!(Object.prototype.toString.call(argument) === '[object String]')) {
|
|
335
|
-
return new Date(NaN);
|
|
336
|
-
}
|
|
337
|
-
var dateStrings = splitDateString(argument);
|
|
338
|
-
var _parseYear = parseYear(dateStrings.date, additionalDigits), year = _parseYear.year, restDateString = _parseYear.restDateString;
|
|
339
|
-
var date = parseDate(restDateString, year);
|
|
340
|
-
if (date === null || isNaN(date.getTime())) {
|
|
341
|
-
return new Date(NaN);
|
|
342
|
-
}
|
|
343
|
-
if (date) {
|
|
344
|
-
var timestamp = date.getTime();
|
|
345
|
-
var time = 0;
|
|
346
|
-
var offset;
|
|
347
|
-
if (dateStrings.time) {
|
|
348
|
-
time = parseTime(dateStrings.time);
|
|
349
|
-
if (time === null || isNaN(time)) {
|
|
350
|
-
return new Date(NaN);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
if (dateStrings.timeZone || options.timeZone) {
|
|
354
|
-
offset = tzParseTimezone(dateStrings.timeZone || options.timeZone, new Date(timestamp + time));
|
|
355
|
-
if (isNaN(offset)) {
|
|
356
|
-
return new Date(NaN);
|
|
357
|
-
}
|
|
358
|
-
} else {
|
|
359
|
-
// get offset accurate to hour in time zones that change offset
|
|
360
|
-
offset = getTimezoneOffsetInMilliseconds(new Date(timestamp + time));
|
|
361
|
-
offset = getTimezoneOffsetInMilliseconds(new Date(timestamp + time + offset));
|
|
362
|
-
}
|
|
363
|
-
return new Date(timestamp + time + offset);
|
|
364
|
-
} else {
|
|
365
|
-
return new Date(NaN);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
function splitDateString(dateString) {
|
|
369
|
-
var dateStrings = {};
|
|
370
|
-
var parts = patterns.dateTimePattern.exec(dateString);
|
|
371
|
-
var timeString;
|
|
372
|
-
if (!parts) {
|
|
373
|
-
parts = patterns.datePattern.exec(dateString);
|
|
374
|
-
if (parts) {
|
|
375
|
-
dateStrings.date = parts[1];
|
|
376
|
-
timeString = parts[2];
|
|
377
|
-
} else {
|
|
378
|
-
dateStrings.date = null;
|
|
379
|
-
timeString = dateString;
|
|
380
|
-
}
|
|
381
|
-
} else {
|
|
382
|
-
dateStrings.date = parts[1];
|
|
383
|
-
timeString = parts[3];
|
|
384
|
-
}
|
|
385
|
-
if (timeString) {
|
|
386
|
-
var token = patterns.timeZone.exec(timeString);
|
|
387
|
-
if (token) {
|
|
388
|
-
dateStrings.time = timeString.replace(token[1], '');
|
|
389
|
-
dateStrings.timeZone = token[1].trim();
|
|
390
|
-
} else {
|
|
391
|
-
dateStrings.time = timeString;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
return dateStrings;
|
|
395
|
-
}
|
|
396
|
-
function parseYear(dateString, additionalDigits) {
|
|
397
|
-
if (dateString) {
|
|
398
|
-
var patternYYY = patterns.YYY[additionalDigits];
|
|
399
|
-
var patternYYYYY = patterns.YYYYY[additionalDigits];
|
|
400
|
-
// YYYY or ±YYYYY
|
|
401
|
-
var token = patterns.YYYY.exec(dateString) || patternYYYYY.exec(dateString);
|
|
402
|
-
if (token) {
|
|
403
|
-
var yearString = token[1];
|
|
404
|
-
return {
|
|
405
|
-
year: parseInt(yearString, 10),
|
|
406
|
-
restDateString: dateString.slice(yearString.length)
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
// YY or ±YYY
|
|
410
|
-
token = patterns.YY.exec(dateString) || patternYYY.exec(dateString);
|
|
411
|
-
if (token) {
|
|
412
|
-
var centuryString = token[1];
|
|
413
|
-
return {
|
|
414
|
-
year: parseInt(centuryString, 10) * 100,
|
|
415
|
-
restDateString: dateString.slice(centuryString.length)
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
// Invalid ISO-formatted year
|
|
420
|
-
return {
|
|
421
|
-
year: null
|
|
422
|
-
};
|
|
423
|
-
}
|
|
424
|
-
function parseDate(dateString, year) {
|
|
425
|
-
// Invalid ISO-formatted year
|
|
426
|
-
if (year === null) {
|
|
427
|
-
return null;
|
|
428
|
-
}
|
|
429
|
-
var date;
|
|
430
|
-
var month;
|
|
431
|
-
var week;
|
|
432
|
-
// YYYY
|
|
433
|
-
if (!dateString || !dateString.length) {
|
|
434
|
-
date = new Date(0);
|
|
435
|
-
date.setUTCFullYear(year);
|
|
436
|
-
return date;
|
|
437
|
-
}
|
|
438
|
-
// YYYY-MM
|
|
439
|
-
var token = patterns.MM.exec(dateString);
|
|
440
|
-
if (token) {
|
|
441
|
-
date = new Date(0);
|
|
442
|
-
month = parseInt(token[1], 10) - 1;
|
|
443
|
-
if (!validateDate(year, month)) {
|
|
444
|
-
return new Date(NaN);
|
|
445
|
-
}
|
|
446
|
-
date.setUTCFullYear(year, month);
|
|
447
|
-
return date;
|
|
448
|
-
}
|
|
449
|
-
// YYYY-DDD or YYYYDDD
|
|
450
|
-
token = patterns.DDD.exec(dateString);
|
|
451
|
-
if (token) {
|
|
452
|
-
date = new Date(0);
|
|
453
|
-
var dayOfYear = parseInt(token[1], 10);
|
|
454
|
-
if (!validateDayOfYearDate(year, dayOfYear)) {
|
|
455
|
-
return new Date(NaN);
|
|
456
|
-
}
|
|
457
|
-
date.setUTCFullYear(year, 0, dayOfYear);
|
|
458
|
-
return date;
|
|
459
|
-
}
|
|
460
|
-
// yyyy-MM-dd or YYYYMMDD
|
|
461
|
-
token = patterns.MMDD.exec(dateString);
|
|
462
|
-
if (token) {
|
|
463
|
-
date = new Date(0);
|
|
464
|
-
month = parseInt(token[1], 10) - 1;
|
|
465
|
-
var day = parseInt(token[2], 10);
|
|
466
|
-
if (!validateDate(year, month, day)) {
|
|
467
|
-
return new Date(NaN);
|
|
468
|
-
}
|
|
469
|
-
date.setUTCFullYear(year, month, day);
|
|
470
|
-
return date;
|
|
471
|
-
}
|
|
472
|
-
// YYYY-Www or YYYYWww
|
|
473
|
-
token = patterns.Www.exec(dateString);
|
|
474
|
-
if (token) {
|
|
475
|
-
week = parseInt(token[1], 10) - 1;
|
|
476
|
-
if (!validateWeekDate(week)) {
|
|
477
|
-
return new Date(NaN);
|
|
478
|
-
}
|
|
479
|
-
return dayOfISOWeekYear(year, week);
|
|
480
|
-
}
|
|
481
|
-
// YYYY-Www-D or YYYYWwwD
|
|
482
|
-
token = patterns.WwwD.exec(dateString);
|
|
483
|
-
if (token) {
|
|
484
|
-
week = parseInt(token[1], 10) - 1;
|
|
485
|
-
var dayOfWeek = parseInt(token[2], 10) - 1;
|
|
486
|
-
if (!validateWeekDate(week, dayOfWeek)) {
|
|
487
|
-
return new Date(NaN);
|
|
488
|
-
}
|
|
489
|
-
return dayOfISOWeekYear(year, week, dayOfWeek);
|
|
490
|
-
}
|
|
491
|
-
// Invalid ISO-formatted date
|
|
492
|
-
return null;
|
|
493
|
-
}
|
|
494
|
-
function parseTime(timeString) {
|
|
495
|
-
var hours;
|
|
496
|
-
var minutes;
|
|
497
|
-
// hh
|
|
498
|
-
var token = patterns.HH.exec(timeString);
|
|
499
|
-
if (token) {
|
|
500
|
-
hours = parseFloat(token[1].replace(',', '.'));
|
|
501
|
-
if (!validateTime(hours)) {
|
|
502
|
-
return NaN;
|
|
503
|
-
}
|
|
504
|
-
return hours % 24 * MILLISECONDS_IN_HOUR;
|
|
505
|
-
}
|
|
506
|
-
// hh:mm or hhmm
|
|
507
|
-
token = patterns.HHMM.exec(timeString);
|
|
508
|
-
if (token) {
|
|
509
|
-
hours = parseInt(token[1], 10);
|
|
510
|
-
minutes = parseFloat(token[2].replace(',', '.'));
|
|
511
|
-
if (!validateTime(hours, minutes)) {
|
|
512
|
-
return NaN;
|
|
513
|
-
}
|
|
514
|
-
return hours % 24 * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE;
|
|
515
|
-
}
|
|
516
|
-
// hh:mm:ss or hhmmss
|
|
517
|
-
token = patterns.HHMMSS.exec(timeString);
|
|
518
|
-
if (token) {
|
|
519
|
-
hours = parseInt(token[1], 10);
|
|
520
|
-
minutes = parseInt(token[2], 10);
|
|
521
|
-
var seconds = parseFloat(token[3].replace(',', '.'));
|
|
522
|
-
if (!validateTime(hours, minutes, seconds)) {
|
|
523
|
-
return NaN;
|
|
524
|
-
}
|
|
525
|
-
return hours % 24 * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE + seconds * 1000;
|
|
526
|
-
}
|
|
527
|
-
// Invalid ISO-formatted time
|
|
528
|
-
return null;
|
|
529
|
-
}
|
|
530
|
-
function dayOfISOWeekYear(isoWeekYear, week, day) {
|
|
531
|
-
week = week || 0;
|
|
532
|
-
day = day || 0;
|
|
533
|
-
var date = new Date(0);
|
|
534
|
-
date.setUTCFullYear(isoWeekYear, 0, 4);
|
|
535
|
-
var fourthOfJanuaryDay = date.getUTCDay() || 7;
|
|
536
|
-
var diff = week * 7 + day + 1 - fourthOfJanuaryDay;
|
|
537
|
-
date.setUTCDate(date.getUTCDate() + diff);
|
|
538
|
-
return date;
|
|
539
|
-
}
|
|
540
|
-
// Validation functions
|
|
541
|
-
var DAYS_IN_MONTH = [
|
|
542
|
-
31,
|
|
543
|
-
28,
|
|
544
|
-
31,
|
|
545
|
-
30,
|
|
546
|
-
31,
|
|
547
|
-
30,
|
|
548
|
-
31,
|
|
549
|
-
31,
|
|
550
|
-
30,
|
|
551
|
-
31,
|
|
552
|
-
30,
|
|
553
|
-
31
|
|
554
|
-
];
|
|
555
|
-
var DAYS_IN_MONTH_LEAP_YEAR = [
|
|
556
|
-
31,
|
|
557
|
-
29,
|
|
558
|
-
31,
|
|
559
|
-
30,
|
|
560
|
-
31,
|
|
561
|
-
30,
|
|
562
|
-
31,
|
|
563
|
-
31,
|
|
564
|
-
30,
|
|
565
|
-
31,
|
|
566
|
-
30,
|
|
567
|
-
31
|
|
568
|
-
];
|
|
569
|
-
function isLeapYearIndex(year) {
|
|
570
|
-
return year % 400 === 0 || year % 4 === 0 && year % 100 !== 0;
|
|
571
|
-
}
|
|
572
|
-
function validateDate(year, month, date) {
|
|
573
|
-
if (month < 0 || month > 11) {
|
|
574
|
-
return false;
|
|
575
|
-
}
|
|
576
|
-
if (date != null) {
|
|
577
|
-
if (date < 1) {
|
|
578
|
-
return false;
|
|
579
|
-
}
|
|
580
|
-
var isLeapYear = isLeapYearIndex(year);
|
|
581
|
-
if (isLeapYear && date > DAYS_IN_MONTH_LEAP_YEAR[month]) {
|
|
582
|
-
return false;
|
|
583
|
-
}
|
|
584
|
-
if (!isLeapYear && date > DAYS_IN_MONTH[month]) {
|
|
585
|
-
return false;
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
return true;
|
|
589
|
-
}
|
|
590
|
-
function validateDayOfYearDate(year, dayOfYear) {
|
|
591
|
-
if (dayOfYear < 1) {
|
|
592
|
-
return false;
|
|
593
|
-
}
|
|
594
|
-
var isLeapYear = isLeapYearIndex(year);
|
|
595
|
-
if (isLeapYear && dayOfYear > 366) {
|
|
596
|
-
return false;
|
|
597
|
-
}
|
|
598
|
-
if (!isLeapYear && dayOfYear > 365) {
|
|
599
|
-
return false;
|
|
600
|
-
}
|
|
601
|
-
return true;
|
|
602
|
-
}
|
|
603
|
-
function validateWeekDate(week, day) {
|
|
604
|
-
if (week < 0 || week > 52) {
|
|
605
|
-
return false;
|
|
606
|
-
}
|
|
607
|
-
if (day != null && (day < 0 || day > 6)) {
|
|
608
|
-
return false;
|
|
609
|
-
}
|
|
610
|
-
return true;
|
|
611
|
-
}
|
|
612
|
-
function validateTime(hours, minutes, seconds) {
|
|
613
|
-
if (hours < 0 || hours >= 25) {
|
|
614
|
-
return false;
|
|
615
|
-
}
|
|
616
|
-
if (minutes != null && (minutes < 0 || minutes >= 60)) {
|
|
617
|
-
return false;
|
|
618
|
-
}
|
|
619
|
-
if (seconds != null && (seconds < 0 || seconds >= 60)) {
|
|
620
|
-
return false;
|
|
621
|
-
}
|
|
622
|
-
return true;
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
/**
|
|
626
|
-
* @name toZonedTime
|
|
627
|
-
* @category Time Zone Helpers
|
|
628
|
-
* @summary Get a date/time representing local time in a given time zone from the UTC date
|
|
629
|
-
*
|
|
630
|
-
* @description
|
|
631
|
-
* Returns a date instance with values representing the local time in the time zone
|
|
632
|
-
* specified of the UTC time from the date provided. In other words, when the new date
|
|
633
|
-
* is formatted it will show the equivalent hours in the target time zone regardless
|
|
634
|
-
* of the current system time zone.
|
|
635
|
-
*
|
|
636
|
-
* @param date the date with the relevant UTC time
|
|
637
|
-
* @param timeZone the time zone to get local time for, can be an offset or IANA time zone
|
|
638
|
-
* @param options the object with options. See [Options]{@link https://date-fns.org/docs/Options}
|
|
639
|
-
* @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate}
|
|
640
|
-
*
|
|
641
|
-
* @throws {TypeError} 2 arguments required
|
|
642
|
-
* @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2
|
|
643
|
-
*
|
|
644
|
-
* @example
|
|
645
|
-
* // In June 10am UTC is 6am in New York (-04:00)
|
|
646
|
-
* const result = toZonedTime('2014-06-25T10:00:00.000Z', 'America/New_York')
|
|
647
|
-
* //=> Jun 25 2014 06:00:00
|
|
648
|
-
*/ function toZonedTime(date, timeZone, options) {
|
|
649
|
-
date = toDate(date, options);
|
|
650
|
-
var offsetMilliseconds = tzParseTimezone(timeZone, date, true);
|
|
651
|
-
var d = new Date(date.getTime() - offsetMilliseconds);
|
|
652
|
-
var resultDate = new Date(0);
|
|
653
|
-
resultDate.setFullYear(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
|
|
654
|
-
resultDate.setHours(d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds());
|
|
655
|
-
return resultDate;
|
|
656
|
-
}
|
|
657
|
-
|
|
658
8
|
/**
|
|
659
9
|
* It returns a `Date` object from a date `string` adjusted on the user timeZone,
|
|
660
10
|
* if a timeZone is not provided we try getting it from the `Intl` browwser native
|
|
@@ -670,7 +20,7 @@ function validateTime(hours, minutes, seconds) {
|
|
|
670
20
|
* @param timeZone Optionally pass a timeZone (e.g. from user preference or from the server), it falls back trying to read it from the `Intl` browwser native API.
|
|
671
21
|
*/let getZonedDate=(o="",n)=>{if(o.endsWith("Z")||(o+="Z"),!n&&utils.isBrowser)try{n=Intl.DateTimeFormat().resolvedOptions().timeZone;}catch(e){"development"===process.env.NODE_ENV&&console.warn("[@koine/browser:getZonedDate] failed reading timeZone, error",e);}// no need to do anything here, it just means `Intl` failed, probably
|
|
672
22
|
// because the browser does not support it
|
|
673
|
-
return n?toZonedTime(new Date(o),n):new Date(o)};
|
|
23
|
+
return n?dateFnsTz.toZonedTime(new Date(o),n):new Date(o)};
|
|
674
24
|
|
|
675
25
|
exports.default = getZonedDate;
|
|
676
26
|
exports.getZonedDate = getZonedDate;
|