moment / moment-timezone

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

The DST calculation does not take effect. #1077

Closed fengnian7 closed 7 months ago

fengnian7 commented 10 months ago

Hello, excuse me. We are using the latest version 0.5.43, which was released on March 31st. Since April 17th, Egypt has re-implemented DST. We find that the software cannot correctly calculate the DST in this region. Is it because the time zone database used by the software is not updated? Do you have an update plan later? We look forward to your reply. Thank you very much.

gilmoreorless commented 10 months ago

Egypt's DST actually started on April 28 (see https://www.timeanddate.com/time/change/egypt), and this change was implemented in version 0.5.42: https://github.com/moment/moment-timezone/pull/1047

Do you have an example of some code that isn't working for you?

fengnian7 commented 10 months ago

Do you have an example of some code that isn't working for you?

Thank you for your reply. I do see that 0.5.42 has considered the restart of DST in Egypt. Unfortunately, our DST didn't work, so I'll show the code below. The following is a code snippet. The timestamp variable indicates the timestamp returned from the server, the formation variable indicates the time format displayed on the foreground page, and the timezone variable indicates the current time zone. Any questions can be answered, I will answer the first time, thank you again.

let date = moment(timestamp).format(formation);
 let isDST:boolean;
  if (moment(timestamp).tz(timezone)) {
    isDST = moment(timestamp).tz(timezone).isDST();
    if (isDST) {
      if (dstOffset === '0') {
        const offset = moment.tz(timezone)._offset;
        const timeZone = parseOffset(offset / 60 - 1);
        const timeSplit = date.split(' ');
        date = `${timeSplit[0]} ${timeSplit[1]} ${timeZone}`;
      }
      date = `${date} DST`;
    }
  }
gilmoreorless commented 10 months ago

It's hard to tell what the problem would be without any example inputs or outputs. However, there are a couple of things I noticed:

  1. Don't use any properties prefixed with an underscore (._offset). These are internal properties that are not part of the official API, and could change without warning. In this case, you should use .utcOffset().
  2. The line const offset = moment.tz(timezone)._offset; is not using timestamp as an input. That means it will use the current date/time, which might have a different offset from the input timestamp, giving the wrong value for the formatted date.