SetJmp SetJmp - 2 months ago 9
Perl Question

How can I print a datetime in the xs:dateTime format in Perl?

I would like to take a time stamp (e.g. 1263531246) and convert it to a string representation suitable for output to an XML file in an attribute field conforming to

xs:dateTime
.
xs:dateTime
expects something like:

2002-05-30T09:30:10-06:00


Ideally, I would use the form of output that includes offset from UTC (as above). In this project, I am constrained to use Perl. Any suggestions?

Answer

This works on Linux:

$ perl -MPOSIX -e 'print POSIX::strftime("%Y-%m-%dT%H:%M:%S%z\n", localtime)'
2010-02-04T17:37:43-0500

On Windows, with ActiveState Perl, it prints:

2010-02-04T17:39:24Eastern Standard Time

Using DateTime:

#!/usr/bin/perl
use strict; use warnings;

use DateTime;
my $dt = DateTime->now(time_zone => 'EST');
print $dt->strftime('%Y-%m-%dT%H:%M:%S%z'), "\n"

I get the correct string on Windows as well:

E:\> t
2010-02-04T18:06:24-0500

I believe Date::Format is much lighter weight module:

#!/usr/bin/perl
use strict; use warnings;

use Date::Format;
print time2str('%Y-%m-%dT%H:%M:%S%z', time, 'EST'), "\n";

Output:

E:\> t
2010-02-04T18:11:36-0500
Comments