Why is UTC (which is not a time zone) considered a time zone in Java (and not only there)?

后端 未结 2 1786
失恋的感觉
失恋的感觉 2020-12-19 02:51

Given that UTC is not a time zone, but a time standard (as stated, for example, here), why in my Java application I can use UTC as if it was a time zone (see the code snippe

相关标签:
2条回答
  • 2020-12-19 03:08

    Is the statement "UTC is not a time zone" in reality wrong?

    Technically and strictly speaking, the statement is not wrong. UTC is a standard, not a timezone (as you already linked).

    A timezone corresponds to some region in the world and has lots of different rules regarding that region:

    • What's the UTC offset (the difference from UTC) when it's in Daylight Saving time and when it's not
    • When DST starts and ends
    • All the changes in offsets and DST this region had during its history

    Example: in 1985, the brazilian state of Acre had standard offset UTC-05:00 (and UTC-04:00 during DST), then in 1988 it was on UTC-05:00 without DST, then in 2008 the standard changed to UTC-04:00 (and no DST), and since 2013 it's back to UTC-05:00 and no DST.

    While the timezone keeps track of all of these changes, UTC has no such rules. You can think of UTC in many different ways:

    • a "base" date/time, from where everybody else is relative to - this difference from UTC is called "offset". Today, São Paulo is in UTC-03:00 (the offset is minus 3 hours, or 3 hours behind UTC) while Tokyo is in UTC+09:00 (offset of plus 9 hours, or 9 hours ahead UTC).
    • a "special" timezone that never varies. It's always in the same offset (zero), it never changes, and never has DST shifts.

    As the "offset of UTC" (not sure how technically accurate is this term) is always zero, it's common to write is as UTC+00:00 or just Z.

    Another difference between UTC and timezone is that a timezone is defined by governments and laws and can change anytime/anywhere. All the changes in Acre described above were defined by politicians, for whatever reasons they thought at that time. (So, even if a region today follows UTC in their timezone, there's no guarantee that it'll stay the same in the future, and that's why even those regions have their own timezones, even if they look redundant).

    But no matter how many times politicians change their regions offsets, they must be relative to UTC (until a new standard comes up, of course).


    Now, when you see implementations like TimeZone.getTimeZone("UTC"), you can think of it in 2 different ways:

    • a design flaw, because it's mixing 2 different concepts and leading people to think they're the same thing
    • a shortcut/simplification/nice-trick/workaround, that makes things easier (as @JonSkeet explained in his answer).

    For me, it's a mix of both (fifty/fifty).


    The new java.time API, though, separates the concepts in 2 classes: ZoneRegion and ZoneOffset (actually both are subclasses of ZoneId, but ZoneRegion is not public so actually we use ZoneId and ZoneOffset):

    • if you use a ZoneId with IANA timezones names (always in the format Continent/City, like America/Sao_Paulo or Europe/Berlin), it will create a ZoneRegion object - a "real" timezone with all the DST rules and offset during its history. So, you can have different offsets depending on the dates you're working with in this ZoneId.
    • if you use a ZoneOffset (with Z, UTC, +03:00 and so on), it will return just an object that represents an offset: the difference from UTC, but without any DST rules. No matter what dates you use this object with, it'll always have the same difference from UTC.

    So, the ZoneId (actually ZoneRegion) is consistent with the idea that offsets in some region (in some timezone) change over time (due to DST rules, politicians changing things because whatever, etc). And ZoneOffset represents the idea of difference from UTC, that has no DST rules and never changes.

    And there's the special constant ZoneOffset.UTC, which represents a zero difference from UTC (which is UTC itself). Note that the new API took a different approach: instead of saying that everything is a timezone and UTC is special kind, it says that UTC is a ZoneOffset which has a value of zero for the offset.

    You can still think it's a "wrong" design decision or a simplification that makes things easier (or a mix of both). IMO, this decision was a great improvement comparing to the old java.util.TimeZone, because it makes clear that UTC is not a timezone (in the sense that it has no DST rules and never changes), it's just a zero difference from the UTC standard (a very technical way of saying "it's UTC").

    And it also separates the concepts of timezone and offset (that are not the same thing, although very related to each other). I see the fact that it defines UTC as a special offset as an "implementation detail". Creating another class just to handle UTC would be redundant and confusing, and keeping it as a ZoneOffset was a good decision that simplified things and didn't mess the API (for me, a fair tradeoff).

    I believe that many other system's decide to take similar approaches (which explains why Windows have UTC in the timezone list).

    0 讨论(0)
  • 2020-12-19 03:19

    Because it makes life much, much simpler to regard UTC as a time zone than to treat it as something else, basically.

    It's one of those "Yeah, strictly speaking it's not" scenarios. For everything except "Which region of the world is this observed?" you can think of UTC as a time zone and it works fine. So it's simpler to bend it slightly out of shape than to have a whole separate concept.

    If you view a time zone as a mapping from "instant in time" to "UTC offset" (or equivalent, from "instant in time" to "locally observed time") then UTC is fine to think of as a time zone - and that's most of what we do within software.

    If you view a time zone as a geographical region along with that mapping, then no, it doesn't work as well - but that's more rarely useful in software. (And you can always fake it by saying it's an empty region :)

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