Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do java.time.ZoneOffset instances sort 'backwards'?

From the documentation:

The offsets are compared in the order that they occur for the same time of day around the world. Thus, an offset of +10:00 comes before an offset of +09:00 and so on down to -18:00.

Does anyone know the reason they are ordered this way? It seems counter-intuitive as it is the opposite of the order of the numerical value of the offset. It is also the opposite of the order they would appear, reading left-to-right, on a GMT/UTC+0 centred map.

(Aside: I don't understand why the quoted documentation refers to "down to -18:00" either. -12:00 or -24:00 I'd understand. Why -18:00? If you print out the offsets of all the supported java.time.ZoneId time zones they range from -11:00 to +12:00.)

UPDATE: An answer to my aside. From elsewhere in the same documentation:

In 2008, time-zone offsets around the world extended from -12:00 to +14:00. To prevent any problems with that range being extended, yet still provide validation, the range of offsets is restricted to -18:00 to 18:00 inclusive.

like image 470
Paul Avatar asked May 26 '16 10:05

Paul


People also ask

What is ZoneOffset in Java?

ZoneOffset extends ZoneId and defines the fixed offset of the current time-zone with GMT/UTC, such as +02:00. This means that this number represents fixed hours and minutes, representing the difference between the time in current time-zone and GMT/UTC: LocalDateTime now = LocalDateTime.

How does timezone offset work?

Breaking Down the DifferenceAn offset is the number of hours or minutes a certain time zone is ahead of or behind GMT**. A time zone's offset can change throughout the year because of Daylight Saving Time. Sometimes laws change a time zone's offset or daylight savings pattern.

What is ZoneOffset Z?

ZoneOffset is a class in Java that denotes the fixed zone offset from the time zone UTC. This class inherits the class ZoneId and implements the interface Comparable.

What is Zone_offset?

zone_offset(zone, year=self.now.year) public. Return the number of seconds the specified time zone differs from UTC. Numeric time zones that include minutes, such as -10:00 or +1330 will work, as will simpler hour-only time zones like -10 or +13.


1 Answers

Imagine you've got a list of localized date/times, with the associated timezone, of when some event occurred:

(1) 2016/05/26 00:00:00 in Australian Eastern Standard Time (UTC+10)
(2) 2016/05/26 00:00:00 in Greenwich Mean Time (UTC+0)
(3) 2016/05/26 00:00:00 in Japan Standard Time (UTC+9)

You can convert these into instants in one single time zone, say GMT:

(1) 2016/05/25 14:00:00 GMT
(2) 2016/05/26 00:00:00 GMT
(3) 2016/05/25 15:00:00 GMT

and now sort them in order of "which happened first":

(1) 2016/05/25 14:00:00 GMT
(3) 2016/05/25 15:00:00 GMT
(2) 2016/05/26 00:00:00 GMT

So the Australian time (1) is before the Japanese time (3), which is before the British time (2).

It's just applying the same order to the time zones: as the quote says:

The offsets are compared in the order that they occur for the same time of day around the world

So Australian Eastern Standard Time is considered "before" Japan Standard Time, which is considered "before" Greenwich Mean Time.

like image 76
Andy Turner Avatar answered Oct 25 '22 13:10

Andy Turner