mkent has asked for the wisdom of the Perl Monks concerning the following question:
A quick question:
I pull time and date from a file, and separate day, month, year, hour, minute and second, then feed the variables into localtime.pl that comes with Perl. However, one of the variables keeps producing an error, even with chop and chomp used on that variable:
my $log_time = timelocal($sec,$min,$hhour,$day,$month,$year);
I'm pretty sure it's not timelocal itself, since feeding it straight numbers works fine, translating into seconds.
I thought Perl automatically selected file types, but it looks to me like it's seeing $day as a string, even though it's a number, like 15.
A web search didn't turn up anything relevant. Any ideas?
Title edit by tye
Re: timelocal
by mojotoad (Monsignor) on Dec 20, 2002 at 18:38 UTC
|
What do you mean, 'one of the variables keeps producing an error'? Which variable, what error?
Are you using use warnings; and use strict;?
What are the contents of $sec, $min, $hhour, $day, $month, and $year just prior to the timelocal call? (just for an example of data that produces the error)
Matt | [reply] [d/l] [select] |
Re: Error from timelocal
by jonnyfolk (Vicar) on Dec 20, 2002 at 20:14 UTC
|
| [reply] |
|
| [reply] |
|
#!/usr/bin/perl -w
use strict;
use CGI ':standard';
use Time::Local;
my $time = timelocal(0,0,0,1,0,100);
my $string = localtime $time;
print header(), start_html(-title => "");
print "the big ball falls at $time => $string\n";
print end_html();
Simply placing the mkent's line:
my $time = timelocal($sec,$min,$hhour,$day,$month,$year);
should work in the same way provided MarkM's advice is followed:
my $time = timelocal($sec,$min,$hhour,$day,$month-1,$year-=1900);
So finally we have:
#!/usr/bin/perl -w
use strict;
use CGI ':standard';
use Time::Local;
my $sec = "50";
my $min = "20";
my $hhour = "10";
my $day = "12";
my $month = "12";
my $year = "2002";
my $time = timelocal($sec,$min,$hhour,$day,$month-1,$year-=190
+0);
my $string = localtime $time;
print header(), start_html(-title => "");
print "the big ball falls at $time => $string\n";
print end_html();
The truth is in there. Somewhere.
| [reply] [d/l] [select] |
|
Thanks. This was good reading, though I still have the problem!
| [reply] |
|
| [reply] |
Re: Error from timelocal
by CountZero (Bishop) on Dec 20, 2002 at 19:48 UTC
|
What you need is Time::Local, part of the standard set of Perl Modules. As per the manual: These routines are the inverse of built-in perl fuctions localtime() and gmtime(). They accept a date as a six-element array, and return the corresponding time(2) value in seconds since the Epoch (Midnight, January 1, 1970). This value can be positive or negative.
CountZero "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
| [reply] |
Re: timelocal
by fglock (Vicar) on Dec 20, 2002 at 19:06 UTC
|
timelocal "converts a time as returned by the time function
to a 9-element list with the time analyzed for the local time zone" - it's input is a simple
number, not a list.
update: my fault. I was talking about localtime. Sorry.
| [reply] |
|
Actually, timelocal() converts a 6 element list into a time value. localtime() converts a time value into a 9 element list.
Without knowing what the error is, I would suspect that the error is that timelocal() expects the month to be 0-based, and the year to be the offset from 1900.
Also, I would recommend that 'use Time::Local' be used, and not 'require "timelocal.pl"'. (timelocal.pl is implemented in terms of Time::Local, therefore 'require "timelocal.pl"' is unnecessary obscurity)
| [reply] |
Re: Error from timelocal
by logan (Curate) on Dec 20, 2002 at 19:51 UTC
|
I really need some more data to diagnose this.
If you could post the values of $sec, $min, $hhour, $day, $month, and $year before the call to timelocal and the value of $log_time after the call, I can take a look.
BTW, did you really mean to use $hhour instead of $hour?
-Logan
"What do I want? I'm an American. I want more." | [reply] |
Re: Error from timelocal
by kjd (Acolyte) on Dec 20, 2002 at 23:41 UTC
|
Edit: Oopsie, I see MarkM has already mentioned this. I must have missed that portion of his reply.
This is just a wild guess, but seeing as it is December, make sure $month is zero-based (January is 0, December is 11). timelocal() does out-of-range checking, and will not accept 12 as a month. | [reply] |
|
Yes, since its December, you're probably using 12 which is out-of range for $month. You may also need to subtract 1900 from the year since it (currently) needs to be 102.
Try dumping the values from localtime so that you can see what the values should look like, as in:
use strict;
use Data::Dumper;
my @time_ary = localtime(time);
print Dumper(\@time_ary);
Where do you want *them* to go today? | [reply] [d/l] |
|
You may also need to subtract 1900 from the year since it (currently) needs to be 102.
Time::Local is smart enough to figure that out by itself.
| [reply] |
|
|