Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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.

Replies are listed 'Best First'.
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 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 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?
[james28909]: what if one day we are able to grab the curtain that is "spacetime" and pull it back to reveal <insert imagination here> pulling at levers and ropes xD
[holli]: like dorothy?
[james28909]: see ya later holli
[james28909]: yeah like wizard of oz lol. its just a joke though
[james28909]: i cannot understand how someone cant see that a cloud of gas that evolves into a solar system or galaxies. it just makes sense to me. everythign that has happened, had to have happened for anything on this planet to be.
[james28909]: /gas/gas and debris/
[james28909]: another thing, i used to be atheist. life experiences, personal evidence. and my own perception is the reason why i believe the things i do.
[erix]: and now you are agnostic?
[james28909]: arunbear, your picking bits of history to fit your own narrative. right now, humans are smarter than in any of those times.

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2017-12-15 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (433 votes). Check out past polls.

    Notices?