Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/@internationalized/date/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export {
today,
getHoursInDay,
getLocalTimeZone,
setLocalTimeZone,
resetLocalTimeZone,
startOfMonth,
startOfWeek,
startOfYear,
Expand Down
11 changes: 10 additions & 1 deletion packages/@internationalized/date/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,23 @@ let localTimeZone: string | null = null;

/** Returns the time zone identifier for the current user. */
export function getLocalTimeZone(): string {
// TODO: invalidate this somehow?
if (localTimeZone == null) {
localTimeZone = new Intl.DateTimeFormat().resolvedOptions().timeZone;
}

return localTimeZone!;
}

/** Sets the time zone identifier for the current user. */
export function setLocalTimeZone(timeZone: string): void {
localTimeZone = timeZone;
}

/** Resets the time zone identifier for the current user. */
export function resetLocalTimeZone(): void {
localTimeZone = null;
}

/** Returns the first date of the month for the given date. */
export function startOfMonth(date: ZonedDateTime): ZonedDateTime;
export function startOfMonth(date: CalendarDateTime): CalendarDateTime;
Expand Down
14 changes: 14 additions & 0 deletions packages/@internationalized/date/tests/queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
endOfYear,
EthiopicCalendar,
getDayOfWeek,
getLocalTimeZone,
getMinimumDayInMonth,
getMinimumMonthInYear,
getWeeksInMonth,
Expand All @@ -31,6 +32,8 @@ import {
maxDate,
minDate,
PersianCalendar,
resetLocalTimeZone,
setLocalTimeZone,
startOfMonth,
startOfWeek,
startOfYear,
Expand Down Expand Up @@ -343,4 +346,15 @@ describe('queries', function () {
expect(b.compare(a)).toBeGreaterThan(0);
});
});

describe('getLocalTimeZone', function () {
it('gets local time zone', function () {
const systemTimeZone = new Intl.DateTimeFormat().resolvedOptions().timeZone;
expect(getLocalTimeZone()).toBe(systemTimeZone);
setLocalTimeZone('America/Denver');
expect(getLocalTimeZone()).toBe('America/Denver');
resetLocalTimeZone();
expect(getLocalTimeZone()).toBe(systemTimeZone);
});
});
});