@trackunit/date-and-time-utils 1.13.26-alpha-27ad777c16e.0 → 1.13.29
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 +33 -4
- package/index.esm.js +33 -5
- package/package.json +1 -1
- package/src/DateAndTimeUtils.d.ts +19 -3
package/index.cjs.js
CHANGED
|
@@ -35,9 +35,35 @@ const timeZonesAvailable = Intl.supportedValuesOf("timeZone");
|
|
|
35
35
|
const LOCALES_WITH_23h_CYCLE = ["da", "de", "jp"];
|
|
36
36
|
const DEFAULT_HOUR_CYCLE = "h12";
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
38
|
+
* Maps a user "time format" preference onto an `Intl` hour cycle.
|
|
39
|
+
*
|
|
40
|
+
* Returns `undefined` for "language default" (or no preference) so callers fall back to the
|
|
41
|
+
* locale-derived hour cycle. A 12-hour preference resolves to `h12` and 24-hour to `h23`.
|
|
42
|
+
*
|
|
43
|
+
* @param timeFormat - The user's time format preference (`"LANGUAGE_DEFAULT" | "TWELVE_HOUR" | "TWENTY_FOUR_HOUR"`).
|
|
44
|
+
* @returns {Intl.LocaleHourCycleKey | undefined} The forced hour cycle, or `undefined` to let the locale decide.
|
|
39
45
|
*/
|
|
40
|
-
const
|
|
46
|
+
const timeFormatToHourCycle = (timeFormat) => {
|
|
47
|
+
switch (timeFormat) {
|
|
48
|
+
case "TWELVE_HOUR":
|
|
49
|
+
return "h12";
|
|
50
|
+
case "TWENTY_FOUR_HOUR":
|
|
51
|
+
return "h23";
|
|
52
|
+
default:
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Return the most probable hour cycle based on the locale.
|
|
58
|
+
*
|
|
59
|
+
* @param locale - The locale used to derive the hour cycle.
|
|
60
|
+
* @param hourCycleOverride - When provided, takes precedence over the locale-derived hour cycle.
|
|
61
|
+
* Used to honor an explicit user time-format preference (12H/24H).
|
|
62
|
+
*/
|
|
63
|
+
const getHourCycle = (locale, hourCycleOverride) => {
|
|
64
|
+
if (hourCycleOverride) {
|
|
65
|
+
return hourCycleOverride;
|
|
66
|
+
}
|
|
41
67
|
if (!locale) {
|
|
42
68
|
return DEFAULT_HOUR_CYCLE;
|
|
43
69
|
}
|
|
@@ -63,6 +89,8 @@ const getHourCycle = (locale) => {
|
|
|
63
89
|
* @param {TemporalFormat} [format] - The format options for the date and/or time. If not provided, default format options will be used.
|
|
64
90
|
* @param {string} [timeZone] - The time zone to use for formatting. If not provided, the system's default time zone will be used.
|
|
65
91
|
* @param {string} [locale] - The locale to use for formatting. If not provided, the system's default locale will be used.
|
|
92
|
+
* @param {Intl.LocaleHourCycleKey} [hourCycleOverride] - Forces a specific hour cycle (e.g. `h12`/`h23`) regardless of locale.
|
|
93
|
+
* Used to honor an explicit user time-format preference. When omitted, the locale decides.
|
|
66
94
|
* @returns {string} The formatted date string.
|
|
67
95
|
* @example
|
|
68
96
|
* const dateString = "2023-05-10"
|
|
@@ -96,8 +124,8 @@ const getHourCycle = (locale) => {
|
|
|
96
124
|
* const formattedDate = formatDateUtil(date, format, timeZone, locale);
|
|
97
125
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
98
126
|
*/
|
|
99
|
-
const formatDateUtil = (date, format, timeZone, locale) => {
|
|
100
|
-
const hourCycle = getHourCycle(locale);
|
|
127
|
+
const formatDateUtil = (date, format, timeZone, locale, hourCycleOverride) => {
|
|
128
|
+
const hourCycle = getHourCycle(locale, hourCycleOverride);
|
|
101
129
|
const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
|
|
102
130
|
if (format?.selectFormat === "dateOnly") {
|
|
103
131
|
if (dateObj instanceof polyfill.Temporal.ZonedDateTime) {
|
|
@@ -1684,6 +1712,7 @@ exports.subtractMinutesUtil = subtractMinutesUtil;
|
|
|
1684
1712
|
exports.subtractMonthsUtil = subtractMonthsUtil;
|
|
1685
1713
|
exports.subtractWeeksUtil = subtractWeeksUtil;
|
|
1686
1714
|
exports.subtractYearsUtil = subtractYearsUtil;
|
|
1715
|
+
exports.timeFormatToHourCycle = timeFormatToHourCycle;
|
|
1687
1716
|
exports.timeSinceAuto = timeSinceAuto;
|
|
1688
1717
|
exports.timeSinceInDays = timeSinceInDays;
|
|
1689
1718
|
exports.timeSinceInHours = timeSinceInHours;
|
package/index.esm.js
CHANGED
|
@@ -34,9 +34,35 @@ const timeZonesAvailable = Intl.supportedValuesOf("timeZone");
|
|
|
34
34
|
const LOCALES_WITH_23h_CYCLE = ["da", "de", "jp"];
|
|
35
35
|
const DEFAULT_HOUR_CYCLE = "h12";
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* Maps a user "time format" preference onto an `Intl` hour cycle.
|
|
38
|
+
*
|
|
39
|
+
* Returns `undefined` for "language default" (or no preference) so callers fall back to the
|
|
40
|
+
* locale-derived hour cycle. A 12-hour preference resolves to `h12` and 24-hour to `h23`.
|
|
41
|
+
*
|
|
42
|
+
* @param timeFormat - The user's time format preference (`"LANGUAGE_DEFAULT" | "TWELVE_HOUR" | "TWENTY_FOUR_HOUR"`).
|
|
43
|
+
* @returns {Intl.LocaleHourCycleKey | undefined} The forced hour cycle, or `undefined` to let the locale decide.
|
|
38
44
|
*/
|
|
39
|
-
const
|
|
45
|
+
const timeFormatToHourCycle = (timeFormat) => {
|
|
46
|
+
switch (timeFormat) {
|
|
47
|
+
case "TWELVE_HOUR":
|
|
48
|
+
return "h12";
|
|
49
|
+
case "TWENTY_FOUR_HOUR":
|
|
50
|
+
return "h23";
|
|
51
|
+
default:
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Return the most probable hour cycle based on the locale.
|
|
57
|
+
*
|
|
58
|
+
* @param locale - The locale used to derive the hour cycle.
|
|
59
|
+
* @param hourCycleOverride - When provided, takes precedence over the locale-derived hour cycle.
|
|
60
|
+
* Used to honor an explicit user time-format preference (12H/24H).
|
|
61
|
+
*/
|
|
62
|
+
const getHourCycle = (locale, hourCycleOverride) => {
|
|
63
|
+
if (hourCycleOverride) {
|
|
64
|
+
return hourCycleOverride;
|
|
65
|
+
}
|
|
40
66
|
if (!locale) {
|
|
41
67
|
return DEFAULT_HOUR_CYCLE;
|
|
42
68
|
}
|
|
@@ -62,6 +88,8 @@ const getHourCycle = (locale) => {
|
|
|
62
88
|
* @param {TemporalFormat} [format] - The format options for the date and/or time. If not provided, default format options will be used.
|
|
63
89
|
* @param {string} [timeZone] - The time zone to use for formatting. If not provided, the system's default time zone will be used.
|
|
64
90
|
* @param {string} [locale] - The locale to use for formatting. If not provided, the system's default locale will be used.
|
|
91
|
+
* @param {Intl.LocaleHourCycleKey} [hourCycleOverride] - Forces a specific hour cycle (e.g. `h12`/`h23`) regardless of locale.
|
|
92
|
+
* Used to honor an explicit user time-format preference. When omitted, the locale decides.
|
|
65
93
|
* @returns {string} The formatted date string.
|
|
66
94
|
* @example
|
|
67
95
|
* const dateString = "2023-05-10"
|
|
@@ -95,8 +123,8 @@ const getHourCycle = (locale) => {
|
|
|
95
123
|
* const formattedDate = formatDateUtil(date, format, timeZone, locale);
|
|
96
124
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
97
125
|
*/
|
|
98
|
-
const formatDateUtil = (date, format, timeZone, locale) => {
|
|
99
|
-
const hourCycle = getHourCycle(locale);
|
|
126
|
+
const formatDateUtil = (date, format, timeZone, locale, hourCycleOverride) => {
|
|
127
|
+
const hourCycle = getHourCycle(locale, hourCycleOverride);
|
|
100
128
|
const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
|
|
101
129
|
if (format?.selectFormat === "dateOnly") {
|
|
102
130
|
if (dateObj instanceof Temporal.ZonedDateTime) {
|
|
@@ -1623,4 +1651,4 @@ const parseValidDate = (date) => {
|
|
|
1623
1651
|
return null;
|
|
1624
1652
|
};
|
|
1625
1653
|
|
|
1626
|
-
export { addDaysUtil, addHoursUtil, addMinutesUtil, addMonthsUtil, addWeeksUtil, addYearsUtil, convertHoursUtil, convertMillisecondsUtil, convertMinutesUtil, convertSecondsUtil, dayNameUtil, daysUtil, differenceInDaysUtil, differenceInHoursUtil, differenceInMinutesUtil, differenceInMonthsUtil, differenceInSecondsUtil, differenceInWeeksUtil, differenceInYearsUtil, endOfDayUtil, endOfHourUtil, endOfMinuteUtil, endOfMonthUtil, endOfWeekUtil, formatDateUtil, formatRangeUtil, getDurationFormat, getHourCycle, getTimeZone, getTimeZoneOffset, getUTCFromTimeZonedUtil, getWeekUtil, isBetweenUtil, isEqualUtil, isFutureUtil, isPastUtil, isSameDayUtil, isSameMonthUtil, isSameWeekUtil, isSameYearUtil, isTodayUtil, isValidDate, monthNameUtil, monthsUtil, parseValidDate, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, startOfMonthUtil, startOfWeekUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, subtractMonthsUtil, subtractWeeksUtil, subtractYearsUtil, timeSinceAuto, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInMonths, timeSinceInSeconds, timeSinceInYears, timeZonesAvailable, toDateUtil, toDuration, toInstantUtil, toTemporalUtil, toZonedDateTimeUtil };
|
|
1654
|
+
export { addDaysUtil, addHoursUtil, addMinutesUtil, addMonthsUtil, addWeeksUtil, addYearsUtil, convertHoursUtil, convertMillisecondsUtil, convertMinutesUtil, convertSecondsUtil, dayNameUtil, daysUtil, differenceInDaysUtil, differenceInHoursUtil, differenceInMinutesUtil, differenceInMonthsUtil, differenceInSecondsUtil, differenceInWeeksUtil, differenceInYearsUtil, endOfDayUtil, endOfHourUtil, endOfMinuteUtil, endOfMonthUtil, endOfWeekUtil, formatDateUtil, formatRangeUtil, getDurationFormat, getHourCycle, getTimeZone, getTimeZoneOffset, getUTCFromTimeZonedUtil, getWeekUtil, isBetweenUtil, isEqualUtil, isFutureUtil, isPastUtil, isSameDayUtil, isSameMonthUtil, isSameWeekUtil, isSameYearUtil, isTodayUtil, isValidDate, monthNameUtil, monthsUtil, parseValidDate, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, startOfMonthUtil, startOfWeekUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, subtractMonthsUtil, subtractWeeksUtil, subtractYearsUtil, timeFormatToHourCycle, timeSinceAuto, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInMonths, timeSinceInSeconds, timeSinceInYears, timeZonesAvailable, toDateUtil, toDuration, toInstantUtil, toTemporalUtil, toZonedDateTimeUtil };
|
package/package.json
CHANGED
|
@@ -40,9 +40,23 @@ export declare const getTimeZoneOffset: (timeZone: string) => string;
|
|
|
40
40
|
*/
|
|
41
41
|
export declare const timeZonesAvailable: string[];
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
43
|
+
* Maps a user "time format" preference onto an `Intl` hour cycle.
|
|
44
|
+
*
|
|
45
|
+
* Returns `undefined` for "language default" (or no preference) so callers fall back to the
|
|
46
|
+
* locale-derived hour cycle. A 12-hour preference resolves to `h12` and 24-hour to `h23`.
|
|
47
|
+
*
|
|
48
|
+
* @param timeFormat - The user's time format preference (`"LANGUAGE_DEFAULT" | "TWELVE_HOUR" | "TWENTY_FOUR_HOUR"`).
|
|
49
|
+
* @returns {Intl.LocaleHourCycleKey | undefined} The forced hour cycle, or `undefined` to let the locale decide.
|
|
50
|
+
*/
|
|
51
|
+
export declare const timeFormatToHourCycle: (timeFormat?: "LANGUAGE_DEFAULT" | "TWELVE_HOUR" | "TWENTY_FOUR_HOUR" | null) => Intl.LocaleHourCycleKey | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Return the most probable hour cycle based on the locale.
|
|
54
|
+
*
|
|
55
|
+
* @param locale - The locale used to derive the hour cycle.
|
|
56
|
+
* @param hourCycleOverride - When provided, takes precedence over the locale-derived hour cycle.
|
|
57
|
+
* Used to honor an explicit user time-format preference (12H/24H).
|
|
44
58
|
*/
|
|
45
|
-
export declare const getHourCycle: (locale?: string) => Intl.LocaleHourCycleKey;
|
|
59
|
+
export declare const getHourCycle: (locale?: string, hourCycleOverride?: Intl.LocaleHourCycleKey) => Intl.LocaleHourCycleKey;
|
|
46
60
|
/**
|
|
47
61
|
* Formats a temporal date according to the specified format, time zone, and locale.
|
|
48
62
|
*
|
|
@@ -50,6 +64,8 @@ export declare const getHourCycle: (locale?: string) => Intl.LocaleHourCycleKey;
|
|
|
50
64
|
* @param {TemporalFormat} [format] - The format options for the date and/or time. If not provided, default format options will be used.
|
|
51
65
|
* @param {string} [timeZone] - The time zone to use for formatting. If not provided, the system's default time zone will be used.
|
|
52
66
|
* @param {string} [locale] - The locale to use for formatting. If not provided, the system's default locale will be used.
|
|
67
|
+
* @param {Intl.LocaleHourCycleKey} [hourCycleOverride] - Forces a specific hour cycle (e.g. `h12`/`h23`) regardless of locale.
|
|
68
|
+
* Used to honor an explicit user time-format preference. When omitted, the locale decides.
|
|
53
69
|
* @returns {string} The formatted date string.
|
|
54
70
|
* @example
|
|
55
71
|
* const dateString = "2023-05-10"
|
|
@@ -83,7 +99,7 @@ export declare const getHourCycle: (locale?: string) => Intl.LocaleHourCycleKey;
|
|
|
83
99
|
* const formattedDate = formatDateUtil(date, format, timeZone, locale);
|
|
84
100
|
* Output: "5/10/23, 12:00:00 AM EDT"
|
|
85
101
|
*/
|
|
86
|
-
export declare const formatDateUtil: (date: TemporalDate, format?: TemporalFormat, timeZone?: string, locale?: string) => string;
|
|
102
|
+
export declare const formatDateUtil: (date: TemporalDate, format?: TemporalFormat, timeZone?: string, locale?: string, hourCycleOverride?: Intl.LocaleHourCycleKey) => string;
|
|
87
103
|
/**
|
|
88
104
|
* Formats a date range.
|
|
89
105
|
*
|