Reputation: 1659
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!
Upvotes: 1
Views: 3246
Reputation: 132896
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.
Upvotes: 12