How to print date and time in xs: dateTime format in Perl?

I would like to take a timestamp (e.g. 1263531246) and convert it to a string representation suitable for output to an XML file in the attribute field corresponding to xs:dateTime. xs:dateTimeexpecting something like:

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

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

+3
source share
2 answers

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

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"

Windows:

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

Date::Format - :

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

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

:

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

DateTime , DateTime - strftime() .

, XSD ( ISO8601, XML-): . DateTime::Format::XSD.

use DateTime;
use DateTime::Format::XSD;

my $dt = DateTime->now;
print DateTime::Format::XSD->format_datetime($dt);

:

2010-02-04T23:24:11+00:00

DateTime, , ; 'formatter' DateTime:

my $dt = DateTime->new(year => 1999, month => 1, day => 1,
                       formatter => 'DateTime::Format::XSD'
                      );

my $xml = "<date>$dt</date>";   # through the magic of overloading, this works!

:

<date>1999-01-01T00:00:00+00:00</date>

. http://search.cpan.org/dist/DateTime/lib/DateTime.pm#Formatters_And_Stringification.

+7

Source: https://habr.com/ru/post/1731466/


All Articles