Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Error from timelocal

by mkent (Acolyte)
on Dec 20, 2002 at 18:33 UTC ( [id://221486]=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
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

Re: Error from timelocal
by jonnyfolk (Vicar) on Dec 20, 2002 at 20:14 UTC
    I would recommend reading merlyn's Getting a date with Perl.

    There's quite a bit of general information there and who knows - it may even solve your problem!

      "Getting a date with Perl" may be the most misleading title since "Naked Lunch". It's Friday night, and despite (or because of) my skill with Perl, I still have the same problem as before: no date. If Perl could help people get dates, C would be deader than Pascal and Larry Wall would be richer than Bill Gates.

      -Logan
      "What do I want? I'm an Perl Coder. I want more dates."

        From my reading of the problem, mkent simply has a problem translating the variables in his file with Time::Local.

        Reproducing merlyn's article we get:

        #!/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.
      Thanks. This was good reading, though I still have the problem!
        Did you run merlyn's script with your parameters? In the script I showed it worked well so you would need to post more code and data to see where the problem might be.

        The truth is in there. Somewhere...

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

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.

      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)

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."

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.
      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?
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://221486]
Approved by fglock
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-24 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found