mattermost-mobile-upstream/app/utils/timezone.ts
Scott Bishel 4a11b7c956
Some checks are pending
ci / lint-typecheck (push) Waiting to run
ci / detox-lint-typecheck (push) Waiting to run
ci / test (push) Waiting to run
FIX: Correct Android minute interval and UTC+offset date display in datetime fields (#10202)
* fix android datetime issues

* fix android date/time tests

* lint fix, review fix

* fix tests

* more tests

* another test fix

* fixing test

* fix tests

* fix tests

* fix tests

* fix tests

* remove test

* make test use android explicitly

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
2026-10-22 15:12:27 -05:00

46 lines
1.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {getTimeZone} from 'react-native-localize';
import {logDebug} from '@utils/log';
/** Returns the raw IANA timezone string reported by the device OS. */
export function getDeviceTimezone() {
return getTimeZone();
}
let lastDeviceTimezone: string | undefined;
let lastSupportedTimezone: string | undefined;
/**
* The device timezone, but only when Intl can actually format with it.
*
* Callers pass this to Intl.DateTimeFormat instead of leaving `timeZone` undefined:
* Hermes does not resolve an absent/undefined `timeZone` to the device zone, it formats
* in UTC, which renders the previous day for any local-midnight value on a device at a
* positive UTC offset. Returns undefined when the platform reports a zone Intl rejects,
* so callers keep their previous behaviour rather than throwing on every render.
*/
export function getSupportedDeviceTimezone(): string | undefined {
try {
const deviceTimezone = getTimeZone();
if (deviceTimezone !== lastDeviceTimezone) {
lastDeviceTimezone = deviceTimezone;
try {
// Throws a RangeError for a zone Intl cannot use; the resolved value is the
// canonical name for the one the platform reported.
lastSupportedTimezone = new Intl.DateTimeFormat('en', {timeZone: deviceTimezone}).resolvedOptions().timeZone;
} catch {
logDebug('getSupportedDeviceTimezone: Intl rejected device timezone, falling back to undefined', deviceTimezone);
lastSupportedTimezone = undefined;
}
}
} catch {
// getTimeZone() native call failed — return whatever was last cached (or undefined).
}
return lastSupportedTimezone;
}