@wordpress/date 5.29.1-next.f34ab90e9.0 → 5.30.1-next.836ecdcae.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 +6 -0
- package/README.md +18 -18
- package/build/index.js +94 -120
- package/build/index.js.map +1 -1
- package/build/types.js +6 -0
- package/build/types.js.map +1 -0
- package/build-module/index.js +71 -121
- package/build-module/index.js.map +1 -1
- package/build-module/types.js +2 -0
- package/build-module/types.js.map +1 -0
- package/build-types/index.d.ts +62 -155
- package/build-types/index.d.ts.map +1 -1
- package/build-types/types.d.ts +116 -0
- package/build-types/types.d.ts.map +1 -0
- package/package.json +3 -3
- package/src/{index.js → index.ts} +115 -141
- package/src/types.ts +138 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
+
import type { Moment } from 'moment';
|
|
4
5
|
import momentLib from 'moment';
|
|
5
6
|
import 'moment-timezone/moment-timezone';
|
|
6
7
|
import 'moment-timezone/moment-timezone-utils';
|
|
@@ -9,54 +10,12 @@ import 'moment-timezone/moment-timezone-utils';
|
|
|
9
10
|
* WordPress dependencies
|
|
10
11
|
*/
|
|
11
12
|
import deprecated from '@wordpress/deprecated';
|
|
12
|
-
|
|
13
|
-
/** @typedef {import('moment').Moment} Moment */
|
|
14
|
-
/** @typedef {import('moment').LocaleSpecification} MomentLocaleSpecification */
|
|
15
|
-
|
|
16
13
|
/**
|
|
17
|
-
*
|
|
18
|
-
* @property {string} am Lowercase AM.
|
|
19
|
-
* @property {string} AM Uppercase AM.
|
|
20
|
-
* @property {string} pm Lowercase PM.
|
|
21
|
-
* @property {string} PM Uppercase PM.
|
|
14
|
+
* Internal dependencies
|
|
22
15
|
*/
|
|
16
|
+
import type { DateSettings } from './types';
|
|
23
17
|
|
|
24
|
-
|
|
25
|
-
* @typedef FormatsConfig
|
|
26
|
-
* @property {string} time Time format.
|
|
27
|
-
* @property {string} date Date format.
|
|
28
|
-
* @property {string} datetime Datetime format.
|
|
29
|
-
* @property {string} datetimeAbbreviated Abbreviated datetime format.
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @typedef TimezoneConfig
|
|
34
|
-
* @property {string} offset Offset setting.
|
|
35
|
-
* @property {string} offsetFormatted Offset setting with decimals formatted to minutes.
|
|
36
|
-
* @property {string} string The timezone as a string (e.g., `'America/Los_Angeles'`).
|
|
37
|
-
* @property {string} abbr Abbreviation for the timezone.
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
/* eslint-disable jsdoc/valid-types */
|
|
41
|
-
/**
|
|
42
|
-
* @typedef L10nSettings
|
|
43
|
-
* @property {string} locale Moment locale.
|
|
44
|
-
* @property {MomentLocaleSpecification['months']} months Locale months.
|
|
45
|
-
* @property {MomentLocaleSpecification['monthsShort']} monthsShort Locale months short.
|
|
46
|
-
* @property {MomentLocaleSpecification['weekdays']} weekdays Locale weekdays.
|
|
47
|
-
* @property {MomentLocaleSpecification['weekdaysShort']} weekdaysShort Locale weekdays short.
|
|
48
|
-
* @property {MeridiemConfig} meridiem Meridiem config.
|
|
49
|
-
* @property {MomentLocaleSpecification['relativeTime']} relative Relative time config.
|
|
50
|
-
* @property {0|1|2|3|4|5|6} startOfWeek Day that the week starts on.
|
|
51
|
-
*/
|
|
52
|
-
/* eslint-enable jsdoc/valid-types */
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* @typedef DateSettings
|
|
56
|
-
* @property {L10nSettings} l10n Localization settings.
|
|
57
|
-
* @property {FormatsConfig} formats Date/time formats config.
|
|
58
|
-
* @property {TimezoneConfig} timezone Timezone settings.
|
|
59
|
-
*/
|
|
18
|
+
export * from './types';
|
|
60
19
|
|
|
61
20
|
const WP_ZONE = 'WP';
|
|
62
21
|
|
|
@@ -66,8 +25,7 @@ const VALID_UTC_OFFSET = /^[+-][0-1][0-9](:?[0-9][0-9])?$/;
|
|
|
66
25
|
|
|
67
26
|
// Changes made here will likely need to be synced with Core in the file
|
|
68
27
|
// src/wp-includes/script-loader.php in `wp_default_packages_inline_scripts()`.
|
|
69
|
-
|
|
70
|
-
let settings = {
|
|
28
|
+
let settings: DateSettings = {
|
|
71
29
|
l10n: {
|
|
72
30
|
locale: 'en',
|
|
73
31
|
months: [
|
|
@@ -139,9 +97,9 @@ let settings = {
|
|
|
139
97
|
/**
|
|
140
98
|
* Adds a locale to moment, using the format supplied by `wp_localize_script()`.
|
|
141
99
|
*
|
|
142
|
-
* @param
|
|
100
|
+
* @param dateSettings Settings, including locale data.
|
|
143
101
|
*/
|
|
144
|
-
export function setSettings( dateSettings ) {
|
|
102
|
+
export function setSettings( dateSettings: DateSettings ) {
|
|
145
103
|
settings = dateSettings;
|
|
146
104
|
|
|
147
105
|
setupWPTimezone();
|
|
@@ -259,31 +217,25 @@ function setupWPTimezone() {
|
|
|
259
217
|
// Date constants.
|
|
260
218
|
/**
|
|
261
219
|
* Number of seconds in one minute.
|
|
262
|
-
*
|
|
263
|
-
* @type {number}
|
|
264
220
|
*/
|
|
265
221
|
const MINUTE_IN_SECONDS = 60;
|
|
266
222
|
/**
|
|
267
223
|
* Number of minutes in one hour.
|
|
268
|
-
*
|
|
269
|
-
* @type {number}
|
|
270
224
|
*/
|
|
271
225
|
const HOUR_IN_MINUTES = 60;
|
|
272
226
|
/**
|
|
273
227
|
* Number of seconds in one hour.
|
|
274
|
-
*
|
|
275
|
-
* @type {number}
|
|
276
228
|
*/
|
|
277
229
|
const HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS;
|
|
278
230
|
|
|
279
231
|
/**
|
|
280
232
|
* Map of PHP formats to Moment.js formats.
|
|
281
233
|
*
|
|
282
|
-
* These are used internally by {@link
|
|
234
|
+
* These are used internally by {@link format}, and are either
|
|
283
235
|
* a string representing the corresponding Moment.js format code, or a
|
|
284
236
|
* function which returns the formatted string.
|
|
285
237
|
*
|
|
286
|
-
* This should only be used through {@link
|
|
238
|
+
* This should only be used through {@link format}, not
|
|
287
239
|
* directly.
|
|
288
240
|
*/
|
|
289
241
|
const formatMap = {
|
|
@@ -297,11 +249,11 @@ const formatMap = {
|
|
|
297
249
|
/**
|
|
298
250
|
* Gets the ordinal suffix.
|
|
299
251
|
*
|
|
300
|
-
* @param
|
|
252
|
+
* @param momentDate Moment instance.
|
|
301
253
|
*
|
|
302
|
-
* @return
|
|
254
|
+
* @return Formatted date.
|
|
303
255
|
*/
|
|
304
|
-
S( momentDate ) {
|
|
256
|
+
S( momentDate: Moment ) {
|
|
305
257
|
// Do - D.
|
|
306
258
|
const num = momentDate.format( 'D' );
|
|
307
259
|
const withOrdinal = momentDate.format( 'Do' );
|
|
@@ -312,11 +264,11 @@ const formatMap = {
|
|
|
312
264
|
/**
|
|
313
265
|
* Gets the day of the year (zero-indexed).
|
|
314
266
|
*
|
|
315
|
-
* @param
|
|
267
|
+
* @param momentDate Moment instance.
|
|
316
268
|
*
|
|
317
|
-
* @return
|
|
269
|
+
* @return Formatted date.
|
|
318
270
|
*/
|
|
319
|
-
z( momentDate ) {
|
|
271
|
+
z( momentDate: Moment ) {
|
|
320
272
|
// DDD - 1.
|
|
321
273
|
return ( parseInt( momentDate.format( 'DDD' ), 10 ) - 1 ).toString();
|
|
322
274
|
},
|
|
@@ -332,11 +284,11 @@ const formatMap = {
|
|
|
332
284
|
/**
|
|
333
285
|
* Gets the days in the month.
|
|
334
286
|
*
|
|
335
|
-
* @param
|
|
287
|
+
* @param momentDate Moment instance.
|
|
336
288
|
*
|
|
337
|
-
* @return
|
|
289
|
+
* @return Formatted date.
|
|
338
290
|
*/
|
|
339
|
-
t( momentDate ) {
|
|
291
|
+
t( momentDate: Moment ) {
|
|
340
292
|
return momentDate.daysInMonth();
|
|
341
293
|
},
|
|
342
294
|
|
|
@@ -344,11 +296,11 @@ const formatMap = {
|
|
|
344
296
|
/**
|
|
345
297
|
* Gets whether the current year is a leap year.
|
|
346
298
|
*
|
|
347
|
-
* @param
|
|
299
|
+
* @param momentDate Moment instance.
|
|
348
300
|
*
|
|
349
|
-
* @return
|
|
301
|
+
* @return Formatted date.
|
|
350
302
|
*/
|
|
351
|
-
L( momentDate ) {
|
|
303
|
+
L( momentDate: Moment ) {
|
|
352
304
|
return momentDate.isLeapYear() ? '1' : '0';
|
|
353
305
|
},
|
|
354
306
|
o: 'GGGG',
|
|
@@ -361,11 +313,11 @@ const formatMap = {
|
|
|
361
313
|
/**
|
|
362
314
|
* Gets the current time in Swatch Internet Time (.beats).
|
|
363
315
|
*
|
|
364
|
-
* @param
|
|
316
|
+
* @param momentDate Moment instance.
|
|
365
317
|
*
|
|
366
|
-
* @return
|
|
318
|
+
* @return Formatted date.
|
|
367
319
|
*/
|
|
368
|
-
B( momentDate ) {
|
|
320
|
+
B( momentDate: Moment ) {
|
|
369
321
|
const timezoned = momentLib( momentDate ).utcOffset( 60 );
|
|
370
322
|
const seconds = parseInt( timezoned.format( 's' ), 10 ),
|
|
371
323
|
minutes = parseInt( timezoned.format( 'm' ), 10 ),
|
|
@@ -393,11 +345,11 @@ const formatMap = {
|
|
|
393
345
|
/**
|
|
394
346
|
* Gets whether the timezone is in DST currently.
|
|
395
347
|
*
|
|
396
|
-
* @param
|
|
348
|
+
* @param momentDate Moment instance.
|
|
397
349
|
*
|
|
398
|
-
* @return
|
|
350
|
+
* @return Formatted date.
|
|
399
351
|
*/
|
|
400
|
-
I( momentDate ) {
|
|
352
|
+
I( momentDate: Moment ) {
|
|
401
353
|
return momentDate.isDST() ? '1' : '0';
|
|
402
354
|
},
|
|
403
355
|
O: 'ZZ',
|
|
@@ -406,11 +358,11 @@ const formatMap = {
|
|
|
406
358
|
/**
|
|
407
359
|
* Gets the timezone offset in seconds.
|
|
408
360
|
*
|
|
409
|
-
* @param
|
|
361
|
+
* @param momentDate Moment instance.
|
|
410
362
|
*
|
|
411
|
-
* @return
|
|
363
|
+
* @return Formatted date.
|
|
412
364
|
*/
|
|
413
|
-
Z( momentDate ) {
|
|
365
|
+
Z( momentDate: Moment ) {
|
|
414
366
|
// Timezone offset in seconds.
|
|
415
367
|
const offset = momentDate.format( 'Z' );
|
|
416
368
|
const sign = offset[ 0 ] === '-' ? -1 : 1;
|
|
@@ -429,11 +381,11 @@ const formatMap = {
|
|
|
429
381
|
/**
|
|
430
382
|
* Formats the date as RFC2822.
|
|
431
383
|
*
|
|
432
|
-
* @param
|
|
384
|
+
* @param momentDate Moment instance.
|
|
433
385
|
*
|
|
434
|
-
* @return
|
|
386
|
+
* @return Formatted date.
|
|
435
387
|
*/
|
|
436
|
-
r( momentDate ) {
|
|
388
|
+
r( momentDate: Moment ) {
|
|
437
389
|
return momentDate
|
|
438
390
|
.locale( 'en' )
|
|
439
391
|
.format( 'ddd, DD MMM YYYY HH:mm:ss ZZ' );
|
|
@@ -444,14 +396,17 @@ const formatMap = {
|
|
|
444
396
|
/**
|
|
445
397
|
* Formats a date. Does not alter the date's timezone.
|
|
446
398
|
*
|
|
447
|
-
* @param
|
|
448
|
-
*
|
|
449
|
-
* @param
|
|
450
|
-
*
|
|
399
|
+
* @param dateFormat PHP-style formatting string.
|
|
400
|
+
* See [php.net/date](https://www.php.net/manual/en/function.date.php).
|
|
401
|
+
* @param dateValue Date object or string,
|
|
402
|
+
* parsable by moment.js.
|
|
451
403
|
*
|
|
452
|
-
* @return
|
|
404
|
+
* @return Formatted date.
|
|
453
405
|
*/
|
|
454
|
-
export function format(
|
|
406
|
+
export function format(
|
|
407
|
+
dateFormat: string,
|
|
408
|
+
dateValue: Moment | Date | string | number = new Date()
|
|
409
|
+
) {
|
|
455
410
|
let i, char;
|
|
456
411
|
const newFormat = [];
|
|
457
412
|
const momentDate = momentLib( dateValue );
|
|
@@ -465,8 +420,7 @@ export function format( dateFormat, dateValue = new Date() ) {
|
|
|
465
420
|
continue;
|
|
466
421
|
}
|
|
467
422
|
if ( char in formatMap ) {
|
|
468
|
-
const formatter =
|
|
469
|
-
formatMap[ /** @type {keyof formatMap} */ ( char ) ];
|
|
423
|
+
const formatter = formatMap[ char as keyof typeof formatMap ];
|
|
470
424
|
if ( typeof formatter !== 'string' ) {
|
|
471
425
|
// If the format is a function, call it.
|
|
472
426
|
newFormat.push( '[' + formatter( momentDate ) + ']' );
|
|
@@ -486,20 +440,24 @@ export function format( dateFormat, dateValue = new Date() ) {
|
|
|
486
440
|
/**
|
|
487
441
|
* Formats a date (like `date()` in PHP).
|
|
488
442
|
*
|
|
489
|
-
* @param
|
|
490
|
-
*
|
|
491
|
-
* @param
|
|
492
|
-
*
|
|
493
|
-
* @param
|
|
494
|
-
*
|
|
495
|
-
*
|
|
443
|
+
* @param dateFormat PHP-style formatting string.
|
|
444
|
+
* See [php.net/date](https://www.php.net/manual/en/function.date.php).
|
|
445
|
+
* @param dateValue Date object or string, parsable
|
|
446
|
+
* by moment.js.
|
|
447
|
+
* @param timezone Timezone to output result in or a
|
|
448
|
+
* UTC offset. Defaults to timezone from
|
|
449
|
+
* site.
|
|
496
450
|
*
|
|
497
451
|
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
498
452
|
* @see https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
|
|
499
453
|
*
|
|
500
454
|
* @return {string} Formatted date in English.
|
|
501
455
|
*/
|
|
502
|
-
export function date(
|
|
456
|
+
export function date(
|
|
457
|
+
dateFormat: string,
|
|
458
|
+
dateValue: Moment | Date | string | number = new Date(),
|
|
459
|
+
timezone?: string
|
|
460
|
+
) {
|
|
503
461
|
const dateMoment = buildMoment( dateValue, timezone );
|
|
504
462
|
return format( dateFormat, dateMoment );
|
|
505
463
|
}
|
|
@@ -507,14 +465,17 @@ export function date( dateFormat, dateValue = new Date(), timezone ) {
|
|
|
507
465
|
/**
|
|
508
466
|
* Formats a date (like `date()` in PHP), in the UTC timezone.
|
|
509
467
|
*
|
|
510
|
-
* @param
|
|
511
|
-
*
|
|
512
|
-
* @param
|
|
513
|
-
*
|
|
468
|
+
* @param dateFormat PHP-style formatting string.
|
|
469
|
+
* See [php.net/date](https://www.php.net/manual/en/function.date.php).
|
|
470
|
+
* @param dateValue Date object or string,
|
|
471
|
+
* parsable by moment.js.
|
|
514
472
|
*
|
|
515
|
-
* @return
|
|
473
|
+
* @return Formatted date in English.
|
|
516
474
|
*/
|
|
517
|
-
export function gmdate(
|
|
475
|
+
export function gmdate(
|
|
476
|
+
dateFormat: string,
|
|
477
|
+
dateValue: Moment | Date | string | number = new Date()
|
|
478
|
+
) {
|
|
518
479
|
const dateMoment = momentLib( dateValue ).utc();
|
|
519
480
|
return format( dateFormat, dateMoment );
|
|
520
481
|
}
|
|
@@ -525,22 +486,26 @@ export function gmdate( dateFormat, dateValue = new Date() ) {
|
|
|
525
486
|
* Backward Compatibility Notice: if `timezone` is set to `true`, the function
|
|
526
487
|
* behaves like `gmdateI18n`.
|
|
527
488
|
*
|
|
528
|
-
* @param
|
|
529
|
-
*
|
|
530
|
-
* @param
|
|
531
|
-
*
|
|
532
|
-
* @param
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
*
|
|
489
|
+
* @param dateFormat PHP-style formatting string.
|
|
490
|
+
* See [php.net/date](https://www.php.net/manual/en/function.date.php).
|
|
491
|
+
* @param dateValue Date object or string, parsable by
|
|
492
|
+
* moment.js.
|
|
493
|
+
* @param timezone Timezone to output result in or a
|
|
494
|
+
* UTC offset. Defaults to timezone from
|
|
495
|
+
* site. Notice: `boolean` is effectively
|
|
496
|
+
* deprecated, but still supported for
|
|
497
|
+
* backward compatibility reasons.
|
|
537
498
|
*
|
|
538
499
|
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
539
500
|
* @see https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
|
|
540
501
|
*
|
|
541
|
-
* @return
|
|
502
|
+
* @return Formatted date.
|
|
542
503
|
*/
|
|
543
|
-
export function dateI18n(
|
|
504
|
+
export function dateI18n(
|
|
505
|
+
dateFormat: string,
|
|
506
|
+
dateValue: Moment | Date | string | number = new Date(),
|
|
507
|
+
timezone?: string | number | boolean
|
|
508
|
+
) {
|
|
544
509
|
if ( true === timezone ) {
|
|
545
510
|
return gmdateI18n( dateFormat, dateValue );
|
|
546
511
|
}
|
|
@@ -558,14 +523,17 @@ export function dateI18n( dateFormat, dateValue = new Date(), timezone ) {
|
|
|
558
523
|
* Formats a date (like `wp_date()` in PHP), translating it into site's locale
|
|
559
524
|
* and using the UTC timezone.
|
|
560
525
|
*
|
|
561
|
-
* @param
|
|
562
|
-
*
|
|
563
|
-
* @param
|
|
564
|
-
*
|
|
526
|
+
* @param dateFormat PHP-style formatting string.
|
|
527
|
+
* See [php.net/date](https://www.php.net/manual/en/function.date.php).
|
|
528
|
+
* @param dateValue Date object or string,
|
|
529
|
+
* parsable by moment.js.
|
|
565
530
|
*
|
|
566
|
-
* @return
|
|
531
|
+
* @return Formatted date.
|
|
567
532
|
*/
|
|
568
|
-
export function gmdateI18n(
|
|
533
|
+
export function gmdateI18n(
|
|
534
|
+
dateFormat: string,
|
|
535
|
+
dateValue: Moment | Date | string | number = new Date()
|
|
536
|
+
) {
|
|
569
537
|
const dateMoment = momentLib( dateValue ).utc();
|
|
570
538
|
dateMoment.locale( settings.l10n.locale );
|
|
571
539
|
return format( dateFormat, dateMoment );
|
|
@@ -574,11 +542,11 @@ export function gmdateI18n( dateFormat, dateValue = new Date() ) {
|
|
|
574
542
|
/**
|
|
575
543
|
* Check whether a date is considered in the future according to the WordPress settings.
|
|
576
544
|
*
|
|
577
|
-
* @param
|
|
545
|
+
* @param dateValue Date String or Date object in the Defined WP Timezone.
|
|
578
546
|
*
|
|
579
|
-
* @return
|
|
547
|
+
* @return Is in the future.
|
|
580
548
|
*/
|
|
581
|
-
export function isInTheFuture( dateValue ) {
|
|
549
|
+
export function isInTheFuture( dateValue: Date | string | number ) {
|
|
582
550
|
const now = momentLib.tz( WP_ZONE );
|
|
583
551
|
const momentObject = momentLib.tz( dateValue, WP_ZONE );
|
|
584
552
|
|
|
@@ -588,11 +556,11 @@ export function isInTheFuture( dateValue ) {
|
|
|
588
556
|
/**
|
|
589
557
|
* Create and return a JavaScript Date Object from a date string in the WP timezone.
|
|
590
558
|
*
|
|
591
|
-
* @param
|
|
559
|
+
* @param dateString Date formatted in the WP timezone.
|
|
592
560
|
*
|
|
593
|
-
* @return
|
|
561
|
+
* @return Date
|
|
594
562
|
*/
|
|
595
|
-
export function getDate( dateString ) {
|
|
563
|
+
export function getDate( dateString?: string | null ) {
|
|
596
564
|
if ( ! dateString ) {
|
|
597
565
|
return momentLib.tz( WP_ZONE ).toDate();
|
|
598
566
|
}
|
|
@@ -603,12 +571,15 @@ export function getDate( dateString ) {
|
|
|
603
571
|
/**
|
|
604
572
|
* Returns a human-readable time difference between two dates, like human_time_diff() in PHP.
|
|
605
573
|
*
|
|
606
|
-
* @param
|
|
607
|
-
* @param
|
|
574
|
+
* @param from From date, in the WP timezone.
|
|
575
|
+
* @param to To date, formatted in the WP timezone.
|
|
608
576
|
*
|
|
609
|
-
* @return
|
|
577
|
+
* @return Human-readable time difference.
|
|
610
578
|
*/
|
|
611
|
-
export function humanTimeDiff(
|
|
579
|
+
export function humanTimeDiff(
|
|
580
|
+
from: Moment | Date | string | number,
|
|
581
|
+
to?: Moment | Date | string | number
|
|
582
|
+
) {
|
|
612
583
|
const fromMoment = momentLib.tz( from, WP_ZONE );
|
|
613
584
|
const toMoment = to ? momentLib.tz( to, WP_ZONE ) : momentLib.tz( WP_ZONE );
|
|
614
585
|
return fromMoment.from( toMoment );
|
|
@@ -617,23 +588,26 @@ export function humanTimeDiff( from, to ) {
|
|
|
617
588
|
/**
|
|
618
589
|
* Creates a moment instance using the given timezone or, if none is provided, using global settings.
|
|
619
590
|
*
|
|
620
|
-
* @param
|
|
621
|
-
*
|
|
622
|
-
* @param
|
|
623
|
-
*
|
|
624
|
-
*
|
|
591
|
+
* @param dateValue Date object or string, parsable
|
|
592
|
+
* by moment.js.
|
|
593
|
+
* @param timezone Timezone to output result in or a
|
|
594
|
+
* UTC offset. Defaults to timezone from
|
|
595
|
+
* site.
|
|
625
596
|
*
|
|
626
597
|
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
627
598
|
* @see https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
|
|
628
599
|
*
|
|
629
|
-
* @return
|
|
600
|
+
* @return A moment instance.
|
|
630
601
|
*/
|
|
631
|
-
function buildMoment(
|
|
602
|
+
function buildMoment(
|
|
603
|
+
dateValue?: Moment | Date | string | number,
|
|
604
|
+
timezone: string | number = ''
|
|
605
|
+
) {
|
|
632
606
|
const dateMoment = momentLib( dateValue );
|
|
633
607
|
|
|
634
608
|
if ( timezone && ! isUTCOffset( timezone ) ) {
|
|
635
609
|
// The ! isUTCOffset() check guarantees that timezone is a string.
|
|
636
|
-
return dateMoment.tz(
|
|
610
|
+
return dateMoment.tz( timezone as string );
|
|
637
611
|
}
|
|
638
612
|
|
|
639
613
|
if ( timezone && isUTCOffset( timezone ) ) {
|
|
@@ -650,11 +624,11 @@ function buildMoment( dateValue, timezone = '' ) {
|
|
|
650
624
|
/**
|
|
651
625
|
* Returns whether a certain UTC offset is valid or not.
|
|
652
626
|
*
|
|
653
|
-
* @param
|
|
627
|
+
* @param offset a UTC offset.
|
|
654
628
|
*
|
|
655
|
-
* @return
|
|
629
|
+
* @return whether a certain UTC offset is valid or not.
|
|
656
630
|
*/
|
|
657
|
-
function isUTCOffset( offset ) {
|
|
631
|
+
function isUTCOffset( offset: number | string ) {
|
|
658
632
|
if ( 'number' === typeof offset ) {
|
|
659
633
|
return true;
|
|
660
634
|
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { LocaleSpecification as MomentLocaleSpecification } from 'moment';
|
|
5
|
+
|
|
6
|
+
export type MeridiemConfig = {
|
|
7
|
+
/**
|
|
8
|
+
* Lowercase AM.
|
|
9
|
+
*/
|
|
10
|
+
am: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Uppercase AM.
|
|
14
|
+
*/
|
|
15
|
+
AM: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Lowercase PM.
|
|
19
|
+
*/
|
|
20
|
+
pm: string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Uppercase PM.
|
|
24
|
+
*/
|
|
25
|
+
PM: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type FormatsConfig = {
|
|
29
|
+
/**
|
|
30
|
+
* Time format.
|
|
31
|
+
*/
|
|
32
|
+
time: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Date format.
|
|
36
|
+
*/
|
|
37
|
+
date: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Datetime format.
|
|
41
|
+
*/
|
|
42
|
+
datetime: string;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Abbreviated datetime format.
|
|
46
|
+
*/
|
|
47
|
+
datetimeAbbreviated: string;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type TimezoneConfig = {
|
|
51
|
+
/**
|
|
52
|
+
* Offset setting.
|
|
53
|
+
*/
|
|
54
|
+
offset: string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Offset setting with decimals formatted to minutes.
|
|
58
|
+
*/
|
|
59
|
+
offsetFormatted: string;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The timezone as a string (e.g., `'America/Los_Angeles'`).
|
|
63
|
+
*/
|
|
64
|
+
string: string;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Abbreviation for the timezone.
|
|
68
|
+
*/
|
|
69
|
+
abbr: string;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export type L10nSettings = {
|
|
73
|
+
/**
|
|
74
|
+
* Moment locale.
|
|
75
|
+
*/
|
|
76
|
+
locale: string;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Locale months.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ['January', 'February', ... ]
|
|
83
|
+
*/
|
|
84
|
+
months: MomentLocaleSpecification[ 'months' ];
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Locale months short.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ['Jan', 'Feb', ... ]
|
|
91
|
+
*/
|
|
92
|
+
monthsShort: MomentLocaleSpecification[ 'monthsShort' ];
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Locale weekdays.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ['Sunday', 'Monday', ... ]
|
|
99
|
+
*/
|
|
100
|
+
weekdays: MomentLocaleSpecification[ 'weekdays' ];
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Locale weekdays short.
|
|
104
|
+
*/
|
|
105
|
+
weekdaysShort: MomentLocaleSpecification[ 'weekdaysShort' ];
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Meridiem config.
|
|
109
|
+
*/
|
|
110
|
+
meridiem: MeridiemConfig;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Relative time config.
|
|
114
|
+
*/
|
|
115
|
+
relative: MomentLocaleSpecification[ 'relativeTime' ];
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Day that the week starts on.
|
|
119
|
+
*/
|
|
120
|
+
startOfWeek: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export type DateSettings = {
|
|
124
|
+
/**
|
|
125
|
+
* Localization settings.
|
|
126
|
+
*/
|
|
127
|
+
l10n: L10nSettings;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Date/time formats config.
|
|
131
|
+
*/
|
|
132
|
+
formats: FormatsConfig;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Timezone settings.
|
|
136
|
+
*/
|
|
137
|
+
timezone: TimezoneConfig;
|
|
138
|
+
};
|