Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Strage result using localtime to convert epoch time

by punch_card_don (Curate)
on Jul 20, 2007 at 21:21 UTC ( #627886=perlquestion: print w/ replies, xml ) Need Help??
punch_card_don has asked for the wisdom of the Perl Monks concerning the following question:

Maverick Monks,

This snippet:

$file_w_path = "../../../images/".$filename; ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, +$ctime, $blksize, $blocks) = stat($file_w_path); print "<br>mtime = $mtime\n"; ($sec, $min, $hr, $day, $month, $year, $day_Of_Week, $julianDate, $dst +) = localtime($mtime); print "<br>$sec, $min, $hr, $day, $month, $year, $day_Of_Week, $julian +Date, $dst\n";
produces this output:
1182819156 Time::tm=ARRAY(0x84eb290), , , , , , , ,
where 1182819156 is the correct epoch time for the file, but I can't make sense of the localtime output. Clearly I'm using localtime wrong...and yet,

http://perldoc.perl.org/functions/localtime.html

localtime

Converts a time as returned by the time function to a 9-element list with the time analyzed for the local time zone. Typically used as follows:

# 0 1 2 3 4 5 6 7 8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

Thanks.




Forget that fear of gravity,
Get a little savagery in your life.

Comment on Strage result using localtime to convert epoch time
Select or Download Code
Re: Strage result using localtime to convert epoch time
by revdiablo (Prior) on Jul 20, 2007 at 21:29 UTC

    I cannot duplicate your output. Is there more to the code you're not showing us? When I run it, I see the following:

    $ perl 627886.pl <br>mtime = 1178489994 <br>54, 19, 15, 6, 4, 107, 0, 125, 1

    Update: ah, I can indeed reproduce your output if I add use Time::localtime; to the code. Now I get:

    $ perl 627886.pl <br>mtime = 1178489994 <br>Time::tm=ARRAY(0x81cce18), , , , , , , ,

    I missed the Time::tm= bit the first time I saw your code. This module is overriding the localtime function and returning an object instead of the usual return values.

Re: Strage result using localtime to convert epoch time
by Joost (Canon) on Jul 20, 2007 at 22:23 UTC
Re: Strage result using localtime to convert epoch time
by punch_card_don (Curate) on Jul 20, 2007 at 23:58 UTC
    Yep - that's it - a little utility that a sub-routine required uses Time::localtime - completely forgot.

    Thanks.

    So, is there a way to convert my epoch time to regular time using Time::localtime?

        Excellent.

        Simply changing to

        ($sec, $min, $hr, $day, $month, $year, $day_Of_Week, $julianDate, $dst +) = CORE::localtime($mtime);
        gave the desired result.

        Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://627886]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-10-25 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls