Hi DateTime enthusiasts,
DateTime::Format::* objects are accepting a 'time_zone' argument,
which lets you conveniently set the time zone in the DateTime
object returned by parse_datetime().
However, DateTime::Format::Strptime (and other formatters) serve
another purpose as well: They're used as output formatters, set via
DateTime's set_formatter() method, and controlling the stringification
of the DateTime object.
Now, regardless what the time_zone setting is in a DateTime's formatter,
the stringified representation of it remains the same.
That's quite confusing. I think the formatter should either convert the
DateTime object's datetime to the formatter's time_zone or the output
formatter shouldn't offer a time_zone setting (therefore be different
from the parse input formatter which needs it).
Here's the status quo, showing that the time_zone setting of the
formatter doesn't affect the output.
use DateTime;
use DateTime::Format::Strptime;
my $format1 = new DateTime::Format::Strptime(
pattern => "%Y/%m/%d %H:%M:%S",
time_zone => 'Asia/Tokyo' );
my $format2 = new DateTime::Format::Strptime(
pattern => "%Y/%m/%d %H:%M:%S",
time_zone => 'America/Los_Angeles' );
my $dt = DateTime->new(
year => 2006,
month => 1,
day => 1,
hour => 1);
# 2006-01-01T01:00:00
print "$dt\n";
$dt->set_formatter($format1);
# 2006/01/01 01:00:00
print "$dt\n";
$dt->set_formatter($format2);
# 2006/01/01 01:00:00
print "$dt\n";
What do you think?
Another thing I found counter-intuitive was that if parse_datetime() is
used with a formatter that doesn't have a time zone, it returns a
DateTime object, which, if you set its time zone, changes its
value:
use DateTime;
use DateTime::Format::Strptime;
my $format = new DateTime::Format::Strptime(
pattern => "%Y/%m/%d %H:%M:%S",
);
my $dt = $format->parse_datetime(
"2006/01/01 05:00:00");
$dt->set_time_zone("Asia/Tokyo");
# Prints "2006-01-01T14:00:00"
print "$dt\n";
Shouldn't parse_datetime() create a DateTime object with a floating time zone
in this case?
Anyway, great work, guys!
-- Mike
Mike Schilli
[EMAIL PROTECTED]