Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Convert epoch seconds to date

by Woody (Initiate)
on Mar 19, 2001 at 19:18 UTC ( [id://65424]=perlquestion: print w/replies, xml ) Need Help??

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

I have worked out how to convert a date string to epoch seconds - but how do i do this if i want to do the reverse - the user enters the epoch seconds, and the script converts this to dd/mm/yyyy...?

Replies are listed 'Best First'.
Re: Convert epoch seconds to date
by lhoward (Vicar) on Mar 19, 2001 at 19:20 UTC
    There are several perl modules that can do this for you. My favorite for this task is Time::CTime (check out the strftime function). This task is also real easy to write yourself using perl's built-in localtime function.

    Of course, this all assumes that your Epoch is Jan 1, 1970 (which is a pretty safe bet) If that isn't your epoch you'll need to do more conversion first.

      @time = (localtime())[3..5]; $time[1]++; $time[2] += 1900; print join ('/', @time);
      can't get it shorter :(

        Update: I thought I was in a different thread so much of this is incorrect. My apologies to slayven. ):

        Note that this does d-m-yyyy which isn't what was asked for (the order is wrong and you don't pad with zeros).

        use mapcar; print join '-', mapcar {sprintf "%02d",pop()+pop} [(localtime())[5,4,3]],[1900,1,0];
        Is my contribution (which does yyyy-mm-dd since that is simply a better format).

        See mapcar -- map for more than one list for one needed part.

                - tye (but my friends call me "Tye")
      my $epoch_seconds = "?";
      my $date = scalar localtime($epoch_seconds);
Re: Convert epoch seconds to date
by davorg (Chancellor) on Mar 19, 2001 at 20:34 UTC

    That's what the localtime function does. Pass it a number of epoch seconds and it'll return an array of values that you can use to build a human-readable date.

    Or, even easier, use localtime in conjunction with the POSIX::strftime function.

    use POSIX 'strftime'; my $epoch = 985015768; print strftime('%d/%m/%Y', localtime($epoch));
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      ty dave, works for me:-)
      use POSIX 'strftime'; $epoch = $escrowClose_e; $epoch_o = $escrowOpen_e; $redate = strftime('%m/%d/%Y', localtime($epoch)); $opendate = strftime('%m/%d/%Y|%H:%M', localtime($epoch_o));
(tye)Re: Convert epoch seconds to date
by tye (Sage) on Mar 19, 2001 at 21:49 UTC
Re: Convert epoch seconds to date
by lachoy (Parson) on Mar 19, 2001 at 22:30 UTC

    You can also use the pure-perl Date::Format module:

    perl -MDate::Format -e 'print time2str( "%Y-%m-%e", 192948519 )' perl -MDate::Format -e 'print time2str( "%Y-%m-%e", 19294851 )'

    Respectively returns:

    1976-02-11 1970-08-12

    Chris
    M-x auto-bs-mode

Re: Convert epoch seconds to date
by frankus (Priest) on Mar 20, 2001 at 18:20 UTC
    There are lots of good answers here, my answer is more generic: the Perl Cook Book has a section on this, and when you understand that bit of the book there are loads more examples that can help you everyday ;-)

    --
    
    Brother Frankus.
Re: Convert epoch seconds to date
by belize (Deacon) on Aug 04, 2001 at 23:58 UTC
    How about this subroutine. I'm new to Perl, but think this works OK?:

    sub unix_to_date { my ($date) = $_[0]; # reads in variable passed to subroutine my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug sep Oct Nov Dec!; #c +reates array of month names @time = (localtime($date))[3,4,5]; #creates array of date from unix ti +me $time[2] += 1900; # converts year to proper format return "$time[0]-$months[$time[1]]-$time[2]"; }

    You now call this with:

    $date_from_unix = &unix_to_date(epochtime);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-28 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found