From 0245e40951780a7bce43ce9a0a97baa7e583f5c6 Mon Sep 17 00:00:00 2001 From: mori-hisayuki <37867997+mori-hisayuki@users.noreply.github.com> Date: Thu, 7 Mar 2024 11:33:18 +0900 Subject: [PATCH] fix: unknown tz (#34) * chore: add comment and type * fix: This code ensures that if tz is undefined, the toLowerCase function is not called. --- src/deviceTZ.ts | 4 +++- src/format.ts | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/deviceTZ.ts b/src/deviceTZ.ts index 3358cc7..56a6311 100644 --- a/src/deviceTZ.ts +++ b/src/deviceTZ.ts @@ -1,6 +1,8 @@ /** * Get the timezone of the device. + * + * * Note: If the environment variable TZ is not set, it will return undefined. */ -export function deviceTZ() { +export function deviceTZ(): string | undefined { return Intl.DateTimeFormat().resolvedOptions().timeZone } diff --git a/src/format.ts b/src/format.ts index f19f54c..475689a 100644 --- a/src/format.ts +++ b/src/format.ts @@ -77,7 +77,7 @@ export function format( // We need to apply an offset to the date so that it can be formatted as UTC. tz ??= deviceTZ() - if (tz.toLowerCase() !== "utc") { + if (tz?.toLowerCase() !== "utc") { inputDateOrOptions = removeOffset( inputDateOrOptions, offset(inputDateOrOptions, tz, "utc")