Operating System - HP-UX
1819725 Members
3014 Online
109606 Solutions
New Discussion юеВ

what the diff bet perl timelocal and gnu date

 
SOLVED
Go to solution
YLTan
Frequent Advisor

what the diff bet perl timelocal and gnu date


I need to convert a date such as 10/15/2003 to seconds. I tried both timelocal function in perl and also the date command from GNU download from HPUX Porting site in UTAH.

The results is below, but I do not know why the different between the two is 31 days apart. Can someone enlight me on this?

[dev01]/home/perfopr/ict01> cat x
#!/usr/contrib/bin/perl
use Time::Local;
$sec=00;
$min=00;
$hours=00;
$mday=15;
$mon=10;
$year=2003;
$TIME = timelocal($sec, $min, $hours, $mday, $mon, $year);
print "$TIME", "\n";
exit 0
[dev01]/home/perfopr/ict01> perl x
1068825600
[dev01]/home/perfopr/ict01> ./gdate -d "10/15/2003 00:00:00" +%s
1066147200
[dev01]/home/perfopr/ict01> bc
((1068825600 - 1066147200) / 3600 ) / 24
31
tyl
4 REPLIES 4
Keely Jackson
Trusted Contributor

Re: what the diff bet perl timelocal and gnu date

Hi

The reason is because the timelocal function in Perl returns the result on mon -1, ie in your case September not October, hence it is 31 days out. To get the correct number of seconds, use $mon=$mon+1;

Cheers
Keely
Live long and prosper
A. Clay Stephenson
Acclaimed Contributor

Re: what the diff bet perl timelocal and gnu date

As has been said, Perl's timelocal (and C's ctime() for that matter) use months in the range of 0-11 thus October should be 9 not 10 but you also have another problem and that is that the year should be expressed as 103 (years since 1900) rather than 2003. Most Perl implementations will relax the year specification and assume that you are an idiot and silently convert 2003 to 103 for you --- but not all.
If it ain't broke, I can fix that.
Ralph Grothe
Honored Contributor
Solution

Re: what the diff bet perl timelocal and gnu date

At your shell, please type

perldoc Time::Local

Therein it is concisely outlined how Perl is treating Dates, and what the functions from the module expect as arguments, and what they return.
Remember in Perl context matters (viz. scalar vs. list) when you assign the functions' return values.

Please also have a look at

perldoc -f localtime
perldoc POSIX

Of the last you may find the strftime() function of interest, which lets you format date strings as used to from Unix date commands.
Madness, thy name is system administration
john korterman
Honored Contributor

Re: what the diff bet perl timelocal and gnu date

Hi,
for checking purposes, it can be helpful to convert the seconds into something human friendly, e.g.:
# SECS=1068825600
# echo 0d${SECS}=Y|adb

but be aware that your TZ setting influences the result

regards,
John K.
it would be nice if you always got a second chance