Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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 (Priest) 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
[Eily]: hello thao4 (and hello all :D), you should [id://http:// 479#post|open a question on Seekers of Perl Wisdom], be sure to include whatever you have tried that didn't work, or only does the job partially
[thao4]: yes, I do a little perl since some years but not fréquently, I can correct the triggers in Clearcase of IBM, but write the code : no
[Eily]: meh, such good link ...
[Eily]: here
[Eily]: still not xD there

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2018-02-21 09:50 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (276 votes). Check out past polls.