Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Print time in Date"T"time+Gmtoffset

by techman2006 (Beadle)
on Apr 01, 2014 at 06:19 UTC ( [id://1080482]=perlquestion: print w/replies, xml ) Need Help??

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

I have a query regarding printing time in below format.

2014-04-01T14:38:27+1:00

Sorry if this query is very novice but I am not able to find the way to do it.

Replies are listed 'Best First'.
Re: Print time in Date"T"time+Gmtoffset
by frozenwithjoy (Priest) on Apr 01, 2014 at 06:38 UTC

    Is the colon required in the offset for your purposes? If not:

    perl -E ' use POSIX; say strftime "%Y-%m-%dT%H:%M:%S%z", localtime(); ' 2014-03-31T23:37:59-0700

    If you want the colon and no zero-padding, you could grab the offset by itself (strftime "%z", localtime();), throw a colon in it and drop the leftmost digit if it is 0. Or there might be an easier way.

      Allowing the leading zero to be dropped would be a bad choice of format as it would no longer be an iso8601 timestamp. (Adding a colon is fine.)

      Thanks but I need a colon in timezone as need to put it in a xml file which understand that notation only.

        then do it in two steps :) second step being adding colon to timezone
Re: Print time in Date"T"time+Gmtoffset ( iso-8601 )
by Anonymous Monk on Apr 01, 2014 at 06:40 UTC
    use DateTime; local $\ = $/; print DateTime->now( qw[ time_zone local ] )->strftime('%F-%H-%M-%S%z' +); print DateTime->now( qw[ time_zone Greenwich ] )->strftime('%F-%H-%M-% +S%z'); __END__ 2014-03-31-23-45-31-0700 2014-04-01-06-45-31+0000
      Those aren't in the format the OP requested, and despite your "tag", those aren't valid iso8601 timestamps either.

        Those aren't in the format the OP requested, and despite your "tag", those aren't valid iso8601 timestamps either.

        And then what happened?

        Not for nothing, but yes, it is valid iso8601 ... and so what if its not 100% what the OP wants?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found