Skip to content

Commit 1f35627

Browse files
authored
fix: allow user to reset timezone (#8054)
* wip * bump actions * use system time zone
1 parent 15c52bc commit 1f35627

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

packages/@internationalized/date/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export {
6363
today,
6464
getHoursInDay,
6565
getLocalTimeZone,
66+
setLocalTimeZone,
67+
resetLocalTimeZone,
6668
startOfMonth,
6769
startOfWeek,
6870
startOfYear,

packages/@internationalized/date/src/queries.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,23 @@ let localTimeZone: string | null = null;
133133

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

141140
return localTimeZone!;
142141
}
143142

143+
/** Sets the time zone identifier for the current user. */
144+
export function setLocalTimeZone(timeZone: string): void {
145+
localTimeZone = timeZone;
146+
}
147+
148+
/** Resets the time zone identifier for the current user. */
149+
export function resetLocalTimeZone(): void {
150+
localTimeZone = null;
151+
}
152+
144153
/** Returns the first date of the month for the given date. */
145154
export function startOfMonth(date: ZonedDateTime): ZonedDateTime;
146155
export function startOfMonth(date: CalendarDateTime): CalendarDateTime;

packages/@internationalized/date/tests/queries.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
endOfYear,
1818
EthiopicCalendar,
1919
getDayOfWeek,
20+
getLocalTimeZone,
2021
getMinimumDayInMonth,
2122
getMinimumMonthInYear,
2223
getWeeksInMonth,
@@ -31,6 +32,8 @@ import {
3132
maxDate,
3233
minDate,
3334
PersianCalendar,
35+
resetLocalTimeZone,
36+
setLocalTimeZone,
3437
startOfMonth,
3538
startOfWeek,
3639
startOfYear,
@@ -343,4 +346,15 @@ describe('queries', function () {
343346
expect(b.compare(a)).toBeGreaterThan(0);
344347
});
345348
});
349+
350+
describe('getLocalTimeZone', function () {
351+
it('gets local time zone', function () {
352+
const systemTimeZone = new Intl.DateTimeFormat().resolvedOptions().timeZone;
353+
expect(getLocalTimeZone()).toBe(systemTimeZone);
354+
setLocalTimeZone('America/Denver');
355+
expect(getLocalTimeZone()).toBe('America/Denver');
356+
resetLocalTimeZone();
357+
expect(getLocalTimeZone()).toBe(systemTimeZone);
358+
});
359+
});
346360
});

0 commit comments

Comments
 (0)