@trackunit/date-and-time-utils 1.13.37 → 1.13.39
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/index.cjs.js +18 -17
- package/index.esm.js +18 -17
- package/package.json +1 -1
- package/src/DateAndTimeUtils.d.ts +7 -0
package/index.cjs.js
CHANGED
|
@@ -32,8 +32,15 @@ const getTimeZoneOffset = (timeZone) => {
|
|
|
32
32
|
* @returns {string[]}
|
|
33
33
|
*/
|
|
34
34
|
const timeZonesAvailable = Intl.supportedValuesOf("timeZone");
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Fallback hour cycle for when the locale's convention can't be derived from `Intl`.
|
|
37
|
+
*
|
|
38
|
+
* English-language locales default to 12-hour (AM/PM); every other locale defaults to the
|
|
39
|
+
* more globally common 24-hour cycle.
|
|
40
|
+
*
|
|
41
|
+
* @param locale - The locale to pick a fallback for. Treated as English-default when absent.
|
|
42
|
+
*/
|
|
43
|
+
const fallbackHourCycle = (locale) => !locale || locale.toLowerCase().startsWith("en") ? "h12" : "h23";
|
|
37
44
|
/**
|
|
38
45
|
* Maps a user "time format" preference onto an `Intl` hour cycle.
|
|
39
46
|
*
|
|
@@ -56,6 +63,13 @@ const timeFormatToHourCycle = (timeFormat) => {
|
|
|
56
63
|
/**
|
|
57
64
|
* Return the most probable hour cycle based on the locale.
|
|
58
65
|
*
|
|
66
|
+
* The locale-derived cycle comes from `Intl.DateTimeFormat(...).resolvedOptions().hourCycle`,
|
|
67
|
+
* which derives the 12H/24H convention from CLDR for every locale and honors an explicit
|
|
68
|
+
* `-u-hc-` extension in the locale tag. Unlike `Intl.Locale.prototype.getHourCycles`, this API
|
|
69
|
+
* is supported in all our target browsers (including Firefox), so locales such as `pl` correctly
|
|
70
|
+
* resolve to `h23` instead of silently falling back to the 12-hour default. If the cycle still
|
|
71
|
+
* can't be derived, it falls back to {@link fallbackHourCycle} (English → 12H, otherwise 24H).
|
|
72
|
+
*
|
|
59
73
|
* @param locale - The locale used to derive the hour cycle.
|
|
60
74
|
* @param hourCycleOverride - When provided, takes precedence over the locale-derived hour cycle.
|
|
61
75
|
* Used to honor an explicit user time-format preference (12H/24H).
|
|
@@ -65,22 +79,9 @@ const getHourCycle = (locale, hourCycleOverride) => {
|
|
|
65
79
|
return hourCycleOverride;
|
|
66
80
|
}
|
|
67
81
|
if (!locale) {
|
|
68
|
-
return
|
|
69
|
-
}
|
|
70
|
-
const intl = new Intl.Locale(locale);
|
|
71
|
-
if (intl.hourCycle) {
|
|
72
|
-
return intl.hourCycle;
|
|
73
|
-
}
|
|
74
|
-
if ("getHourCycles" in intl && typeof intl.getHourCycles === "function") {
|
|
75
|
-
const hourCycles = intl.getHourCycles();
|
|
76
|
-
if (hourCycles[0]) {
|
|
77
|
-
return hourCycles[0];
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (LOCALES_WITH_23h_CYCLE.includes(locale)) {
|
|
81
|
-
return "h23";
|
|
82
|
+
return fallbackHourCycle(locale);
|
|
82
83
|
}
|
|
83
|
-
return
|
|
84
|
+
return new Intl.DateTimeFormat(locale, { hour: "numeric" }).resolvedOptions().hourCycle ?? fallbackHourCycle(locale);
|
|
84
85
|
};
|
|
85
86
|
/**
|
|
86
87
|
* Formats a temporal date according to the specified format, time zone, and locale.
|
package/index.esm.js
CHANGED
|
@@ -31,8 +31,15 @@ const getTimeZoneOffset = (timeZone) => {
|
|
|
31
31
|
* @returns {string[]}
|
|
32
32
|
*/
|
|
33
33
|
const timeZonesAvailable = Intl.supportedValuesOf("timeZone");
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Fallback hour cycle for when the locale's convention can't be derived from `Intl`.
|
|
36
|
+
*
|
|
37
|
+
* English-language locales default to 12-hour (AM/PM); every other locale defaults to the
|
|
38
|
+
* more globally common 24-hour cycle.
|
|
39
|
+
*
|
|
40
|
+
* @param locale - The locale to pick a fallback for. Treated as English-default when absent.
|
|
41
|
+
*/
|
|
42
|
+
const fallbackHourCycle = (locale) => !locale || locale.toLowerCase().startsWith("en") ? "h12" : "h23";
|
|
36
43
|
/**
|
|
37
44
|
* Maps a user "time format" preference onto an `Intl` hour cycle.
|
|
38
45
|
*
|
|
@@ -55,6 +62,13 @@ const timeFormatToHourCycle = (timeFormat) => {
|
|
|
55
62
|
/**
|
|
56
63
|
* Return the most probable hour cycle based on the locale.
|
|
57
64
|
*
|
|
65
|
+
* The locale-derived cycle comes from `Intl.DateTimeFormat(...).resolvedOptions().hourCycle`,
|
|
66
|
+
* which derives the 12H/24H convention from CLDR for every locale and honors an explicit
|
|
67
|
+
* `-u-hc-` extension in the locale tag. Unlike `Intl.Locale.prototype.getHourCycles`, this API
|
|
68
|
+
* is supported in all our target browsers (including Firefox), so locales such as `pl` correctly
|
|
69
|
+
* resolve to `h23` instead of silently falling back to the 12-hour default. If the cycle still
|
|
70
|
+
* can't be derived, it falls back to {@link fallbackHourCycle} (English → 12H, otherwise 24H).
|
|
71
|
+
*
|
|
58
72
|
* @param locale - The locale used to derive the hour cycle.
|
|
59
73
|
* @param hourCycleOverride - When provided, takes precedence over the locale-derived hour cycle.
|
|
60
74
|
* Used to honor an explicit user time-format preference (12H/24H).
|
|
@@ -64,22 +78,9 @@ const getHourCycle = (locale, hourCycleOverride) => {
|
|
|
64
78
|
return hourCycleOverride;
|
|
65
79
|
}
|
|
66
80
|
if (!locale) {
|
|
67
|
-
return
|
|
68
|
-
}
|
|
69
|
-
const intl = new Intl.Locale(locale);
|
|
70
|
-
if (intl.hourCycle) {
|
|
71
|
-
return intl.hourCycle;
|
|
72
|
-
}
|
|
73
|
-
if ("getHourCycles" in intl && typeof intl.getHourCycles === "function") {
|
|
74
|
-
const hourCycles = intl.getHourCycles();
|
|
75
|
-
if (hourCycles[0]) {
|
|
76
|
-
return hourCycles[0];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (LOCALES_WITH_23h_CYCLE.includes(locale)) {
|
|
80
|
-
return "h23";
|
|
81
|
+
return fallbackHourCycle(locale);
|
|
81
82
|
}
|
|
82
|
-
return
|
|
83
|
+
return new Intl.DateTimeFormat(locale, { hour: "numeric" }).resolvedOptions().hourCycle ?? fallbackHourCycle(locale);
|
|
83
84
|
};
|
|
84
85
|
/**
|
|
85
86
|
* Formats a temporal date according to the specified format, time zone, and locale.
|
package/package.json
CHANGED
|
@@ -52,6 +52,13 @@ export declare const timeFormatToHourCycle: (timeFormat?: "LANGUAGE_DEFAULT" | "
|
|
|
52
52
|
/**
|
|
53
53
|
* Return the most probable hour cycle based on the locale.
|
|
54
54
|
*
|
|
55
|
+
* The locale-derived cycle comes from `Intl.DateTimeFormat(...).resolvedOptions().hourCycle`,
|
|
56
|
+
* which derives the 12H/24H convention from CLDR for every locale and honors an explicit
|
|
57
|
+
* `-u-hc-` extension in the locale tag. Unlike `Intl.Locale.prototype.getHourCycles`, this API
|
|
58
|
+
* is supported in all our target browsers (including Firefox), so locales such as `pl` correctly
|
|
59
|
+
* resolve to `h23` instead of silently falling back to the 12-hour default. If the cycle still
|
|
60
|
+
* can't be derived, it falls back to {@link fallbackHourCycle} (English → 12H, otherwise 24H).
|
|
61
|
+
*
|
|
55
62
|
* @param locale - The locale used to derive the hour cycle.
|
|
56
63
|
* @param hourCycleOverride - When provided, takes precedence over the locale-derived hour cycle.
|
|
57
64
|
* Used to honor an explicit user time-format preference (12H/24H).
|