2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
daliaessam
  • 1,636
  • 2
  • 21
  • 43
  • 01/01/1970 is the zero time of EPOCH date format – Gilles Quénot Oct 07 '12 at 13:09
  • I found VB module that works on dates before 1970 but it need time to translate to Perl. UNIX Epoch Timestamp via API (+ Other Time-Related Subs) (modCnTime.bas) '# By: Nick Campbeln '# '# Revision History: '# 1.1 (Apr 2, 2003): '# Added DateSerialToTimestamp() and isDaylightSavings(), completed work on TimestampToDate() '# Contributed to PSC.com (Apr 19, 2003) '# 1.0 (Aug 26, 2002): '# Initial Release '# '# Copyright © 2002-2003 Nick Campbeln (opensource@nick.campbeln.com) – daliaessam Oct 07 '12 at 14:19
  • To make the question clear, I know when the epoch starts 1/1/1970 and I do get the timestamp negative integer for dates before the epoch, the problem is formatting the negative dates in Perl, i.e. the function like locatime(-xxxxxxx) will return empty values. – daliaessam Oct 07 '12 at 14:27
  • no it doesn't: `perl -E 'say scalar localtime(-1)' Thu Jan 1 00:59:59 1970`. what version of Perl are you using? – pavel Oct 07 '12 at 17:21
  • Time::Local docs says: On older versions of perl, negative epoch (time_t) values, which are not officially supported by the POSIX standards, are known not to work on some systems. These include MacOS (pre-OSX) and Win32. On systems which do support negative epoch values, this module should be able to cope with dates before the start of the epoch, down the minimum value of time_t for the system. – jira Oct 07 '12 at 17:52
  • You say "unix" - what Unix? Your sample code work on CentOS5 and perl5.8.8 – jira Oct 07 '12 at 17:58
  • perl >= 5.12 should work everywhere – jira Oct 07 '12 at 17:59
  • I think if you showed us the *exact* output of your code in your question that might clear a lot of things up. Also the output of `use Time::Local; print $Time::Local::VERSION`. – Schwern Oct 08 '12 at 01:16

3 Answers3

2

have you tried using DateTime?

snoofkin
  • 8,725
  • 14
  • 49
  • 86
  • Yes spent several days on cpan and perl search, all says that the size of time_t varies on different systems and all claims they do not support dates before epoch date. do you have specific example that handles dates before the epoch using DateTime or any other modules or even none module code. Thank you – daliaessam Oct 07 '12 at 14:11
2

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.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • I did not get the point, Time::Local function localtime already returns the negative number for dates before epoch 1970 what I need is to pass this negative number to localtime or some other function to return formated date time array. currently locatime return empty values for negative timestamps. – daliaessam Oct 07 '12 at 14:14
2

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
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • this does work on Windows with Perl 5.8.8 or any Perl not compiled with 64 bit support. Your code is Perl 5.10 or higher as it has the say function instead of print. – daliaessam Oct 07 '12 at 19:25