Identifying time zones in ISO 8601

后端 未结 2 1196
醉梦人生
醉梦人生 2020-12-30 19:49

No, I\'m not talking about zone offsets --- those can vary during the year for a region based on e.g. DST. I\'m talking about the actual time zones maintained by IANA. I und

相关标签:
2条回答
  • 2020-12-30 20:39

    I understand these are not supported by ISO 8601, correct?

    Correct. ISO-8601 does not concern itself with time zone identifiers. IANA/Olson TZ names are not a "standard". They are just the most reliable thing we have. (Some may consider them the de facto standard.)

    What are platforms doing to support this?

    Support what exactly? This part of your question is unclear. If you mean to support IANA time zones, well that's all over the place. Some platforms have them built-in, and some rely on libraries. If you mean to support a string representation of an ISO-8601 date-time-offset + time zone ID, some platforms have this and some do not. You'll have to be more specific if you want to know more.

    I notice that the latest Java date/time library is using an extended ISO 8601 format for this, e.g. 2011-12-03T10:15:30+01:00[Europe/Paris]. (See DateTimeFormatter API.)

    I think you are talking about DateTimeFormatter.ISO_ZONED_DATE_TIME. The docs say specifically:

    The ISO-like date-time formatter...

    ...extends the ISO-8601 extended offset date-time format to add the time-zone. The section in square brackets is not part of the ISO-8601 standard.

    So this is Java's specific format, not a standard.

    Is there some converging convention (e.g. with other languages and platforms) for extending ISO 8601 to support time zone designation?

    As far as I know, there is currently no standard that covers the combining of an ISO8601 timestamp and an IANA time zone identifier into a single format. One could represent it many different ways, including:

    • 2011-12-03T10:15:30+01:00[Europe/Paris] (this is the default in Java 8)
    • 2011-12-03T10:15:30+01:00(Europe/Paris)
    • 2011-12-03T10:15:30+01:00 Europe/Paris
    • 2011-12-03T10:15:30+01:00 - Europe/Paris
    • 2011-12-03T10:15:30+01:00/Europe/Paris
    • 2011-12-03T10:15:30+01:00|Europe/Paris
    • 2011-12-03T10:15:30 Europe/Paris (+01) (this is the default in Noda Time)

    If what you're looking for is a way to include a ZonedDateTime or similar data in an API in a standardized manner, my personal recommendation would be to pass the time zone name in a separate field. That way, each portion of data is as good as it can be. For example in JSON:

    {
      "timestamp": "2011-12-03T10:15:30+01:00",
      "timezone": "Europe/Paris"
    }
    
    0 讨论(0)
  • 2020-12-30 20:42

    The Answer by Matt Johnson is spot-on correct. I'll just add a few thoughts.

    Time zone versus offset-from-UTC

    An offset-from-UTC is merely a number of hours, minutes, and seconds ahead/behind UTC. Alone, this does make a date-time into a specific moment on the timeline. But it is not nearly as informative as including the official time zone name as well.

    While there is no standard yet for including the time zone name, I do hope others follow the lead of the java.time classes in appending in square brackets the name of the time zone. This format seems sensible to me as it would be simple to truncate the square-bracket portion to be backward-compatible with non-savvy software.

    For example:
    2011-12-03T10:15:30+01:00[Europe/Paris]. If the data were only 2011-12-03T10:15:30+01:00, we would be able to identify the moment on the timeline, but would not be able to adjust other moments into the same frame of mind as we would not know what rules of adjustment to apply. Zones such as Europe/Zagreb, Africa/Brazzaville, Arctic/Longyearbyen, and Europe/Isle_of_Man all share the offset of +01:00, but they may well have other adjustments in force differing from those of Europe/Paris. So if you were to try to add three days to the value 2011-12-03T10:15:30+01:00, you really cannot faithfully compute the result because you do not know what adjustments may need to apply such as DST cutovers that may be occurring during those three days.

    A time zone defines the set of rules for handling anomalies such as Daylight Saving Time (DST). Politicians around the world enjoy making adjustments to their time zones, or even re-defining them. So these rules change frequently. Think of a time zone as a collection of offsets over time, many periods of time in history wherein each period had a particular offset in use in that particular region.

    You can think of a time zone as a collection of offset-from-UTC values. In America/Los_Angeles part of this year is 8 hours behind UTC, and part of the year will be 7 hours behind UTC. That makes 2 points of data collected as part of that time zone.

    Another example, in previous years, Turkey spent part of each year 2 hours ahead of UTC and part of each year 3 hours ahead. In 2016, that changed to indefinitely staying 3 hours ahead. So, multiple points of data in the time zone Europe/Istanbul.

    Just use UTC

    Personally I do not see much value in even using values such as 2011-12-03T10:15:30+01:00. Without a time zone, you might just as well use UTC alone. In this case, 2011-12-03T09:15:30Z (9 AM instead of 10 AM).

    Generally the best practice is to use UTC when storing and exchanging date-time values. Think of UTC as the One-True-Time, with zoned or offset values being mere variations.

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