import assert from 'node:assert/strict'; import process from 'node:process'; import {describe, it} from 'mocha'; import tz from '../lib/tz-utils.js'; describe('unit: tz-utils', () => { it('validates IANA zone names', () => { assert.equal(tz.isValidIana('Europe/Berlin'), true); assert.equal(tz.isValidIana('Not/AZone'), false); }); it('parses local wall time with explicit offset', () => { const d = tz.parseWithOffset('20240101T120000', '+02:00'); assert.equal(d.toISOString(), '2024-01-01T10:00:00.000Z'); assert.equal(tz.parseWithOffset('00501001T120000', '+02:00').toISOString(), '0050-10-01T10:00:00.000Z'); }); it('throws on malformed offset strings', () => { assert.throws(() => tz.parseWithOffset('20240101T120000', 'bogus'), /Invalid offset string: bogus/v); }); it('parses local wall time within a named zone (standard time)', () => { // Europe/Berlin observes UTC+1 in January, so Intl-backed parsing should subtract one hour const d = tz.parseDateTimeInZone('20240101T120000', 'Europe/Berlin'); assert.equal(d.toISOString(), '2024-01-01T11:00:00.000Z'); }); it('rolls back a zero month or day in a named zone', () => { for (const [value, expected] of [ ['20271000T120000', '2027-09-30T10:00:00.000Z'], ['2027-10-00T12:00:00', '2027-09-30T10:00:00.000Z'], // Across a year, and into February with and without a leap day ['20270100T120000', '2026-12-31T11:00:00.000Z'], ['20270300T120000', '2027-02-28T11:00:00.000Z'], ['20280300T120000', '2028-02-29T11:00:00.000Z'], // A zero month keeps its day, and a zero day then goes back one more month ['20270015T120000', '2026-12-15T11:00:00.000Z'], ['20270000T120000', '2026-11-30T11:00:00.000Z'], // An early year stays, bar year 0, which would otherwise turn negative ['00501000T120000', '0050-09-30T11:06:32.000Z'], ['00000000T120000', '1899-11-30T11:00:00.000Z'], // Landing in a skipped or repeated hour resolves like any other time there ['20240400T023000', '2024-03-31T01:30:00.000Z'], ['20271100T023000', '2027-10-31T00:30:00.000Z'], ]) { assert.equal(tz.parseDateTimeInZone(value, 'Europe/Berlin').toISOString(), expected, value); } }); it('adds UTC-based durations without changing local semantics', () => { const start = new Date('2024-01-01T00:00:00.000Z'); const plusOneDay = tz.utcAdd(start, 1, 'days'); assert.equal(plusOneDay.toISOString(), '2024-01-02T00:00:00.000Z'); }); describe('DST transitions', () => { it('skips the missing hour during spring forward', () => { const berlin = 'Europe/Berlin'; const localGap = '20240331T023000'; const resolved = tz.parseDateTimeInZone(localGap, berlin); assert.ok(resolved instanceof Date); // 02:30 local never occurs on the DST start date, so we land on the next representable instant (03:30 local / 01:30Z) assert.equal(resolved.toISOString(), '2024-03-31T01:30:00.000Z'); assert.equal(resolved.tz, berlin); }); it('disambiguates the repeated hour during fall back', () => { const berlin = 'Europe/Berlin'; const ambiguous = '20241027T023000'; const resolved = tz.parseDateTimeInZone(ambiguous, berlin); assert.ok(resolved instanceof Date); // The first occurrence keeps the summer offset (UTC+2) so 02:30 local resolves to 00:30Z assert.equal(resolved.toISOString(), '2024-10-27T00:30:00.000Z'); assert.equal(resolved.tz, berlin); }); it('keeps UTC math consistent across the fall-back repetition', () => { const firstOccurrence = tz.parseWithOffset('20241027T023000', '+02:00'); const secondOccurrence = tz.parseWithOffset('20241027T023000', '+01:00'); assert.equal(firstOccurrence.toISOString(), '2024-10-27T00:30:00.000Z'); assert.equal(secondOccurrence.toISOString(), '2024-10-27T01:30:00.000Z'); assert.equal(tz.utcAdd(firstOccurrence, 1, 'hours').toISOString(), secondOccurrence.toISOString()); }); it('finds the later occurrence of a repeated wall time', () => { const berlin = 'Europe/Berlin'; const firstOccurrence = tz.parseDateTimeInZone('20241027T023000', berlin); const unambiguous = tz.parseDateTimeInZone('20241027T033000', berlin); // 02:30 local happens again at 01:30Z, once clocks fall back from UTC+2 to UTC+1 assert.equal(tz.findLaterOccurrence(firstOccurrence), Date.parse('2024-10-27T01:30:00.000Z')); assert.equal(tz.findLaterOccurrence(unambiguous), undefined); const withoutZone = new Date(firstOccurrence); assert.equal(tz.findLaterOccurrence(withoutZone), undefined, 'a date without a zone has nothing to repeat'); }); }); it('guesses a local zone string when TZ is set', () => { // Set process.env.TZ to a known valid IANA zone const zone = 'Europe/Berlin'; const oldTZ = process.env.TZ; process.env.TZ = zone; try { assert.equal(tz.guessLocalZone(), zone); assert.ok(tz.getZoneNames().includes(zone)); } finally { // Restore previous TZ if (oldTZ === undefined) { delete process.env.TZ; } else { process.env.TZ = oldTZ; } } }); describe('isUtcTimezone', () => { it('should return false for undefined/null/empty', () => { assert.equal(tz.__test__.isUtcTimezone(undefined), false); assert.equal(tz.__test__.isUtcTimezone(null), false); assert.equal(tz.__test__.isUtcTimezone(''), false); }); it('should return true for UTC timezones', () => { assert.equal(tz.__test__.isUtcTimezone('Etc/UTC'), true); assert.equal(tz.__test__.isUtcTimezone('UTC'), true); assert.equal(tz.__test__.isUtcTimezone('Etc/GMT'), true); }); it('should be case insensitive', () => { assert.equal(tz.__test__.isUtcTimezone('etc/utc'), true); assert.equal(tz.__test__.isUtcTimezone('utc'), true); assert.equal(tz.__test__.isUtcTimezone('ETC/GMT'), true); }); it('should return false for non-UTC timezones', () => { assert.equal(tz.__test__.isUtcTimezone('Europe/Berlin'), false); assert.equal(tz.__test__.isUtcTimezone('America/New_York'), false); assert.equal(tz.__test__.isUtcTimezone('Etc/GMT+1'), false); }); }); describe('resolveVTimezoneToIana', () => { // Simulates the parsed VTIMEZONE from bad_ms_tz.ics (EST/EDT equivalent) const estVTimezone = { type: 'VTIMEZONE', tzid: 'Customized Time Zone', 'abc-standard': { type: 'STANDARD', tzoffsetfrom: '-0400', tzoffsetto: '-0500', rrule: 'RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11', }, 'abc-daylight': { type: 'DAYLIGHT', tzoffsetfrom: '-0500', tzoffsetto: '-0400', rrule: 'RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3', }, }; it('resolves EST/EDT VTIMEZONE to a valid IANA zone', () => { const result = tz.resolveVTimezoneToIana(estVTimezone, 2020); assert.ok(result.iana, 'should resolve to an IANA zone'); assert.ok(tz.isValidIana(result.iana), `resolved zone ${result.iana} should be a valid IANA zone`); assert.equal(result.offset, '-05:00'); }); it('returns STANDARD offset as fallback for fixed-offset zones', () => { const fixedVTimezone = { type: 'VTIMEZONE', tzid: 'No DST Zone', 'abc-standard': { type: 'STANDARD', tzoffsetfrom: '+0530', tzoffsetto: '+0530', }, }; const result = tz.resolveVTimezoneToIana(fixedVTimezone, 2020); assert.equal(result.offset, '+05:30'); }); it('treats equal STANDARD and DAYLIGHT offsets as fixed-offset zones', () => { const equalOffsetVTimezone = { type: 'VTIMEZONE', tzid: 'Fixed Offset With DAYLIGHT', standard: { type: 'STANDARD', tzoffsetfrom: '+0100', tzoffsetto: '+0100', }, daylight: { type: 'DAYLIGHT', tzoffsetfrom: '+0100', tzoffsetto: '+0100', }, }; const result = tz.resolveVTimezoneToIana(equalOffsetVTimezone, 2020); assert.equal(result.offset, '+01:00'); }); it('returns CET/CEST VTIMEZONE as Europe/Berlin (or equivalent)', () => { const cetVTimezone = { type: 'VTIMEZONE', tzid: 'Customized Time Zone', standard: { type: 'STANDARD', tzoffsetfrom: '+0200', tzoffsetto: '+0100', }, daylight: { type: 'DAYLIGHT', tzoffsetfrom: '+0100', tzoffsetto: '+0200', }, }; const result = tz.resolveVTimezoneToIana(cetVTimezone, 2020); // Must be a valid CET/CEST IANA zone (could be Europe/Berlin, Europe/Paris, etc.) assert.ok(result.iana, 'should resolve to an IANA zone'); assert.equal(result.offset, '+01:00'); }); it('returns undefined for empty/missing vtimezone input', () => { assert.deepEqual(tz.resolveVTimezoneToIana(null, 2020), {iana: undefined, offset: undefined}); assert.deepEqual(tz.resolveVTimezoneToIana(undefined, 2020), {iana: undefined, offset: undefined}); assert.deepEqual(tz.resolveVTimezoneToIana({type: 'VTIMEZONE'}, 2020), {iana: undefined, offset: undefined}); }); it('does not crash when year is < 1000 (e.g. DTSTART:00010325T020000)', () => { // Regression test for https://github.com/jens-maus/node-ical/issues/495 // VTIMEZONE blocks like "W. Europe Standard Time" from emClient use DTSTART year 0001. // Temporal.Instant.from() requires a 4-digit ISO year; "1-01-15T12:00:00Z" is invalid. // // Note: The parser calls `new Date(1, ...)` for year 0001 which JS interprets as 1901, // but the `year` argument to resolveVTimezoneToIana comes from parseInt('0001') = 1. const cetVTimezone = { type: 'VTIMEZONE', tzid: 'W. Europe Standard Time', daylight: { type: 'DAYLIGHT', start: new Date(1, 2, 25, 2, 0, 0), // JS interprets as 1901 (matches real parser behavior) tzoffsetfrom: '+0100', tzoffsetto: '+0200', }, standard: { type: 'STANDARD', start: new Date(1, 9, 28, 3, 0, 0), // JS interprets as 1901 (matches real parser behavior) tzoffsetfrom: '+0200', tzoffsetto: '+0100', }, }; // Must not throw; should resolve to a valid CET/CEST IANA zone. // The key scenario: year=1 would produce "1-01-15T12:00:00Z" without the fix. const result = tz.resolveVTimezoneToIana(cetVTimezone, 1); assert.ok(result.iana, 'should resolve to an IANA zone despite year 0001 DTSTART'); assert.equal(result.offset, '+01:00'); }); }); });