mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-07 19:30:14 +00:00
* 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>
46 lines
1.8 KiB
TypeScript
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;
|
|
}
|