tags:

views:

85

answers:

1

We are processing data from an API that is returning date/time values of the following form:

1061943540000

When I used the tool on this page it converts the value to the following correct date:

Tue Aug 26 2003 17:19:00 GMT-0700 (PDT)

We are using the following Perl code to convert the date/time in our script:

use DateTime;
my $dt = DateTime->from_epoch(epoch => $time);

However, this is producing the following output which is incorrect:

1969-12-31 23:59:59

I'm totally confused why this isn't working and would appreciate someone explaining what I'm doing wrong and what code I should be using to convert it correctly. Thanks in advance!

+9  A: 

That's not an epoch time in seconds. It looks like it has milliseconds attached to it. That screws up the stuff that DateTime uses, which is seconds since the epoch. Perl, in general, has a mindset of whole seconds since the epoch (see time, localtime, gmtime).

As given, you get a time that is far, far away. I might get a different date than you because my Perl is 64-bit and has the Y2038 fix:

$ perl -MDateTime -E 'say DateTime->from_epoch( epoch => shift() )' 1061943540000
35621-08-26T04:40:00

Dividing by 1000 gives you the right date, although in UTC:

$ perl -MDateTime -E 'say DateTime->from_epoch( epoch => shift() / 1000 )' 1061943540000   
2003-08-27T00:19:00

If you go back to your online Java tool, you'll notice that it gives you the same date with 1061943540000 and 1061943540. It's guessing that one is milliseconds. That also means it gives the wrong date if 1061943540 was in milliseconds.

brian d foy
Java reports times in milliseconds since The Epoch.
Jonathan Leffler
Just to clarify, it doesn't overflow because "DateTime can't handle sub-second times" or anything like that -- it overflows when you give it a number of milliseconds, but make it think it's a number of seconds instead :)
hobbs
Yes, DateTime actually goes down to nanoseconds as I recall.
brian d foy
@Brian - worked like a charm. Thanks for helping clarify!
Russell C.