The ISO 8601 extended time zone designator has a colon in it: -08:00 vis-à-vis 
-0800. DateTime doesn't seem to support the extended format time zone 
designator in its format_cldr method and CLDR patterns. My current workaround 
is simply to add the colon myself.

C:\>type example.pl
#!perl

use strict;
use warnings;

use DateTime;
use DateTime::Format::ISO8601;

my $TO_TIME_ZONE = 'EST5EDT'; # Not 'America/New_York' civil time

while (my $from = <DATA>) {
    chomp $from;
    my $to = ctz($from);
    print "$from $to\n";
}

exit 0;

sub ctz {
    my $timestamp = DateTime::Format::ISO8601->parse_datetime(shift)
                    ->set_time_zone($TO_TIME_ZONE)
                    ->format_cldr('yyyy-MM-ddTHH:mm:ss.SSSZ');

    # Add colon to ISO 8601 time zone designator...
    $timestamp =~ s/(?<=[+-]\d\d)(?=\d\d$)/:/;

    return $timestamp;
}

__END__
1600-12-31T16:00:00.000-08:00
1969-12-31T22:00:00.000-08:00
1980-03-05T22:51:10.000-08:00
2000-04-13T12:23:34.390-07:00
2001-10-27T09:15:00.000-07:00
2001-10-30T07:08:26.000-08:00
2005-01-03T07:29:32.710-08:00
2005-08-17T19:01:00.000-07:00
2009-12-31T22:22:22.222-08:00
2009-01-27T12:15:40.406-08:00
2009-03-07T23:15:00.000-08:00
2009-03-08T02:30:00.000-08:00
2009-03-08T12:45:00.000-08:00
2015-01-28T09:30:00.000-08:00

C:\>perl example.pl
1600-12-31T16:00:00.000-08:00 1600-12-31T19:00:00.000-05:00
1969-12-31T22:00:00.000-08:00 1970-01-01T01:00:00.000-05:00
1980-03-05T22:51:10.000-08:00 1980-03-06T01:51:10.000-05:00
2000-04-13T12:23:34.390-07:00 2000-04-13T15:23:34.390-04:00
2001-10-27T09:15:00.000-07:00 2001-10-27T12:15:00.000-04:00
2001-10-30T07:08:26.000-08:00 2001-10-30T10:08:26.000-05:00
2005-01-03T07:29:32.710-08:00 2005-01-03T10:29:32.710-05:00
2005-08-17T19:01:00.000-07:00 2005-08-17T22:01:00.000-04:00
2009-12-31T22:22:22.222-08:00 2010-01-01T01:22:22.222-05:00
2009-01-27T12:15:40.406-08:00 2009-01-27T15:15:40.406-05:00
2009-03-07T23:15:00.000-08:00 2009-03-08T03:15:00.000-04:00
2009-03-08T02:30:00.000-08:00 2009-03-08T06:30:00.000-04:00
2009-03-08T12:45:00.000-08:00 2009-03-08T16:45:00.000-04:00
2015-01-28T09:30:00.000-08:00 2015-01-28T12:30:00.000-05:00

C:\>


Is there another, better way to handle this? Is DateTime::Format::ISO8601 
likely ever to format dates and times as well as parse them?

-- 
Jim Monty

Reply via email to