Skip to content

Commit def8b55

Browse files
committed
Fix timezone offset range and month boundary in isValid
- Remove local timezone dependency in getLastDayOfMonth by using Date.UTC - Expand timezone offset (Z token) valid range from -840~720 to -913~956 to support historical timezone offsets (e.g., Metlakatla, Manila) - Add test cases for min (America/Metlakatla) and max (Asia/Manila) offsets
1 parent c54dc6e commit def8b55

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/isValid.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ParserOptions } from './parser.ts';
44
import type { PreparseResult } from './preparse.ts';
55

66
const getLastDayOfMonth = (year: number, month: number) => {
7-
return new Date(year, month - (year < 100 ? 1900 * 12 : 0), 0).getDate();
7+
return new Date(Date.UTC(year, month - (year < 100 ? 1900 * 12 : 0), 0)).getUTCDate();
88
};
99

1010
/**
@@ -32,7 +32,7 @@ export function validatePreparseResult(pr: PreparseResult, options?: ParserOptio
3232
&& range(pr.m, 0, 59)
3333
&& range(pr.s, 0, 59)
3434
&& range(pr.S, 0, 999)
35-
&& range(pr.Z, -840, 720);
35+
&& range(pr.Z, -913, 956);
3636
}
3737

3838
/**

tests/isValid.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test, beforeAll } from 'vitest';
2-
import { isValid, compile } from '@/index.ts';
2+
import { isValid, compile, format } from '@/index.ts';
33
import { parser } from '@/plugins/day-of-week.ts';
44

55
beforeAll(() => (process.env.TZ = 'UTC'));
@@ -137,4 +137,16 @@ describe('options', () => {
137137
expect(isValid('0544-01-01 12:00 AM', 'YYYY-MM-DD hh:mm A', { calendar: 'buddhist' })).toBe(true);
138138
expect(isValid('0543-01-01 12:00 AM', 'YYYY-MM-DD hh:mm A', { calendar: 'buddhist' })).toBe(false);
139139
});
140+
141+
test('timeZone: min', () => {
142+
process.env.TZ = 'America/Metlakatla';
143+
const dateString = format(new Date(-3225223728000), 'YYYY-MM-DD[T]HH:mm:ss ZZ');
144+
expect(isValid(dateString, 'YYYY-MM-DD[T]HH:mm:ss ZZ')).toBe(true);
145+
});
146+
147+
test('timeZone: max', () => {
148+
process.env.TZ = 'Asia/Manila';
149+
const dateString = format(new Date(-3944621033000), 'YYYY-MM-DD[T]HH:mm:ss ZZ');
150+
expect(isValid(dateString, 'YYYY-MM-DD[T]HH:mm:ss ZZ')).toBe(true);
151+
});
140152
});

0 commit comments

Comments
 (0)