Skip to content

parseAbsolute and parseZonedDateTime apply minutes of negative UTC offsets in the wrong direction #10809

Open

Description

@sebxt

Summary

parseAbsolute returns the wrong instant for any negative UTC offset that has a minutes component, such as -03:30 (Newfoundland standard time) or -09:30 (Marquesas). parseAbsolute and parseZonedDateTime also drop the sign of offsets whose hour part is zero, such as -00:30 or the historical -00:44:30 used by Africa/Monrovia.

Expected Behavior

parseAbsolute('2021-11-07T07:45:00-03:30', 'UTC') returns 2021-11-07T11:15:00+00:00[UTC], the same instant as new Date('2021-11-07T07:45:00-03:30').

parseZonedDateTime(date.toString()) round trips a ZonedDateTime whose offset is -00:44:30.

Current Behavior

import {parseAbsolute, parseZonedDateTime, ZonedDateTime} from '@internationalized/date';

parseAbsolute('2021-11-07T07:45:00-03:30', 'UTC').toString();
// '2021-11-07T10:15:00+00:00[UTC]'  (one hour early; Date gives 11:15Z)

parseAbsolute('2021-11-07T07:45:00-00:30', 'UTC').toString();
// '2021-11-07T07:15:00+00:00[UTC]'  (offset treated as +00:30; Date gives 08:15Z)

let date = new ZonedDateTime(1960, 1, 1, 'Africa/Monrovia', -2670000, 0, 0, 0);
date.toString();          // '1960-01-01T00:00:00-00:44:30[Africa/Monrovia]'
parseZonedDateTime(date.toString());
// Error: Offset +00:00 is invalid for 1960-01-01T00:00:00 in Africa/Monrovia

Root cause

In packages/@internationalized/date/src/string.ts:

parseAbsolute computes the offset as hours * 3600000 + minutes * 60000. For -03:30 the hours are -3 and the minutes are added as positive, giving -02:30.

parseZonedDateTime multiplies by Math.sign(hourOffset), which handles -03:30 but is 0 (or -0) when the hour part is -00, so the minutes and seconds lose their sign.

Possible Solution

Take the sign from the first character of the hour string and apply it to the whole offset, in one helper used by both functions.

Version

@internationalized/date at main (2613443).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions