Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using clone() and add() on a date in the America/Santiago timezone, where a daylight saving time change occurs. #1074

Open
Jofemago opened this issue Sep 7, 2023 · 1 comment
Labels

Comments

@Jofemago
Copy link

Jofemago commented Sep 7, 2023

Describe the bug

When we take the exact moment when the timezone change occurs, Moment Timezone advances us one hour forward due to the timezone change. In the week 2023-09-03 to 2023-09-09, in Chile, on the date 2023-09-03, the timezone change occurs. If we stand on any date this week and perform a start of week operation, it will return the time 2023-09-03 but at 00:00 hours. If we clone this time, the clone moves it one hour forward, causing issues when we use the add function and convert it to UTC, generating incorrect dates. However, if we apply start of week after cloning, the problem is resolved, and it correctly processes the additions we want to make but from week 1 onwards. If we want to do it in the same week (week 0), it becomes misaligned when converted to UTC.

To Reproduce
Steps to reproduce the behavior:

  1. Create a file named script.ts.
  2. Paste the code written below.
import * as moment from "moment-timezone";

const dateString = "2023-09-09T00:00:00";

//Time when the daylight saving time change occurs.
console.log( moment.tz( "2023-09-03T00:00:00", "America/Santiago"))

//Date in timezone America/Santiago -3
let date = moment.tz(dateString, "America/Santiago");

//Date when the daylight saving time change occurs getting with a start of week
console.log(date.startOf("week"))

//at this point the start is applied
// Clone the date and see how it modifies the value due to the timezone change.
console.log(date.clone());

//clone the date and apply the start of week
console.log(date.clone().startOf('week'))

//add 0 week and 3 days and 8 hours without apply start of week before clone
console.log(date.clone().add(0, "week").add(3, "day").add(8, "hour"));
console.log(date.clone().add(0, "week").add(3, "day").add(8, "hour").utc());//utc

//add 0 week and 3 days and 8 hours with start of week applied before clone
console.log(date.clone().startOf("week").add(0, "week").add(3, "day").add(8, "hour"));
console.log(date.clone().startOf("week").add(0, "week").add(3, "day").add(8, "hour").utc());//utc


//add 1 week and 3 days and 8 hours without apply start of week before clone
console.log(date.clone().add(1, "week").add(3, "day").add().add(8, "hour"));
console.log(date.clone().add(1, "week").add(3, "day").add().add(8, "hour").utc());//utc

//add 1 week and 3 days and 8 hours with start of week applied before clone
console.log(date.clone().startOf("week").add(1, "week").add(3, "day").add(8, "hour"));
console.log(date.clone().startOf("week").add(1, "week").add(3, "day").add(8, "hour").utc());//utc
  1. Validate this output (it should be the same as long as the bug exists).
Moment<2023-09-03T01:00:00-03:00>
Moment<2023-09-03T00:00:00-04:00>
Moment<2023-09-03T01:00:00-03:00>
Moment<2023-09-03T00:00:00-04:00>
Moment<2023-09-06T09:00:00-03:00>
Moment<2023-09-06T12:00:00Z>
Moment<2023-09-06T09:00:00-03:00>
Moment<2023-09-06T12:00:00Z>
Moment<2023-09-13T09:00:00-03:00>
Moment<2023-09-13T12:00:00Z>
Moment<2023-09-13T08:00:00-03:00>
Moment<2023-09-13T11:00:00Z>
  1. Notice that when you use clone after using a start of week, it changes the hour and moves it one hour forward. This causes it to always be one hour ahead, both in the specific timezone and in UTC, when you add weeks, days, and hours.
  2. "Note that when you apply a start of the week to the cloned time, it removes the offset. However, if you use add to move within the same week, the offset persists. But if you move to the next week using add(1, "week"), it works correctly and delivers the time correctly."

Expected behavior

  1. What you should expect is that when you clone the time, the hour should retain exactly the state it had, even at a moment when a timezone change occurs.
  2. That when I use an 'add' from a date where the timezone change occurs, it generates the jumps correctly using 'add,' not as it happens in the code from the previous point.

Screenshots

Look, in Chile, during these dates, 8 am is 11 am in UTC. However, due to the clone issue, it's delivering it at 12 am.
image

I've attached an image and link of the daylight saving time changes.

image

Desktop (please complete the following information):

  • OS: Ubuntu 22.04.3 LTS
  • Node JS app
  • Node v18.1.0
  • moment "^2.29.4"
  • moment-timezone "0.5.43"

Moment-specific environment

  • America/Bogota
  • 2023/09/07
  • typescript

Please run the following code in your environment and include the output:

Thu Sep 07 2023 18:21:01 GMT-0500 (Colombia Standard Time)
9/7/2023, 6:21:01 PM
300
// console.log(navigator.userAgent)
2.29.4

Additional context
I only tested it with this specific timezone change; it was not tested with another timezone change elsewhere in the world or within Chile.

@gilmoreorless gilmoreorless transferred this issue from moment/moment Sep 9, 2023
@gilmoreorless
Copy link
Member

I haven't looked into this properly, but at first glance it looks a lot like similar issues such as #1017 and #409.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants