Osvaldo Cordova A
Osvaldo Cordova A

Reputation: 45

How can i get the correct utc offset in javascript when the dst in mexico was permanently removed?

I am using the RRule library in JavaScript and I've noticed that when I retrieve a date range, it returns the date and time in UTC but it is incorrect. The code looks like this:

return new RRule(removeUndefinedFromObject({
   dtstart: utcToZonedTime(params.start, params.timezone),
   freq: RRule.DAILY,
   byweekday: params.weekdays.map((day) => weekdaysMap[day]),
   until: utcToZonedTime(params.end, params.timezone),
   tzid: params.timezone,
}));

When the start is 2023-04-01 12:00:00, the end is 2023-04-04 18:00:00 and the timezone America/Mexico_City.

I am expecting the following date ranges:

start_time end_time
2023-04-01 12:00:00 2023-04-01 18:00:00
2023-04-02 12:00:00 2023-04-02 18:00:00
2023-04-03 12:00:00 2023-04-03 18:00:00
2023-04-04 12:00:00 2023-04-04 18:00:00

But i am getting:

start_time end_time
2023-04-01 12:00:00 2023-04-01 18:00:00
2023-04-02 11:00:00 2023-04-02 17:00:00
2023-04-03 11:00:00 2023-04-03 17:00:00
2023-04-04 11:00:00 2023-04-04 17:00:00

The government of Mexico removed Daylight Saving Time in October 2022, so the time should remain constant. However, I am encountering different times in April due to the previous implementation of Daylight Saving Time. Does anyone know how to resolve this issue in JavaScript? I've encountered similar problems with other libraries, such as date-fns."

Update: The problem was the version of nodejs, i was using nodejs 14.x but after updating to 18.x it is working as expected.

Upvotes: 0

Views: 787

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241728

Both RRule and date-fns-tz take their data from Intl in JavaScript, so you'll get whatever time zone data is provided by the runtime environment.

The change you mentioned for Mexico was released in IANA 2022f, October 29, 2022. Assuming you're running server-side in Node.js, simply update Node to the current release. You'll find tzdata 2022f mentioned in the changelog for Node.js 18.13.0 (LTS) and also 19.2.0 (current).

Upvotes: 2

Related Questions