http://www.perlmonks.org?node_id=627886

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