Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

seconds since the epoch

by my_nihilist (Sexton)
on Mar 15, 2008 at 23:57 UTC ( #674401=perlquestion: print w/replies, xml ) Need Help??
my_nihilist has asked for the wisdom of the Perl Monks concerning the following question:

does anyone have a quick easy way to convert seconds since the epoch into yy/mm/dd hh:mm or some such?

Replies are listed 'Best First'.
Re: seconds since the epoch
by hipowls (Curate) on Mar 16, 2008 at 00:22 UTC

    There is Date::Format

    use Date::Format; print time2str("%a %b %e %T %Y\n", time); __END__ Sun Mar 16 11:20:38 2008
    There are lots of formatting options, you should be able to find what you need.

Re: seconds since the epoch
by igelkott (Curate) on Mar 16, 2008 at 00:45 UTC
    "Seconds since the epoch" is the optional argument to "localtime". So my $date = localtime $epoch provides the basic data string according to the value of $epoch.

    Formatting dates can be done is many different ways and you'll most certainly see some in other replies or the documentation.

Re: seconds since the epoch
by tachyon-II (Chaplain) on Mar 16, 2008 at 00:46 UTC

    You can do it using localtime and printf or POSIX::strftime. POSIX is a core module. FYI POSIX::mktime converts sec,min,hr,d,m,y to epoch time if you want to go the other way.

    my $time = time(); my ($sec,$min,$hour,$day,$mon,$year) = localtime($time); $year+= 1900; $mon += 1; $year = substr $year, 1; printf "%02d/%02d/%02d %02d:%02d\n", $year, $mon, $day, $hour, $min; use POSIX qw(strftime); print strftime("%y/%m/%d %H:%M\n", localtime); print strftime(" POSIX strftime a %a A %A b %b B %B c %c d %d H %H I %I j %j m %m M %M p %p S %S U %U w %w W %W x %x X %X y %y Y %Y Z %Z ", localtime); __DATA__ 08/03/16 11:37 08/03/16 11:37 POSIX strftime a Sun A Sunday b Mar B March c 3/16/2008 11:37:48 AM d 16 H 11 I 11 j 076 m 03 M 37 p AM S 48 U 11 w 0 W 10 x 3/16/2008 X 11:37:48 AM y 08 Y 2008 Z Vulcan Standard Time
Re: seconds since the epoch
by syphilis (Chancellor) on Mar 16, 2008 at 00:24 UTC
    See perldoc -f localtime.

Re: seconds since the epoch
by ikegami (Pope) on Mar 16, 2008 at 00:28 UTC
    There's also strftime in core module POSIX, as mentioned in the docs for localtime.
    use POSIX qw( strftime ); my $time = 400_000_000; print strftime("%y/%m/%d %H:%M\n", localtime($time));

    Update: Changed "%Y" to "%y" to match OP's format.

      s/y/Y/ s/Y/y/
        eh? there is no "y" in my postOh, you mean s/Y/y/
Re: seconds since the epoch
by apl (Monsignor) on Mar 16, 2008 at 01:38 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://674401]
Approved by ikegami
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2017-02-19 23:46 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (293 votes). Check out past polls.