Why So Many IANA Time Zones Names?

后端 未结 1 1117
萌比男神i
萌比男神i 2021-01-02 07:38

Javascript allows you to see what time it is in another timezone if you specify the IANA given name of that timezone. For example:

new Date().toLocaleString(         


        
1条回答
  •  遥遥无期
    2021-01-02 08:26

    I'll use your example:

    For example, both America/Detroit and America/New_York are in the Eastern Time Zone. Why don't these two locations share a single timezone name?

    In the TZDB, the Zone entry for America/New_York looks like this:

    # Zone  NAME              GMTOFF    RULES   FORMAT   [UNTIL]
    Zone    America/New_York  -4:56:02  -       LMT      1883 Nov 18 12:03:58
                              -5:00     US      E%sT     1920
                              -5:00     NYC     E%sT     1942
                              -5:00     US      E%sT     1946
                              -5:00     NYC     E%sT     1967
                              -5:00     US      E%sT
    

    While the Zone entry for America/Detroit looks like this:

    # Zone  NAME              GMTOFF    RULES   FORMAT   [UNTIL]
    Zone    America/Detroit   -5:32:11  -       LMT      1905
                              -6:00     -       CST      1915 May 15  2:00
                              -5:00     -       EST      1942
                              -5:00     US      E%sT     1946
                              -5:00     Detroit E%sT     1973
                              -5:00     US      E%sT     1975
                              -5:00     -       EST      1975 Apr 27  2:00
                              -5:00     US      E%sT
    

    To fully decipher this, one also needs the Rule entries for US, NYC, and Detroit (which I won't copy/paste here, but you can follow the links).

    As you can see, Detroit has had variations from New York, the last of which was in 1975 when Detroit started daylight saving time slightly later than most of the Eastern time zone (Apr 27 shown here vs Feb 23rd given by Rule US).

    Since then however, they have been the same. The TZDB rules require a unique zone for areas that have agreed since 1970, and these areas have deviations in 1973 and 1975, thus they require unique zone identifiers.

    One can see this difference in JavaScript like so:

    var d = new Date("1975-03-01T00:00:00.000Z"); // Midnight UTC on March 1st
    d.toLocaleString("en-US", {timeZone: "America/New_York"})  //=> "2/28/1975, 8:00:00 PM"
    d.toLocaleString("en-US", {timeZone: "America/Detroit"})   //=> "2/28/1975, 7:00:00 PM"
    

    Of course, if in your application, you never deal with dates going back that far, then you can just use America/New_York to represent the US Eastern time zone, and omit America/Detroit (and a few others) - but this is entirely your decision to make.

    You may also be interested in reading the Theory file with in the tzdb itself, which explains the concepts and principles of the time zone database in a lot more detail.

    0 讨论(0)
提交回复
热议问题