Question

I have problem formatting and dealing with dates before the epoch 1/1/1970 in Perl, dates comes back as negative integer:

my $time=timelocal(0, 0, 0, 1, 1, 1969);
print "$time\n"; 
$theTime = localtime($time); 
print "the time is good: $theTime\n\n";

How to deal with dates before the epoch in Perl, both on unix and windows have the same problem Perl 5.8.8. PHP shows the date normal without problems.

Was it helpful?

Solution

have you tried using DateTime?

OTHER TIPS

If perl print a negative integer, this is the good behaviour since 01/01/1970 is the zero day of this date format. Search the word negative on https://en.wikipedia.org/wiki/Unix_epoch

An example in shell :

$ date -d "1957-10-04T00:00:00Z" +%s
-386380800

This is correct.

I need is to pass this negative number to localtime or some other function to return formated date time array.

Ok, what's stopping you?

# ActivePerl on Windows
>perl -E"say ''.localtime(-386380800)"
Thu Oct  3 20:00:00 1957

# Linux
$ perl -E'say "".localtime(-386380800)'
Thu Oct  3 16:00:00 1957

# Cygwin
$ perl -E'say "".localtime(-386380800)'
Thu Oct  3 16:00:00 1957
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top