@trackunit/date-and-time-utils 0.0.51 → 0.0.52

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 CHANGED
@@ -37,6 +37,31 @@ const getTimeZoneOffset = (timeZone) => {
37
37
  const timeZonesAvailable = !intlEnumerator.shouldPolyfill()
38
38
  ? Intl.supportedValuesOf("timeZone")
39
39
  : intlEnumerator.supportedValuesOf("timeZone");
40
+ const LOCALES_WITH_23h_CYCLE = ["da", "de", "jp"];
41
+ const DEFAULT_HOUR_CYCLE = "h12";
42
+ /**
43
+ * Return the most probable hour cycle based on the locale
44
+ */
45
+ const getHourCycle = (locale) => {
46
+ if (!locale) {
47
+ return DEFAULT_HOUR_CYCLE;
48
+ }
49
+ const intl = new Intl.Locale(locale);
50
+ if (intl.hourCycle) {
51
+ return intl.hourCycle;
52
+ }
53
+ // Some browser e.g. apparently the latest chrome have a cycle defined here,
54
+ // but not in intl.hourCycle
55
+ // @ts-ignore
56
+ const hourCycles = intl.hourCycles || (typeof intl.getHourCycles === "function" && intl.getHourCycles());
57
+ if (Array.isArray(hourCycles) && hourCycles[0]) {
58
+ return hourCycles[0];
59
+ }
60
+ if (LOCALES_WITH_23h_CYCLE.includes(locale)) {
61
+ return "h23";
62
+ }
63
+ return DEFAULT_HOUR_CYCLE;
64
+ };
40
65
  /**
41
66
  * Formats a temporal date according to the specified format, time zone, and locale.
42
67
  *
@@ -77,8 +102,9 @@ const timeZonesAvailable = !intlEnumerator.shouldPolyfill()
77
102
  * const formattedDate = formatDateUtil(date, format, timeZone, locale);
78
103
  * Output: "5/10/23, 12:00:00 AM EDT"
79
104
  */
80
- const formatDateUtil = (date, format, timeZone, locale, hourCycle) => {
105
+ const formatDateUtil = (date, format, timeZone, locale) => {
81
106
  var _a, _b, _c, _d, _e, _f;
107
+ const hourCycle = getHourCycle(locale);
82
108
  const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
83
109
  if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "dateOnly") {
84
110
  if (dateObj instanceof polyfill.Temporal.ZonedDateTime) {
@@ -88,25 +114,25 @@ const formatDateUtil = (date, format, timeZone, locale, hourCycle) => {
88
114
  }
89
115
  return dateObj.toLocaleString(locale, {
90
116
  dateStyle: (_b = format.dateFormat) !== null && _b !== void 0 ? _b : "medium",
91
- hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
117
+ hourCycle,
92
118
  });
93
119
  }
94
120
  if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "timeOnly") {
95
121
  if (dateObj instanceof polyfill.Temporal.ZonedDateTime) {
96
122
  return dateObj.toPlainTime().toLocaleString(locale, {
97
123
  timeStyle: (_c = format.timeFormat) !== null && _c !== void 0 ? _c : "short",
98
- hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
124
+ hourCycle,
99
125
  });
100
126
  }
101
127
  return dateObj.toLocaleString(locale, {
102
128
  timeStyle: (_d = format.timeFormat) !== null && _d !== void 0 ? _d : "short",
103
- hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
129
+ hourCycle,
104
130
  });
105
131
  }
106
132
  return dateObj.toLocaleString(locale, {
107
133
  dateStyle: (_e = format === null || format === void 0 ? void 0 : format.dateFormat) !== null && _e !== void 0 ? _e : "medium",
108
134
  timeStyle: (_f = format === null || format === void 0 ? void 0 : format.timeFormat) !== null && _f !== void 0 ? _f : "short",
109
- hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
135
+ hourCycle,
110
136
  });
111
137
  };
112
138
  /**
@@ -1608,6 +1634,7 @@ exports.endOfWeekUtil = endOfWeekUtil;
1608
1634
  exports.formatDateUtil = formatDateUtil;
1609
1635
  exports.formatRangeUtil = formatRangeUtil;
1610
1636
  exports.getDurationFormat = getDurationFormat;
1637
+ exports.getHourCycle = getHourCycle;
1611
1638
  exports.getTimeZone = getTimeZone;
1612
1639
  exports.getTimeZoneOffset = getTimeZoneOffset;
1613
1640
  exports.getUTCFromTimeZonedUtil = getUTCFromTimeZonedUtil;
package/index.esm.js CHANGED
@@ -35,6 +35,31 @@ const getTimeZoneOffset = (timeZone) => {
35
35
  const timeZonesAvailable = !shouldPolyfill()
36
36
  ? Intl.supportedValuesOf("timeZone")
37
37
  : supportedValuesOf("timeZone");
38
+ const LOCALES_WITH_23h_CYCLE = ["da", "de", "jp"];
39
+ const DEFAULT_HOUR_CYCLE = "h12";
40
+ /**
41
+ * Return the most probable hour cycle based on the locale
42
+ */
43
+ const getHourCycle = (locale) => {
44
+ if (!locale) {
45
+ return DEFAULT_HOUR_CYCLE;
46
+ }
47
+ const intl = new Intl.Locale(locale);
48
+ if (intl.hourCycle) {
49
+ return intl.hourCycle;
50
+ }
51
+ // Some browser e.g. apparently the latest chrome have a cycle defined here,
52
+ // but not in intl.hourCycle
53
+ // @ts-ignore
54
+ const hourCycles = intl.hourCycles || (typeof intl.getHourCycles === "function" && intl.getHourCycles());
55
+ if (Array.isArray(hourCycles) && hourCycles[0]) {
56
+ return hourCycles[0];
57
+ }
58
+ if (LOCALES_WITH_23h_CYCLE.includes(locale)) {
59
+ return "h23";
60
+ }
61
+ return DEFAULT_HOUR_CYCLE;
62
+ };
38
63
  /**
39
64
  * Formats a temporal date according to the specified format, time zone, and locale.
40
65
  *
@@ -75,8 +100,9 @@ const timeZonesAvailable = !shouldPolyfill()
75
100
  * const formattedDate = formatDateUtil(date, format, timeZone, locale);
76
101
  * Output: "5/10/23, 12:00:00 AM EDT"
77
102
  */
78
- const formatDateUtil = (date, format, timeZone, locale, hourCycle) => {
103
+ const formatDateUtil = (date, format, timeZone, locale) => {
79
104
  var _a, _b, _c, _d, _e, _f;
105
+ const hourCycle = getHourCycle(locale);
80
106
  const dateObj = timeZone ? toZonedDateTimeUtil(date, timeZone) : toTemporalUtil(toDateUtil(date));
81
107
  if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "dateOnly") {
82
108
  if (dateObj instanceof Temporal.ZonedDateTime) {
@@ -86,25 +112,25 @@ const formatDateUtil = (date, format, timeZone, locale, hourCycle) => {
86
112
  }
87
113
  return dateObj.toLocaleString(locale, {
88
114
  dateStyle: (_b = format.dateFormat) !== null && _b !== void 0 ? _b : "medium",
89
- hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
115
+ hourCycle,
90
116
  });
91
117
  }
92
118
  if ((format === null || format === void 0 ? void 0 : format.selectFormat) === "timeOnly") {
93
119
  if (dateObj instanceof Temporal.ZonedDateTime) {
94
120
  return dateObj.toPlainTime().toLocaleString(locale, {
95
121
  timeStyle: (_c = format.timeFormat) !== null && _c !== void 0 ? _c : "short",
96
- hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
122
+ hourCycle,
97
123
  });
98
124
  }
99
125
  return dateObj.toLocaleString(locale, {
100
126
  timeStyle: (_d = format.timeFormat) !== null && _d !== void 0 ? _d : "short",
101
- hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
127
+ hourCycle,
102
128
  });
103
129
  }
104
130
  return dateObj.toLocaleString(locale, {
105
131
  dateStyle: (_e = format === null || format === void 0 ? void 0 : format.dateFormat) !== null && _e !== void 0 ? _e : "medium",
106
132
  timeStyle: (_f = format === null || format === void 0 ? void 0 : format.timeFormat) !== null && _f !== void 0 ? _f : "short",
107
- hourCycle: hourCycle !== null && hourCycle !== void 0 ? hourCycle : "h12",
133
+ hourCycle,
108
134
  });
109
135
  };
110
136
  /**
@@ -1579,4 +1605,4 @@ const startOfWeek = (date, weekStartsOn) => {
1579
1605
  return date.subtract({ days: diff });
1580
1606
  };
1581
1607
 
1582
- 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, getTimeZone, getTimeZoneOffset, getUTCFromTimeZonedUtil, getWeekUtil, isBetweenUtil, isEqualUtil, isFutureUtil, isPastUtil, isSameDayUtil, isSameMonthUtil, isSameWeekUtil, isSameYearUtil, isTodayUtil, monthNameUtil, monthsUtil, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, startOfMonthUtil, startOfWeekUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, subtractMonthsUtil, subtractWeeksUtil, subtractYearsUtil, timeSinceAuto, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInMonths, timeSinceInSeconds, timeSinceInYears, timeZonesAvailable, toDateUtil, toDuration, toInstantUtil, toTemporalUtil, toZonedDateTimeUtil };
1608
+ 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, monthNameUtil, monthsUtil, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, startOfMonthUtil, startOfWeekUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, subtractMonthsUtil, subtractWeeksUtil, subtractYearsUtil, timeSinceAuto, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInMonths, timeSinceInSeconds, timeSinceInYears, timeZonesAvailable, toDateUtil, toDuration, toInstantUtil, toTemporalUtil, toZonedDateTimeUtil };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/date-and-time-utils",
3
- "version": "0.0.51",
3
+ "version": "0.0.52",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -4,7 +4,6 @@ export type TemporalDate = Temporal.Instant | Temporal.ZonedDateTime | Date;
4
4
  type TemporalFormatTypes = Intl.DateTimeFormatOptions;
5
5
  type TemporalTimeTypes = Extract<Intl.RelativeTimeFormatUnit, "days" | "hours" | "minutes" | "seconds">;
6
6
  type TemporalFormatSelector = "dateOnly" | "timeOnly";
7
- export type HourCycle = "h12" | "h23";
8
7
  export type TemporalFormatStyle = Intl.ListFormatStyle;
9
8
  export interface DateRange {
10
9
  start: Date;
@@ -39,6 +38,10 @@ export declare const getTimeZoneOffset: (timeZone: string) => string;
39
38
  * @returns {string[]}
40
39
  */
41
40
  export declare const timeZonesAvailable: string[];
41
+ /**
42
+ * Return the most probable hour cycle based on the locale
43
+ */
44
+ export declare const getHourCycle: (locale?: string) => Intl.LocaleHourCycleKey;
42
45
  /**
43
46
  * Formats a temporal date according to the specified format, time zone, and locale.
44
47
  *
@@ -79,7 +82,7 @@ export declare const timeZonesAvailable: string[];
79
82
  * const formattedDate = formatDateUtil(date, format, timeZone, locale);
80
83
  * Output: "5/10/23, 12:00:00 AM EDT"
81
84
  */
82
- export declare const formatDateUtil: (date: TemporalDate, format?: TemporalFormat, timeZone?: string, locale?: string, hourCycle?: HourCycle) => string;
85
+ export declare const formatDateUtil: (date: TemporalDate, format?: TemporalFormat, timeZone?: string, locale?: string) => string;
83
86
  /**
84
87
  * Formats a date range.
85
88
  *