@trackunit/react-date-and-time-hooks 2.1.16-alpha-9d327375fc1.0 → 2.1.19

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
@@ -3,8 +3,8 @@
3
3
  var react = require('react');
4
4
  var dateAndTimeUtils = require('@trackunit/date-and-time-utils');
5
5
  var sharedUtils = require('@trackunit/shared-utils');
6
- var irisAppRuntimeCoreApi = require('@trackunit/iris-app-runtime-core-api');
7
6
  var reactCoreHooks = require('@trackunit/react-core-hooks');
7
+ var irisAppRuntimeCoreApi = require('@trackunit/iris-app-runtime-core-api');
8
8
 
9
9
  const AssetTimezoneContext = react.createContext({ assetTimeZone: null });
10
10
  /** Returns the resolved asset timezone from the nearest AssetTimezoneProvider in the component tree. */
@@ -25,6 +25,23 @@ const convertToLocale = (userPreferenceLanguage) => {
25
25
  return userPreferenceLanguage;
26
26
  };
27
27
 
28
+ /**
29
+ * Resolves the hour cycle that should be forced based on the current user's time-format preference.
30
+ *
31
+ * Returns `undefined` when the user has chosen "let the language decide" (the default), so that
32
+ * locale-based formatting keeps deciding 12H/24H. Returns `"h12"` for a 12-hour preference and
33
+ * `"h23"` for a 24-hour preference.
34
+ *
35
+ * Pass the result to `formatDateUtil(..., hourCycleOverride)` (or use `useDateAndTime`, `DateTime`,
36
+ * and `DateTimeCell`, which already apply it) to make displayed times honor the preference.
37
+ *
38
+ * @returns {Intl.LocaleHourCycleKey | undefined} The forced hour cycle, or `undefined` to defer to the locale.
39
+ */
40
+ const useHourCycle = () => {
41
+ const { timeFormat } = reactCoreHooks.useCurrentUserTimeFormat();
42
+ return react.useMemo(() => dateAndTimeUtils.timeFormatToHourCycle(timeFormat), [timeFormat]);
43
+ };
44
+
28
45
  const LANG_STORAGE_KEY = "i18nextLng";
29
46
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
30
47
  const BrowserLocale = navigator.language ?? "en";
@@ -135,6 +152,7 @@ const useTimezone = () => {
135
152
  */
136
153
  const useDateAndTime = () => {
137
154
  const currentLocale = useLocale();
155
+ const hourCycle = useHourCycle();
138
156
  const { current, assetTimeZone, preferred } = useTimezone();
139
157
  const currentTimeZone = react.useMemo(() => current.id || preferred.id || assetTimeZone?.id, [current, assetTimeZone, preferred]);
140
158
  const getNowDate = react.useCallback(() => {
@@ -150,8 +168,8 @@ const useDateAndTime = () => {
150
168
  * @returns {string}
151
169
  */
152
170
  const formatDate = react.useCallback((date, format) => {
153
- return dateAndTimeUtils.formatDateUtil(date, format, currentTimeZone, currentLocale);
154
- }, [currentLocale, currentTimeZone]);
171
+ return dateAndTimeUtils.formatDateUtil(date, format, currentTimeZone, currentLocale, hourCycle);
172
+ }, [currentLocale, currentTimeZone, hourCycle]);
155
173
  /**
156
174
  * Formats a date using the custom locale and custom timezone.
157
175
  *
@@ -162,8 +180,8 @@ const useDateAndTime = () => {
162
180
  * @returns {string}
163
181
  */
164
182
  const formatDateWithTimezone = react.useCallback((date, format, timezone, locale) => {
165
- return dateAndTimeUtils.formatDateUtil(date, format, timezone ?? currentTimeZone, locale ?? currentLocale);
166
- }, [currentLocale, currentTimeZone]);
183
+ return dateAndTimeUtils.formatDateUtil(date, format, timezone ?? currentTimeZone, locale ?? currentLocale, hourCycle);
184
+ }, [currentLocale, currentTimeZone, hourCycle]);
167
185
  /**
168
186
  * Formats a date range using the current locale and time zone.
169
187
  *
@@ -519,5 +537,6 @@ exports.AssetTimezoneContext = AssetTimezoneContext;
519
537
  exports.convertToLocale = convertToLocale;
520
538
  exports.useAssetTimezoneContext = useAssetTimezoneContext;
521
539
  exports.useDateAndTime = useDateAndTime;
540
+ exports.useHourCycle = useHourCycle;
522
541
  exports.useLocale = useLocale;
523
542
  exports.useTimezone = useTimezone;
package/index.esm.js CHANGED
@@ -1,8 +1,8 @@
1
- import { createContext, useContext, useState, useCallback, useMemo } from 'react';
2
- import { Temporal, getTimeZone, toDateUtil, toZonedDateTimeUtil, formatDateUtil, formatRangeUtil, subtractYearsUtil, subtractMonthsUtil, subtractWeeksUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, addYearsUtil, addMonthsUtil, addWeeksUtil, addDaysUtil, addHoursUtil, addMinutesUtil, startOfMonthUtil, startOfWeekUtil, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, endOfMonthUtil, endOfWeekUtil, endOfDayUtil, endOfHourUtil, endOfMinuteUtil, differenceInYearsUtil, differenceInMonthsUtil, differenceInWeeksUtil, differenceInDaysUtil, differenceInHoursUtil, differenceInMinutesUtil, differenceInSecondsUtil, isBetweenUtil, isTodayUtil, isFutureUtil, isPastUtil, isSameYearUtil, isSameMonthUtil, isSameWeekUtil, isSameDayUtil, timeSinceInYears, timeSinceInMonths, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInSeconds, timeSinceAuto, toDuration, getDurationFormat, convertMillisecondsUtil, convertSecondsUtil, convertMinutesUtil, convertHoursUtil, dayNameUtil, daysUtil, monthNameUtil, monthsUtil, getUTCFromTimeZonedUtil, isEqualUtil } from '@trackunit/date-and-time-utils';
1
+ import { createContext, useContext, useMemo, useState, useCallback } from 'react';
2
+ import { timeFormatToHourCycle, Temporal, getTimeZone, toDateUtil, toZonedDateTimeUtil, formatDateUtil, formatRangeUtil, subtractYearsUtil, subtractMonthsUtil, subtractWeeksUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, addYearsUtil, addMonthsUtil, addWeeksUtil, addDaysUtil, addHoursUtil, addMinutesUtil, startOfMonthUtil, startOfWeekUtil, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, endOfMonthUtil, endOfWeekUtil, endOfDayUtil, endOfHourUtil, endOfMinuteUtil, differenceInYearsUtil, differenceInMonthsUtil, differenceInWeeksUtil, differenceInDaysUtil, differenceInHoursUtil, differenceInMinutesUtil, differenceInSecondsUtil, isBetweenUtil, isTodayUtil, isFutureUtil, isPastUtil, isSameYearUtil, isSameMonthUtil, isSameWeekUtil, isSameDayUtil, timeSinceInYears, timeSinceInMonths, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInSeconds, timeSinceAuto, toDuration, getDurationFormat, convertMillisecondsUtil, convertSecondsUtil, convertMinutesUtil, convertHoursUtil, dayNameUtil, daysUtil, monthNameUtil, monthsUtil, getUTCFromTimeZonedUtil, isEqualUtil } from '@trackunit/date-and-time-utils';
3
3
  import { exhaustiveCheck } from '@trackunit/shared-utils';
4
+ import { useCurrentUserTimeFormat, useCurrentUserTimeZonePreference } from '@trackunit/react-core-hooks';
4
5
  import { TimeZonePreference } from '@trackunit/iris-app-runtime-core-api';
5
- import { useCurrentUserTimeZonePreference } from '@trackunit/react-core-hooks';
6
6
 
7
7
  const AssetTimezoneContext = createContext({ assetTimeZone: null });
8
8
  /** Returns the resolved asset timezone from the nearest AssetTimezoneProvider in the component tree. */
@@ -23,6 +23,23 @@ const convertToLocale = (userPreferenceLanguage) => {
23
23
  return userPreferenceLanguage;
24
24
  };
25
25
 
26
+ /**
27
+ * Resolves the hour cycle that should be forced based on the current user's time-format preference.
28
+ *
29
+ * Returns `undefined` when the user has chosen "let the language decide" (the default), so that
30
+ * locale-based formatting keeps deciding 12H/24H. Returns `"h12"` for a 12-hour preference and
31
+ * `"h23"` for a 24-hour preference.
32
+ *
33
+ * Pass the result to `formatDateUtil(..., hourCycleOverride)` (or use `useDateAndTime`, `DateTime`,
34
+ * and `DateTimeCell`, which already apply it) to make displayed times honor the preference.
35
+ *
36
+ * @returns {Intl.LocaleHourCycleKey | undefined} The forced hour cycle, or `undefined` to defer to the locale.
37
+ */
38
+ const useHourCycle = () => {
39
+ const { timeFormat } = useCurrentUserTimeFormat();
40
+ return useMemo(() => timeFormatToHourCycle(timeFormat), [timeFormat]);
41
+ };
42
+
26
43
  const LANG_STORAGE_KEY = "i18nextLng";
27
44
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
28
45
  const BrowserLocale = navigator.language ?? "en";
@@ -133,6 +150,7 @@ const useTimezone = () => {
133
150
  */
134
151
  const useDateAndTime = () => {
135
152
  const currentLocale = useLocale();
153
+ const hourCycle = useHourCycle();
136
154
  const { current, assetTimeZone, preferred } = useTimezone();
137
155
  const currentTimeZone = useMemo(() => current.id || preferred.id || assetTimeZone?.id, [current, assetTimeZone, preferred]);
138
156
  const getNowDate = useCallback(() => {
@@ -148,8 +166,8 @@ const useDateAndTime = () => {
148
166
  * @returns {string}
149
167
  */
150
168
  const formatDate = useCallback((date, format) => {
151
- return formatDateUtil(date, format, currentTimeZone, currentLocale);
152
- }, [currentLocale, currentTimeZone]);
169
+ return formatDateUtil(date, format, currentTimeZone, currentLocale, hourCycle);
170
+ }, [currentLocale, currentTimeZone, hourCycle]);
153
171
  /**
154
172
  * Formats a date using the custom locale and custom timezone.
155
173
  *
@@ -160,8 +178,8 @@ const useDateAndTime = () => {
160
178
  * @returns {string}
161
179
  */
162
180
  const formatDateWithTimezone = useCallback((date, format, timezone, locale) => {
163
- return formatDateUtil(date, format, timezone ?? currentTimeZone, locale ?? currentLocale);
164
- }, [currentLocale, currentTimeZone]);
181
+ return formatDateUtil(date, format, timezone ?? currentTimeZone, locale ?? currentLocale, hourCycle);
182
+ }, [currentLocale, currentTimeZone, hourCycle]);
165
183
  /**
166
184
  * Formats a date range using the current locale and time zone.
167
185
  *
@@ -513,4 +531,4 @@ const useDateAndTime = () => {
513
531
  ]);
514
532
  };
515
533
 
516
- export { AssetTimezoneContext, convertToLocale, useAssetTimezoneContext, useDateAndTime, useLocale, useTimezone };
534
+ export { AssetTimezoneContext, convertToLocale, useAssetTimezoneContext, useDateAndTime, useHourCycle, useLocale, useTimezone };
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@trackunit/react-date-and-time-hooks",
3
- "version": "2.1.16-alpha-9d327375fc1.0",
3
+ "version": "2.1.19",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
7
7
  "node": ">=24.x"
8
8
  },
9
9
  "dependencies": {
10
- "@trackunit/date-and-time-utils": "1.13.25-alpha-9d327375fc1.0",
11
- "@trackunit/iris-app-runtime-core-api": "1.16.25-alpha-9d327375fc1.0",
12
- "@trackunit/shared-utils": "1.15.24-alpha-9d327375fc1.0",
13
- "@trackunit/react-core-hooks": "1.17.29-alpha-9d327375fc1.0"
10
+ "@trackunit/date-and-time-utils": "1.13.28",
11
+ "@trackunit/iris-app-runtime-core-api": "1.16.28",
12
+ "@trackunit/shared-utils": "1.15.26",
13
+ "@trackunit/react-core-hooks": "1.17.32"
14
14
  },
15
15
  "devDependencies": {
16
- "@trackunit/react-core-contexts-test": "1.17.25-alpha-9d327375fc1.0"
16
+ "@trackunit/react-core-contexts-test": "1.17.28"
17
17
  },
18
18
  "peerDependencies": {
19
19
  "react": "^19.0.0"
package/src/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./AssetTimezoneContext";
2
2
  export * from "./localeUtils";
3
3
  export * from "./useDateAndTime";
4
+ export * from "./useHourCycle";
4
5
  export * from "./useLocale";
5
6
  export * from "./useTimezone";
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Resolves the hour cycle that should be forced based on the current user's time-format preference.
3
+ *
4
+ * Returns `undefined` when the user has chosen "let the language decide" (the default), so that
5
+ * locale-based formatting keeps deciding 12H/24H. Returns `"h12"` for a 12-hour preference and
6
+ * `"h23"` for a 24-hour preference.
7
+ *
8
+ * Pass the result to `formatDateUtil(..., hourCycleOverride)` (or use `useDateAndTime`, `DateTime`,
9
+ * and `DateTimeCell`, which already apply it) to make displayed times honor the preference.
10
+ *
11
+ * @returns {Intl.LocaleHourCycleKey | undefined} The forced hour cycle, or `undefined` to defer to the locale.
12
+ */
13
+ export declare const useHourCycle: () => Intl.LocaleHourCycleKey | undefined;