moment / moment-timezone

Timezone support for moment.js
momentjs.com/timezone
MIT License
3.82k stars 835 forks source link

Diffrent output in local and prod #1064

Closed dimfu closed 1 year ago

dimfu commented 1 year ago

Environment

For bug reports, please run the following code in your environment and include the output:

// Local
{
  moment: '2.29.4',
  momentTz: '0.5.43',
  date: 'Wed May 24 2023 12:29:40 GMT+0800 (Waktu Indonesia Tengah)',
  intlZone: 'Asia/Makassar'
}

// Prod
{
  moment: '2.29.4',
  momentTz: '0.5.43',
  date: 'Wed May 24 2023 04:39:12 GMT+0000 (Coordinated Universal Time)',
  intlZone: 'UTC'
}

Issue description

I expect the production output to be similar to that of the local, I'm stuck at this problem currently any help would be very nice. Thanks

const startDate = 'Friday, April 7, 2023 7:00AM'
const timezone = 'America/Los_Angeles'
return moment.tz(`${startDate} ${time}`, 'dddd, MMMM D, YYYY h:mmA', timezone).format()

Local output: 2023-04-07T07:00:00-07:00 Production output: 2023-04-07T07:00:00Z

gilmoreorless commented 1 year ago

Your prod environment is reporting its time zone as UTC, and the resulting call to moment.tz().format() is also returning in UTC. I wonder if there's a problem loading the data for Moment Timezone in the prod environment.

It's hard to diagnose a problem like this without knowing the project details, but here are some things you could look into:

  1. Is the data definitely loading in prod? Try other time zones as well. If they all produce UTC time, the data might not be loading correctly.
  2. Are you using moment-timezone on the server in something like NodeJS, or in the browser? The data loading path can be different between them.
  3. Are you using any kind of framework or bundling tool? They can sometimes mess up the loading of other modules or JSON files.
dimfu commented 1 year ago

I am indeed using moment-timezone on the server as a pre-build script that generate JSON file for my Next JS application deployed on Vercel, if you interested on looking here is the code for it. Currently I had to run the script locally to get the correct time.

If you have any suggestions or advice on how I could run the script on the server and get the correct time, I would greatly appreciate it. Thank you.

gilmoreorless commented 1 year ago

Hmmm, I'm not really familiar with Vercel's specific setup. But I do know that most of these code platforms will default the servers to UTC, and possibly allow setting a TZ environment variable to change the server's time zone.

I think the bigger question is why parsing a date as being in America/Los_Angeles returns the Z indicator for UTC when it's formatted. To me that looks like the time zone data file hasn't loaded correctly. Are you able to check the logs for a message like Moment Timezone has no data for America/Los_Angeles?

dimfu commented 1 year ago

I now understand that the problem I encountered was not caused by the moment-timezone library. After conducting a thorough investigation, I came to the realization that the root cause was a silly error. This API was responsible for retrieving the timezone name, but it was returning a 400 error due to an incorrect API key in production. I apologize for any misunderstanding and for taking up your valuable time on this matter.