Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Converting from "time" into a nice string?

by ultranerds (Hermit)
on Nov 14, 2008 at 13:18 UTC ( [id://723645]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

This should be a piece of cake for you guys, but I can't for the life of me work out how to do it :(

I have strings like:

1226667500
1226667512
1226667877
1226667880
1226667918
1226667936

(these examples are all from today, just taken a minute or 2 appart).
What I need to do, is convert them to something like:
14/11/2008 13:17:43

..can anyone suggest the best approach for this? I've had a look at Time::Local and Time::Epoch, but they don't seem to do what I need :(

TIA for any suggestions!

Andy
  • Comment on Converting from "time" into a nice string?

Replies are listed 'Best First'.
Re: Converting from "time" into a nice string?
by Anonymous Monk on Nov 14, 2008 at 13:22 UTC
    print scalar localtime($time)
      Wow, that was painless - thanks :)
Re: Converting from "time" into a nice string?
by ccn (Vicar) on Nov 14, 2008 at 13:24 UTC
Re: Converting from "time" into a nice string?
by gwadej (Chaplain) on Nov 14, 2008 at 13:25 UTC

    Check out strftime in the POSIX module. Allows you to format (more or less) how you want.

    G. Wade
      Thanks.

      Another quick question if I may :)

      The current server is setup as PST.

      ultranerds@east ~ $ date
      Fri Nov 14 05:33:17 PST 2008

      How would I go about increasing (and decreasing, depending on the timezone I wish to show the date stamp in), the output from PST, to say GMT , or another timezone.

      I guess its something like:

      hours difference from GMT * 60 * 60 +(or -) the timestamp

      would that be about right?

      TIA!

      Andy

        Presuming your libc plays along, in theory you can diddle $ENV{TZ} and get the right representation from the same time_t value. Otherwise you're going to want to look into DateTime and let it and DateTime::TimeZone convert things around for you.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: Converting from "time" into a nice string?
by dHarry (Abbot) on Nov 14, 2008 at 13:33 UTC
    use strict; use warnings; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(1 +226667500); printf("%02d/%02d/%04d %02d:%02d:%02d\n", $mday, $mon+1, $year+1900,$hour, $min, $sec);

    Outputs

    14/11/2008 13:58:20

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-23 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found