[Jifty-commit] r4856 - in jifty/trunk: lib/Jifty

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Jan 15 19:15:13 EST 2008


Author: sartak
Date: Tue Jan 15 19:15:13 2008
New Revision: 4856

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/DateTime.pm

Log:
 r50373 at onn:  sartak | 2008-01-15 18:32:16 -0500
 Improve Jifty::DateTime->new_from_string so that it can handle time zones properly, and add Jifty::DateTime->get_tz_offset.


Modified: jifty/trunk/lib/Jifty/DateTime.pm
==============================================================================
--- jifty/trunk/lib/Jifty/DateTime.pm	(original)
+++ jifty/trunk/lib/Jifty/DateTime.pm	Tue Jan 15 19:15:13 2008
@@ -207,7 +207,7 @@
 sub new_from_string {
     my $class  = shift;
     my $string = shift;
-    my $now;
+    my $epoch;
 
     # Hack to use Date::Manip to flexibly scan dates from strings
     {
@@ -218,23 +218,33 @@
         if($string =~ /^\s* (?:monday|tuesday|wednesday|thursday|friday|saturday|sunday)$/xi) {
             $string = "next $string";
         }
-        
-        # Why are we parsing this as GMT? This feels really wrong.  It will get the wrong answer
-        # if the current user is in another tz.
+
+        my $offset = $class->get_tz_offset;
+        my $dt_now = $class->now;
+        my $now = $dt_now->ymd . ' ' . $dt_now->hms;
+
         require Date::Manip;
-        Date::Manip::Date_Init("TZ=GMT");
-        $now = Date::Manip::UnixDate( $string, "%o" );
+
+        # TZ sets the timezone for parsing
+        # ConvTZ sets the output timezone
+        # ForceDate forces the current date to be now in the user's timezone,
+        #    if we don't set it then DM uses the machine's timezone
+        Date::Manip::Date_Init("TZ=$offset", "ConvTZ=+0000", "ForceDate=$now");
+        $epoch = Date::Manip::UnixDate( $string, "%o" );
     }
 
     # Stop here if Date::Manip couldn't figure it out
-    return undef unless $now;
+    return undef unless $epoch;
 
     # Build a DateTime object from the Date::Manip value and setup the TZ
-    my $self = $class->from_epoch( epoch => $now, time_zone => 'gmt' );
+    my $self = $class->from_epoch( epoch => $epoch, time_zone => 'GMT' );
     if (my $tz = $self->current_user_has_timezone) {
-        $self->set_time_zone("floating")
-            unless ( $self->hour or $self->minute or $self->second );
-        $self->set_time_zone( $tz );
+        if ($self->hour || $self->minute || $self->second) {
+            $self->set_time_zone( $tz );
+        }
+        else {
+            $self->set_time_zone("floating")
+        }
     }
 
     return $self;
@@ -297,6 +307,25 @@
     return 1;
 }
 
+=head2 get_tz_offset [DateTime] -> String
+
+Returns the offset for the current user's timezone. If there is no current
+user, or the current user's time zone is unset, then UTC will be used.
+
+The optional DateTime argument lets you calculate an offset for some time other
+than "right now".
+
+=cut
+
+sub get_tz_offset {
+    my $self = shift;
+    my $dt   = shift || DateTime->now();
+
+    $dt->set_time_zone( $self->current_user_has_timezone || 'UTC' );
+
+    return $dt->strftime("%z");
+}
+
 =head2 jifty_serialize_format
 
 This returns a DateTime (or string) consistent with Jifty's date format.


More information about the Jifty-commit mailing list