



(This is a companion piece to my article about DateTime::Moonpig on the Perl Advent Calendar today. One of the ways DateTime::Moonpig differs from DateTime is by defaulting to UTC time instead of to DateTime 's "floating" time zone. This article explains some of the reasons why.) Perl's DateTime module lets you create time values in a so-called "floating" time zone. What this means really isn't clear. It would be coherent for it to mean a time with an unknown or unspecified time zone, but it isn't treated that way. If it were, you wouldn't be allowed to compare "floating" times with regular times, or convert "floating" times to epoch times. If "floating" meant "unspecified time zone", the computer would have to honestly say that it didn't know what to do in such cases. But it doesn't. Unfortunately, this confused notion is the default. Here are two demonstrations of why I don't like "floating" time zones. 1. The behavior of the set_time_zone method may not be what you were expecting, but it makes sense and it is useful: my $a = DateTime->new( second => 0, minute => 0, hour => 5, day => 23, month => 12, year => 2013, time_zone => "America/New_York", ); printf "The time in New York is %s.

", $a->hms; $a->set_time_zone("Asia/Seoul"); printf "The time in Seoul is %s.

", $a->hms; Here we have a time value and we change its time zone from New York to Seoul. There are at least two reasonable ways to behave here. This could simply change the time zone, leaving everything else the same, so that the time changes from 05:00 New York time to 05:00 Seoul time. Or changing the time zone could make other changes to the object so that it represents the same absolute time as it did before: If I pick up the phone at 05:00 in New York and call my mother-in-law in Seoul, she answers the call at 19:00 in Seoul, so if I change the object's time zone from New York to Seoul, it should change from 05:00 to 19:00. DateTime chooses the second of these: setting the time zone retains the absolute time stored by the object, so this program prints: The time in New York is 05:00:00. The time in Seoul is 19:00:00. Very good. And we can get to Seoul by any route we want: $a->set_time_zone("Europe/Berlin"); $a->set_time_zone("Chile/EasterIsland"); $a->set_time_zone("Asia/Seoul"); printf "The time in Seoul is still %s.

", $a->hms; This prints: The time in Seoul is still 19:00:00. We can hop all around the globe, but the object always represents 19:00 in Seoul, and when we get back to Seoul it's still 19:00. But now let's do the same thing with floating time zones: my $b = DateTime->new( second => 0, minute => 0, hour => 5, day => 23, month => 12, year => 2013, time_zone => "America/New_York", ); printf "The time in New York is %s.

", $b->hms; $b->set_time_zone("floating"); $b->set_time_zone("Asia/Seoul"); printf "The time in Seoul is %s.

", $b->hms; Here we take a hop through the imaginary "floating" time zone. The output is now: The time in New York is 05:00:00. The time in Seoul is 05:00:00. The time has changed! I said there were at least two reasonable ways to behave, and that set_time_zone behaves in the second reasonable way. Which it does, except that conversions to the "floating" time zone behave the first reasonable way. Put together, however, they are unreasonable. 2. use DateTime; sub dec23 { my ($hour, $zone) = @_; return DateTime->new( second => 0, minute => 0, hour => $hour, day => 23, month => 12, year => 2013, time_zone => $zone, ); } my $a = dec23( 8, "Asia/Seoul" ); my $b = dec23( 6, "America/New_York" ); my $c = dec23( 7, "floating" ); printf "A is %s B

", $a < $b ? "less than" : "not less than"; printf "B is %s C

", $b < $c ? "less than" : "not less than"; printf "C is %s A

", $c < $a ? "less than" : "not less than"; With DateTime 1.04, this prints: A is less than B B is less than C C is less than A There are non-transitive relations in the world, but comparison of times is not among them. And if your relation is not transitive, you have no business binding it to the < operator. However... Rik Signes points out that the manual says: If you are planning to use any objects with a real time zone, it is strongly recommended that you do not mix these with floating datetimes. However, while a disclaimer in the manual can document incorrect behavior, it does not annul it. A bug doesn't stop being a bug just because you document it in the manual. I think it would have been possible to implement floating times sanely, but DateTime didn't do that. [ Addendum: Rik has now brought to my attention that while the main ->new constructor defaults to the "floating" time zone, the ->now method always returns the current time in the UTC zone, which seems to me to be a mockery of the advice not to mix the two. ]

[Other articles in category /prog/perl] permanent link

