Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Is there a way to convert epoch time into mm/dd/yy hh:mm:ss

by TASdvlper (Monk)
on Apr 19, 2004 at 16:42 UTC ( #346338=perlquestion: print w/replies, xml ) Need Help??

TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

All,

I checked the module Date::Manip but could find anything useful. I have a epoch time (e.g. 1082392815) but I want to convert it to a date/time format. 04/19/04 12:35:00

One problem is that this has to work in a Windows and Linux environment.

Is there a module, maybe I just can't find it, that does this little conversion ?

Thanks all ...

  • Comment on Is there a way to convert epoch time into mm/dd/yy hh:mm:ss

Replies are listed 'Best First'.
Re: Is there a way to convert epoch time into mm/dd/yy hh:mm:ss
by matija (Priest) on Apr 19, 2004 at 16:48 UTC
    You can use Date::Manip for that. Simply use
    $date = &ParseDateString("epoch $secs"); print UnixDate($date,"%d/%m/%y %H:%M:%S");
Re: Is there a way to convert epoch time into mm/dd/yy hh:mm:ss
by blue_cowdawg (Monsignor) on Apr 19, 2004 at 16:51 UTC

        Is there a module, maybe I just can't find it, that does this little conversion ?

    perldoc -f localtime

    Here's an application of the doco for you:

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); my $date=sprintf("%0.2d/%0.2d/%0.2d %0.2d:%0.2d:%0.2d", $mon+1,$mday,($year-100), $hour,$min,$sec);
    Should work both on *nix and *doze.

Re: Is there a way to convert epoch time into mm/dd/yy hh:mm:ss
by sgifford (Prior) on Apr 19, 2004 at 17:08 UTC

    POSIX::strftime is a quick and easy way to do this sort of thing. Despite its name, it seems to work fine on my Windows copy of ActiveState Perl.

    It can be used in conjunction with localtime:

    perl -MPOSIX -e"print strftime('%m/%d/%y %H:%M:%S', localtime(10823928 +15));" 04/19/04 12:40:15
Re: Is there a way to convert epoch time into mm/dd/yy hh:mm:ss
by pbeckingham (Parson) on Apr 19, 2004 at 16:59 UTC

    The following is a starting point:

    print scalar localtime 1082392815, "\n";
    Look into what can be done with the return values of localtime.

Re: Is there a way to convert epoch time into mm/dd/yy hh:mm:ss
by asarih (Hermit) on Apr 19, 2004 at 16:51 UTC
    What's wrong with the built-in function localtime?
Re: Is there a way to convert epoch time into mm/dd/yy hh:mm:ss
by Belgarion (Chaplain) on Apr 19, 2004 at 21:18 UTC

    Another option would be to use the Time::Piece module. It replaces the built in localtime function with an object based method.

    You could then do something like this:

    use Time::Piece; my $t = localtime($epoch); print $t->strftime("%m/%d/%y %H:%M:%S");

Re: Is there a way to convert epoch time into mm/dd/yy hh:mm:ss
by jonnyfolk (Vicar) on Apr 19, 2004 at 16:56 UTC
    Perhaps a look at Q&A would help?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-09-28 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (124 votes). Check out past polls.

    Notices?