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

Problem with tz methods and daylight change in America #1042

Closed
adrianmarinwork opened this issue Mar 16, 2023 · 17 comments
Closed

Problem with tz methods and daylight change in America #1042

adrianmarinwork opened this issue Mar 16, 2023 · 17 comments

Comments

@adrianmarinwork
Copy link

adrianmarinwork commented Mar 16, 2023

Moment version which you use:

Version: "moment": "^2.29.4",

Moment-timezone version which you use:

Version: "moment-timezone": "^0.5.41"

Issue description:

Hello, this past 12 of March in America they changed from -5 to -4 because of the daylight change.

Looks like when I run moment.tz(date, timezone) or moment().tz(timezone) it's still using the old offset (-5) and the date returned is wrong.

As an example, I'm from Spain and I've set my computer timezone to New York. As you will see in the screenshots, when I create the date with moment, it's using the correct offset, but when I use one of the methods mentioned above, it changes to the old offset, returning me a date that I don't want.

Screenshot 2023-03-16 at 10 21 38

Screenshot 2023-03-16 at 10 23 53

Do you know If I'm missing something or a way to fix this?
Thank you very much!

@gilmoreorless
Copy link
Member

I'm having trouble reproducing this, but I'm probably just missing some context. To clarify:

  1. Your computer is set to America/New_York time zone, then you're also converting the moment objects to that same zone. Is that correct, or have I misread your post?
  2. How are you creating the original moment objects? Other bug reports have shown there's slightly different (buggy) behaviour depending on how the original object is created (e.g. startDate = moment() vs startDate = moment(someString) vs startDate = moment(dateObject) vs startDate = moment.tz(date, zoneName))

@adrianmarinwork
Copy link
Author

adrianmarinwork commented Mar 19, 2023

Thank you for answering @gilmoreorless!!!

Yeah thats right, for me to test, it's easier if I move to the timezone that is having problems, so yeah, I'm basically changing the moment to the same timezone that I am.
I'm in America/New_York and I'm converting the moment objects to the America/New_York timezone.

About the second question, I'm creating the original moment from the object in the screenshot doing this: moment(objectInTheScreenshot).clone().
Let me do some tests to check If I'm having the same problem by creating the startDate with different methods as you mentioned.

Screenshot 2023-03-19 at 03 23 08

@adrianmarinwork
Copy link
Author

Okay I've created the different test to see how it's the behaviour between the way of creating the original moment.
I've probably done it wrong or I'm missing something because I've done it fast, but as you can see in the screenshot, except the last one (example created directly with the timezone) the others examples are using the timezone of my computer and they have an offset of -4, but when I apply the timezone America/New_York, the offset changes to -5.

Screenshot 2023-03-19 at 03 36 05

Screenshot 2023-03-19 at 03 36 16

@gilmoreorless
Copy link
Member

Hrm that's interesting. I've tried those examples and still get -04:00 for all of them. I know sometimes there can be weird calculations when something else is interfering with the environment.

To help diagnose this, can you give me the output of these 3 values?

moment.version;
moment.tz.version;
moment.tz.dataVersion;

I know you gave the version definitions in the opening description, but dependency weirdness doesn't always produce what we expect (here's a good example). Also, how are you including moment-timezone in your project? (e.g. package manager, CDN script tag, etc)

@adrianmarinwork
Copy link
Author

adrianmarinwork commented Mar 19, 2023

Here you go, looks like the problem could be coming from the moment.tz.version and the moment.tz.dataVersion, but how is it that those two versions are pretty old when I've installed the last version of moment-timezone via NPM?

Screenshot 2023-03-19 at 17 25 19

Screenshot 2023-03-19 at 17 26 32

@adrianmarinwork
Copy link
Author

adrianmarinwork commented Mar 19, 2023

I've read the example that you linked in the message above, and I've run the npm ls moment command, this are the packages that use moment and all have the same version.

Screenshot 2023-03-19 at 17 31 23

@gilmoreorless
Copy link
Member

That's odd — your npm ls output is showing moment-timezone@0.5.41, but moment.tz.version is showing 0.5.14. I wonder if there's a second (older) copy of moment-timezone being brought in by another dependency, and that's what's being included? npm ls moment-timezone might show the culprit.

@adrianmarinwork
Copy link
Author

adrianmarinwork commented Mar 20, 2023

Looks like the only version that I've installed of moment-timezone is the 0.5.41. This is the only moment-timezone that I have, no other package has dependency on moment-timezone.

Screenshot 2023-03-20 at 08 04 03

@adrianmarinwork
Copy link
Author

Heeeeeeeey @gilmoreorless, I managed to update the moment.tz.version and moment.tz.dataVersion.

To add context, my application runs in Meteor framework, so I decided to do the next to see if it could fix the issue, it's probably no the best solution but it worked 😄.
I uninstalled moment and moment-timezone from my project with the following commands:

npm uninstall moment
npm uninstall moment-timezone
meteor npm uninstall moment-timezone
meteor npm uninstall moment

After that I installed moment-timezone using the following command:
meteor npm install moment-timezone

As moment-timezone adds moment as a dependency, I have not installed the moment package.

Now if I run npm ls moment, npm ls moment-timezone and the three variables this is how it looks like (I'm also adding screenshots of the package.json and package-lock.json:

Screenshot 2023-03-20 at 03 44 07

Screenshot 2023-03-20 at 03 44 15

Screenshot 2023-03-20 at 03 44 40

Screenshot 2023-03-20 at 03 44 53

Screenshot 2023-03-20 at 03 45 10

@adrianmarinwork
Copy link
Author

I've reverted the changes to do more testing and see where the problem was and looks like it was that npm install moment-timezone updated my package.json and package-lock.json and was changing the versions installed but wasn't really taking effect in the Meteor version installed or something like that.

Because if I run meteor npm install moment-timezone now it shows the correct moment.tz.version and moment.tz.dataVersion. Things in how Meteor works I suppose.

If I apply this change except of the one I sent in the message above, the thing is that I have installed moment with a different version, what do you think is the best solution? I prefer the first one (getting rid of everything and install moment-timezone) in the future will give less problems I think.

Screenshot 2023-03-20 at 04 04 57

Screenshot 2023-03-20 at 04 05 02

@gilmoreorless
Copy link
Member

Ah good to see you found the issue! Dependency problems are inconsistent in appearance, but consistently annoying. 😆

I haven't used Meteor before, so I'm not sure how its internal dependency mechanism works. Something to note, though, is that we're planning to remove the automatic dependency on moment at some point. That would you'll have to add moment manually anyway.

@adrianmarinwork
Copy link
Author

Perfect, I'll install moment then.

Thank you very much for everything @gilmoreorless.
Pretty professional.

@adrianmarinwork
Copy link
Author

Just to add if someone with the same problem comes here, in order that this fix works, you have to install moment-timezone before installing moment, for some reason when you install moment first the moment.tz.version and moment.tz.dataVersion are the old ones.

If you install first moment-timezone it works fine.

@leo-li-ls
Copy link

leo-li-ls commented Mar 29, 2023

Just to add if someone with the same problem comes here, in order that this fix works, you have to install moment-timezone before installing moment, for some reason when you install moment first the moment.tz.version and moment.tz.dataVersion are the old ones.

If you install first moment-timezone it works fine.

Hi @adrianmarinwork, I have the same issue and tried your solution, but it doesn't help.

I found a temporary solution:

// Replace this code below
const wrongDatetime = moment.tz(timeZone);

// By this one
const currentTime = new Date().toLocaleString("en-US", {timeZone});
const currentDate = new Date(currentTime);
const correctDatetime = moment(currentDate.toISOString());

console.log({
    wrongDatetime,
    correctDatetime
});

It will output:

{
    "wrongDatetime": "2023-03-27T13:56:09-05:00",
    "correctDatetime": "2023-03-27T14:56:09-04:00"
}

@sslincoco
Copy link

sslincoco commented Mar 29, 2023 via email

@washu
Copy link

washu commented Apr 4, 2023

this isnt working for me.

@adrianmarinwork
Copy link
Author

Right now I'm using the 0.5.41 version of moment-timezone and the last version of moment, I tried to update my moment-timezone to the version 0.5.42 and it's true that the fix that we mentioned in this thread does not work. That's why I haven't updated my moment-timezone, I'm not sure what could be happening tbh.

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

No branches or pull requests

5 participants