$ perl -MDateTime -e 'print DateTime->now()'
2006-04-04T23:08:37
$ perl -e 'print scalar localtime'
Tue Apr 4 19:09:10 2006
It seems that DateTime->now() doesn't do what I thought it did. I thought it returned the current date and time at this point on the planet. What it actually does is return the current date and time in UTC! (And please don't tell me that DateTime can't reliably figure out my timezone. Perl's localtime() manages just fine!) (You can, however, lambaste me for not reading the DateTime docs, since it's quite clear that now() doesn't return local time.)
Unfortunately I discovered this only in the late stages of testing version 3 of a large application that makes moderately extensive use of Datetime. That means the "easy" way to fix this, namely:
$ perl -MDateTime -e 'print DateTime->now(time_zone => "local")'
2006-04-04T19:12:03
Isn't so easy, as it would mean touching a lot of files.
As far as I can tell I don't have a lot of good options here. I'm going to make a sub-class of DateTime which defaults to "local" for now(), new() and from_epoch(), but that will still mean touching lots of files. I did find discussion of this problem on the DateTime lists, but I don't think a solution made it into the module.
I did discover that Tim Bunce considers my chosen solution "trivial", whatever that means!
-sam
hate to be harsh but ... (Score:2)
How is this possible? How can you have been using versions 1 and 2, and well into testing version, with the time stamps off by hours, without anyone noticing?
Re:hate to be harsh but ... (Score:2)
Another factor is that a lot of the most important dates were right. MySQL has a DWIM that matched our expectations - NOW() really is now, right here, on this spot on the planet. So times
Other Way Round? (Score:1)
Suppose that
DateTimewere the other way round, and defaulted to the local timezone. Then somebody could've made an equivalent blog post to yours but comparingDateTime->nowwithgmtimeand complaining that they are different!There are 2 different possible behaviours. Obviously the module can only do 1 of them by default.
I've been inconvenienced the other way round, when I discovered that our MySQL database was storing timestamps in local time rather than UTC and not storing the timezone.
Re:Other Way Round? (Score:2)
Sure, that doesn't mean that everyone should expect that or that the developers of DateTime are morons. They ju
Re:Other Way Round? (Score:1)
E.g., in your example, the only thing we know about your timezone is that it is '+0400' now, but we don't know what DST rules you have. So instead of guessing the correct timezone, DateTime chooses to let the user decide.
Re:Other Way Round? (Score:1)
Except that it isn't it's more like floating time, because it doesn't also note the timezone. That's just wrong, because it means that MySQL's timestamps don't map to a particular moment in time, but to several possible moments. And even if you know the physical location, because of daylight-saving time that isn't good enough.
I don't mind at all which timezone MySQL uses to display its times. And I certainly don't
Re:Other Way Round? (Score:2)
You could also note that it displays the date by default in ISO8601 format, which has the fields in an order that most humans don't use in their day-to-day lives.
I do, now, ever since I first used DateTime and thought, "What is that weird format, and why do they use it?"
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
__ (Score:1)
Re:__ (Score:2)
Re:__ (Score:2)
-sam
Re:__ (Score:1)
Do what I did (Score:2)
I simply switched my own personal timezone to UTC. My watch is set to UTC, my PC timezone is set to UTC, my TZ variables everywhere I go are set to UTC, my Wikipedia preferences are set to UTC, my forum preferences many places on the net are set to UTC. I know when the sun rises and sets. I'm always reminded to convert timestamps for the benefit of my coworkers, 100% of whom are all distributed across the other three continental U.S. timezones. The hardest part is twice a year when I have to remember th
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
On the flip side (Score:1)
I hate date time conversions in general. They're right up there is characher/codepage conversions in my book if things no human should ever have to think about.
In MSSQL land, we have GETDATE(), and GETUTCDATE(). Converting them on the fly is even more fun.
Mistake #1 (Score:2)
The Ruby Way (Score:2)
Time.now.utc # UTC
Why not make DateTime->now() return localtime and create a utc() method? I guess it would break backwards compatibility, though.