On Tue, Dec 15, 2009 at 6:51 PM, Evan Carroll <m...@evancarroll.com> wrote:
> > Why then does parse_datetime automatically convert from a string to a > > timezone? > > Shouldn't it then just always return a datetime in floating context? > > It doesn't "convert" it "reads it as". > > > my $dt = $Strp->parse_datetime($my_string); #Floating > > Not floating, that is whatever the time_zone you have set in the > ::Format module. Or, if the Format module reads in a different time > zone from the string as in the case of %z and %Z in strptime. > I put floating for an example for not passing a time_zone parameter. Also, I've never been able to get %Z to reliably work for parsing, just %z. And the time_zone parameter overrides anything in your string for %z not the other way around: Give it a try: my $dt = DateTime->now; my $string1 = $dt->strftime('%m/%d/%Y %T %Z'); my $string2 = $dt->strftime('%m/%d/%Y %T %z'); print "1: $string1\n"; print "2: $string2\n"; my $Strp1 = new DateTime::Format::Strptime( pattern => '%m/%d/%Y %T %Z', ); my $Strp11 = new DateTime::Format::Strptime( pattern => '%m/%d/%Y %T %Z', time_zone => 'America/New_York', ); my $Strp2 = new DateTime::Format::Strptime( pattern => '%m/%d/%Y %T %z', ); my $Strp21 = new DateTime::Format::Strptime( pattern => '%m/%d/%Y %T %z', time_zone => 'America/New_York', ); my $dt1 = $Strp1->parse_datetime($string1); my $dt11 = $Strp11->parse_datetime($string1); my $dt2 = $Strp2->parse_datetime($string2); my $dt21 = $Strp21->parse_datetime($string2); print "DT 1: " . ($dt1 || "can't do it") . "\n"; print "DT 1 time_zone: " . ($dt1 || "can't do it") . "\n"; print "DT 2: " . ($dt2 || "can't do it") . "\n"; print "DT 2 time_zone: " . ($dt21 || "can't do it") . "\n"; > > > Not overriding time_zone, just format_datetime if and only if a option is > > set. The attribute 'time_zone' seems pretty generic to me, it's not > > 'parse_time_zone' or 'time_zone_from_string'. The confusing part is that > > it's just named 'time_zone'. > > I'm not arguing that it is the best name in the world, I'm just saying > it is simply the respective constructor argument that well behaved > DateTime::Format module will provide under the hood to DateTime. > And that part would not change. > > > Actually that would be pretty cool, because I could parse all of my > DateTime > > strings to UTC and format all my DateTime objects to a desired time_zone > > within a single formatter. > > You can already do this. > > foreach ( qw/timezone list here/ ) { > state $dtf = DateTime::Format::Strptime->new({ time_zone => UTC }); > my $dt = $dtf->parse_datetime( $str ); > $dt->set_time_zone( $_ ); > say "time zone $_"; > say $dtf->format_datetime( $dt ); > } > I agree the difference here is small, but still cool. It is also not what I am advocating for at the moment though: foreach ( qw/timezone list here/ ) { state $dtf = DateTime::Format::Strptime-> > > new({ time_zone => UTC, output_time_zone => 'America/New_York' }); > my $dt = $dtf->parse_datetime( $str ); > say "time zone $_"; > say $dtf->format_datetime( $dt ); > } > It would prob work more like this: #In some master controller or sub my $dtf = DateTime::Format::Strptime->new({ time_zone => UTC, output_time_zone => 'America/New_York' }); #Later on form submission or input from DB my $string_from_form_by_user = $dtf->parse_datetime($string_from_form); #Knows the input should be converted to UTC # LATER SOMEWHERE ELSE, possibly output to web page my $string = $dtf->format_datetime($dt); # Knows the output should be display based on the 'America/New_York' time_zone. > > > Also, why is it practical to do something like the following for parsing > > which you appear to be fine with: > > > > my $Strp = new DateTime::Format::Strptime( > > pattern => $user->datetime_pattern, > > time_zone => 'America/New_York', > > ); > > > > my $Strp2 = new DateTime::Format::Strptime( > > pattern => $user->datetime_pattern, > > time_zone => 'America/Chicago', > > ); > > It is not, this is why I created DateTimeX::Format, because I wanted > each and every DateTimeX::Format module to support a runtime override > for the constructor arguments to DateTimeX::Format modules. So in my > world you could just > > > my $Strp = new DateTimeX::Format::Strptime( > > pattern => $user->datetime_pattern, > > ); > > $Strp->parse_datetime( $str, { time_zone => $tz } ); > Nothing stopping users from: $Strp->parse_datetime( $str, { time_zone => $tz1 } ); $Strp->parse_datetime( $str, { time_zone => $tz2 } ); > > But the difference is I'm not really adding functionality, and nothing > is happening under the hood to the DateTime object, just a new value > will be supplied to the DateTime constructor. > I guess I still don't see how: $Strp->format_datetime( $str, { time_zone => $tz1 } ); $Strp->format_datetime( $str, { time_zone => $tz2 } ); would be so evil. I want to get a formatted string based on pattern I provided as it would be displayed in the specified time_zone. > > -- > Evan Carroll > System Lord of the Internets > http://www.evancarroll.com >