[Rt-commit] r5415 - in rt/branches/3.7-EXPERIMENTAL: .

ruz at bestpractical.com ruz at bestpractical.com
Thu Jun 22 00:59:41 EDT 2006


Author: ruz
Date: Thu Jun 22 00:59:39 2006
New Revision: 5415

Modified:
   rt/branches/3.7-EXPERIMENTAL/   (props changed)
   rt/branches/3.7-EXPERIMENTAL/lib/RT/Date.pm
   rt/branches/3.7-EXPERIMENTAL/lib/RT/Ticket_Overlay.pm

Log:
 r3297 at cubic-pc:  cubic | 2006-06-22 08:55:58 +0400
 * $MONTH != (4 * $WEEK == 28 * $DAY), only one month has 28 days,
   use ((365*3+366)/(4*12) * $DAY) which is much closer to the truth
 ** similar logic with number of days in year
 * add support for timezones all over the lib, with backward compatible
   defaults
 * in DurationAsString use math round functions instead of plain
   perl's int
 * new method Timelocal, timezone dependant wrapper over timelocal and
   timegm from Time::Local module. Reverse version of Localtime method.
 * add and update docs
 * get rid of /^# ({{{|}}})/, inline tests
 


Modified: rt/branches/3.7-EXPERIMENTAL/lib/RT/Date.pm
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/lib/RT/Date.pm	(original)
+++ rt/branches/3.7-EXPERIMENTAL/lib/RT/Date.pm	Thu Jun 22 00:59:39 2006
@@ -83,8 +83,8 @@
 $HOUR   = 60 * $MINUTE;
 $DAY    = 24 * $HOUR;
 $WEEK   = 7 * $DAY;
-$MONTH  = 4 * $WEEK;
-$YEAR   = 365 * $DAY;
+$MONTH  = 30.4375 * $DAY;
+$YEAR   = 365.25 * $DAY;
 
 our @MONTHS = qw(
     Jan
@@ -111,8 +111,6 @@
     Sat
 );
 
-# {{{ sub new
-
 =head2 new
 
 Object constructor takes one argument C<RT::CurrentUser> object.
@@ -129,15 +127,11 @@
     return $self;
 }
 
-# }}}
-
-# {{{ sub Set
-
 =head2 Set
 
-Takes a param hash with the fields 'Format' and 'Value'
+Takes a param hash with the fields C<Format>, C<Value> and C<Timezone>.
 
-if $args->{'Format'} is 'unix', takes the number of seconds since the epoch 
+If $args->{'Format'} is 'unix', takes the number of seconds since the epoch.
 
 If $args->{'Format'} is ISO, tries to parse an ISO date.
 
@@ -146,110 +140,96 @@
 within RT's core. But it's really useful for something like the textbox date
 entry where we let the user do whatever they want.
 
-If $args->{'Value'}  is 0, assumes you mean never.
-
-=begin testing
-
-use_ok(RT::Date);
-my $date = RT::Date->new($RT::SystemUser);
-$date->Set(Format => 'unix', Value => '0');
-ok ($date->ISO eq '1970-01-01 00:00:00', "Set a date to midnight 1/1/1970 GMT");
-
-=end testing
+If $args->{'Value'} is 0, assumes you mean never.
 
 =cut
 
 sub Set {
     my $self = shift;
-    my %args = ( Format => 'unix',
-                 Value  => time,
-                 @_ );
-
-    unless( $args{'Value'} ) {
-        $self->Unix(-1);
-        return ( $self->Unix() );
-    }
+    my %args = (
+        Format   => 'unix',
+        Value    => time,
+        Timezone => 'user',
+        @_
+    );
+
+    return $self->Unix(0) unless $args{'Value'};
 
     if ( $args{'Format'} =~ /^unix$/i ) {
-        $self->Unix( $args{'Value'} );
+        return $self->Unix( $args{'Value'} );
     }
     elsif ( $args{'Format'} =~ /^(sql|datemanip|iso)$/i ) {
         $args{'Value'} =~ s!/!-!g;
 
-        if (( $args{'Value'} =~ /^(\d{4}?)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/ )
-            || ( $args{'Value'} =~
-                 /^(\d{4}?)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/ )
-            || ( $args{'Value'} =~
-                 /^(\d{4}?)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)\+00$/ )
-            || ($args{'Value'} =~ /^(\d{4}?)(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/ )
+        if (   ( $args{'Value'} =~ /^(\d{4})?(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/ )
+            || ( $args{'Value'} =~ /^(\d{4})?(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/ )
+            || ( $args{'Value'} =~ /^(?:(\d{4})-)?(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/ )
+            || ( $args{'Value'} =~ /^(?:(\d{4})-)?(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)\+00$/ )
           ) {
 
-            my $year  = $1;
-            my $mon   = $2;
-            my $mday  = $3;
-            my $hours = $4;
-            my $min   = $5;
-            my $sec   = $6;
+            my ($year, $mon, $mday, $hours, $min, $sec)  = ($1, $2, $3, $4, $5, $6);
+
+            # use current year if string has no value
+            $year ||= (localtime time)[5] + 1900;
 
             #timegm expects month as 0->11
             $mon--;
 
-            #now that we've parsed it, deal with the case where everything
-            #was 0
-            return ($self->Unix(-1)) if $mon < 0 || $mon > 11;
-
-            #Dateamnip strings aren't in GMT.
-            if ( $args{'Format'} =~ /^datemanip$/i ) {
-                $self->Unix(
-                      timelocal( $sec, $min, $hours, $mday, $mon, $year ) );
-            }
-
-            #ISO and SQL dates are in GMT
-            else {
-                $self->Unix(
-                         timegm( $sec, $min, $hours, $mday, $mon, $year ) );
-            }
+            #now that we've parsed it, deal with the case where everything was 0
+            return $self->Unix(0) if $mon < 0 || $mon > 11;
 
-            $self->Unix(-1) unless $self->Unix;
+            my $tz = lc $args{'Format'} eq 'datemanip'? 'user': 'utc';
+            $self->Unix( $self->Timelocal( $tz, $sec, $min, $hours, $mday, $mon, $year ) );
+
+            $self->Unix(0) unless $self->Unix > 0;
         }
         else {
-            require Carp;
-            Carp::cluck;
-            $RT::Logger->debug(
-                     "Couldn't parse date $args{'Value'} as a $args{'Format'}");
-
+            $RT::Logger->warning(
+                "Couldn't parse date '$args{'Value'}' as a $args{'Format'} format"
+            );
+            return $self->Unix(0);
         }
     }
     elsif ( $args{'Format'} =~ /^unknown$/i ) {
         require Time::ParseDate;
+        # the module supports only legacy timezones like PDT or EST...
+        # so we parse date as GMT and later apply offset
+        my $date = Time::ParseDate::parsedate(
+            $args{'Value'},
+            GMT           => 1,
+            UK            => RT->Config->Get('DateDayBeforeMonth'),
+            PREFER_PAST   => RT->Config->Get('AmbiguousDayInPast'),
+            PREFER_FUTURE => !RT->Config->Get('AmbiguousDayInPast')
+        );
+        # apply timezone offset
+        $date -= ($self->Localtime( $args{Timezone}, $date ))[9];
+
+        $RT::Logger->debug(
+            "RT::Date used Time::ParseDate to make '$args{'Value'}' $date\n"
+        );
 
-        #Convert it to an ISO format string
-
-        my $date = Time::ParseDate::parsedate($args{'Value'},
-                        GMT => 0,
-                        UK => RT->Config->Get('DateDayBeforeMonth'),
-                        PREFER_PAST => RT->Config->Get('AmbiguousDayInPast'),
-                        PREFER_FUTURE => !RT->Config->Get('AmbiguousDayInPast') );
-
-        #This date has now been set to a date in the _local_ timezone.
-        #since ISO dates are known to be in GMT (for RT's purposes);
-
-        $RT::Logger->debug( "RT::Date used date::parse to make "
-                            . $args{'Value'}
-                            . " $date\n" );
-
-        return ( $self->Set( Format => 'unix', Value => $date) );
+        return $self->Set( Format => 'unix', Value => $date);
     }
     else {
-        die "Unknown Date format: " . $args{'Format'} . "\n";
+        $RT::Logger->error(
+            "Unknown Date format: $args{'Format'}\n"
+        );
+        return $self->Unix(0);
     }
 
-    return ( $self->Unix() );
+    return $self->Unix;
 }
 
-# }}}
+=head2 SetToNow
 
-# {{{ sub SetToMidnight 
+Set the object's time to the current time. Takes no arguments
+and returns unix time.
+
+=cut
+
+sub SetToNow {
+    return $_[0]->Unix(time);
+}
 
 =head2 SetToMidnight [Timezone => 'utc']
 
@@ -260,39 +240,32 @@
 
 =over 4
 
-=item Timezone - Timezone context C<server> or C<UTC>
-
-=cut
+=item Timezone
 
-sub SetToMidnight {
-    my $self = shift;
-    my %args = ( Timezone => 'UTC', @_ );
-    if ( lc $args{'Timezone'} eq 'server' ) {
-        $self->Unix( timelocal( 0,0,0,(localtime $self->Unix)[3..7] ) );
-    } else {
-        $self->Unix( timegm( 0,0,0,(gmtime $self->Unix)[3..7] ) );
-    }
-    return ($self->Unix);
-}
+Timezone context C<user>, C<server> or C<UTC>. See also L</Timezone>.
 
+=back
 
-# }}}
+=cut
 
-# {{{ sub SetToNow
-sub SetToNow {
+sub SetToMidnight {
     my $self = shift;
-    return($self->Set(Format => 'unix', Value => time))
+    my %args = ( Timezone => '', @_ );
+    my $new = $self->Timelocal(
+        $args{'Timezone'},
+        0,0,0,($self->Localtime( $args{'Timezone'} ))[3..9]
+    );
+    return $self->Unix( $new );
 }
-# }}}
-
-# {{{ sub Diff
 
 =head2 Diff
 
-Takes either an RT::Date object or the date in unixtime format as a string, if
-nothing is specified uses the current time.
+Takes either an C<RT::Date> object or the date in unixtime format as a string,
+if nothing is specified uses the current time.
 
-Returns the differnce between $self and that time as a number of seconds
+Returns the differnce between the time in the current object and that time
+as a number of seconds. Returns C<undef> if any of two compared values is
+incorrect or not set.
 
 =cut
 
@@ -300,93 +273,81 @@
     my $self = shift;
     my $other = shift;
     $other = time unless defined $other;
-
     if ( UNIVERSAL::isa( $other, 'RT::Date' ) ) {
         $other = $other->Unix;
     }
+    return undef unless $other > 0;
 
-    return ($self->Unix - $other);
-}
-# }}}
+    my $unix = $self->Unix;
+    return undef unless $unix > 0;
 
-# {{{ sub DiffAsString
+    return $unix - $other;
+}
 
 =head2 DiffAsString
 
-Takes either an RT::Date object or the date in unixtime format as a string
+Takes either an C<RT::Date> object or the date in unixtime format as a string,
+if nothing is specified uses the current time.
 
-Returns the differnce between $self and that time as a number of seconds as
-as string fit for human consumption
+Returns the differnce between C<$self> and that time as a number of seconds as
+a localized string fit for human consumption. Returns empty string if any of
+two compared values is incorrect or not set.
 
 =cut
 
 sub DiffAsString {
     my $self = shift;
-    my $other = shift;
-
-    if ($other < 1) {
-        return ("");
-    }
-    if ($self->Unix < 1) {
-        return("");
-    }
-    my $diff = $self->Diff($other);
+    my $diff = $self->Diff( @_ );
+    return '' unless defined $diff;
 
-    return ($self->DurationAsString($diff));
+    return $self->DurationAsString( $diff );
 }
-# }}}
-
-# {{{ sub DurationAsString
-
 
 =head2 DurationAsString
 
-Takes a number of seconds. returns a string describing that duration
+Takes a number of seconds. Returns a localized string describing
+that duration.
 
 =cut
 
 sub DurationAsString {
-
     my $self     = shift;
-    my $duration = shift;
-
-    my ( $negative, $s );
+    my $duration = int shift;
 
-    $negative = 1 if ( $duration < 0 );
+    my ( $negative, $s, $time_unit );
+    $negative = 1 if $duration < 0;
+    $duration = abs $duration;
 
-    $duration = abs($duration);
-
-    my $time_unit;
     if ( $duration < $MINUTE ) {
         $s         = $duration;
         $time_unit = $self->loc("sec");
     }
     elsif ( $duration < ( 2 * $HOUR ) ) {
-        $s         = int( $duration / $MINUTE );
+        $s         = int( $duration / $MINUTE + 0.5 );
         $time_unit = $self->loc("min");
     }
     elsif ( $duration < ( 2 * $DAY ) ) {
-        $s         = int( $duration / $HOUR );
+        $s         = int( $duration / $HOUR + 0.5 );
         $time_unit = $self->loc("hours");
     }
     elsif ( $duration < ( 2 * $WEEK ) ) {
-        $s         = int( $duration / $DAY );
+        $s         = int( $duration / $DAY + 0.5 );
         $time_unit = $self->loc("days");
     }
     elsif ( $duration < ( 2 * $MONTH ) ) {
-        $s         = int( $duration / $WEEK );
+        $s         = int( $duration / $WEEK + 0.5 );
         $time_unit = $self->loc("weeks");
     }
     elsif ( $duration < $YEAR ) {
-        $s         = int( $duration / $MONTH );
+        $s         = int( $duration / $MONTH + 0.5 );
         $time_unit = $self->loc("months");
     }
     else {
-        $s         = int( $duration / $YEAR );
+        $s         = int( $duration / $YEAR + 0.5 );
         $time_unit = $self->loc("years");
     }
 
-    if ($negative) {
+    if ( $negative ) {
         return $self->loc( "[_1] [_2] ago", $s, $time_unit );
     }
     else {
@@ -394,37 +355,34 @@
     }
 }
 
-# }}}
-
-# {{{ sub AgeAsString
-
 =head2 AgeAsString
 
-Takes nothing
-
-Returns a string that's the differnce between the time in the object and now
+Takes nothing. Returns a string that's the differnce between the
+time in the object and now.
 
 =cut
 
-sub AgeAsString {
-    my $self = shift;
-    return ($self->DiffAsString(time));
-}
-# }}}
+sub AgeAsString { return $_[0]->DiffAsString }
+
 
-# {{{ sub AsString
 
 =head2 AsString
 
-Returns the object's time as a string with the current timezone.
+Returns the object's time as a localized string with curent user's prefered
+format and timezone.
+
+If the current user didn't choose prefered format then system wide setting is
+used or L</DefaultFormat> if the latter is not specified. See config option
+C<DateTimeFormat>.
 
 =cut
 
 sub AsString {
     my $self = shift;
-    return $self->loc("Not set") if $self->Unix <= 0;
-
     my %args = (@_);
+
+    return $self->loc("Not set") unless $self->Unix > 0;
+
     my $format = RT->Config->Get( 'DateTimeFormat', $self->CurrentUser ) || 'DefaultFormat';
     $format = { Format => $format } unless ref $format;
     %args = (%$format, %args);
@@ -432,13 +390,11 @@
     return $self->Get( Timezone => 'user', %args );
 }
 
-# }}}
-
-# {{{ GetWeekday
-
 =head2 GetWeekday DAY
 
-Takes an integer day of week and returns a localized string for that day of week
+Takes an integer day of week and returns a localized string for
+that day of week. Valid values are from range 0-6, Note that B<0
+is sunday>.
 
 =cut
 
@@ -450,13 +406,10 @@
     return '';
 }
 
-# }}}
-
-# {{{ GetMonth
+=head2 GetMonth MONTH
 
-=head2 GetMonth DAY
-
-Takes an integer month and returns a localized string for that month 
+Takes an integer month and returns a localized string for that month.
+Valid values are from from range 0-11.
 
 =cut
 
@@ -468,15 +421,11 @@
     return '';
 }
 
-# }}}
-
-# {{{ sub AddSeconds
+=head2 AddSeconds SECONDS
 
-=head2 AddSeconds
+Takes a number of seconds and returns the new unix time.
 
-Takes a number of seconds as a string
-
-Returns the new time
+Negative value can be used to substract seconds.
 
 =cut
 
@@ -485,48 +434,33 @@
     my $delta = shift or return $self->Unix;
     
     $self->Set(Format => 'unix', Value => ($self->Unix + $delta));
-    
+ 
     return ($self->Unix);
-    
-
 }
 
-# }}}
+=head2 AddDays [DAYS]
 
-# {{{ sub AddDays
+Adds C<24 hours * DAYS> to the current time. Adds one day when
+no argument is specified. Negative value can be used to substract
+days.
 
-=head2 AddDays $DAYS
-
-Adds 24 hours * $DAYS to the current time
+Returns new unix time.
 
 =cut
 
 sub AddDays {
     my $self = shift;
-    my $days = shift;
-    $self->AddSeconds($days * $DAY);
-    
+    my $days = shift || 1;
+    return $self->AddSeconds( $days * $DAY );
 }
 
-# }}}
-
-# {{{ sub AddDay
-
 =head2 AddDay
 
-Adds 24 hours to the current time
+Adds 24 hours to the current time. Returns new unix time.
 
 =cut
 
-sub AddDay {
-    my $self = shift;
-    $self->AddSeconds($DAY);
-    
-}
-
-# }}}
-
-# {{{ sub Unix
+sub AddDay { return $_[0]->AddSeconds($DAY) }
 
 =head2 Unix [unixtime]
 
@@ -536,16 +470,17 @@
 =cut
 
 sub Unix {
-    my $self = shift;
-    
-    $self->{'time'} = (shift || 0) if (@_);
-    
-    return ($self->{'time'});
+    my $self = shift; 
+    $self->{'time'} = int(shift || 0) if @_;
+    return $self->{'time'};
 }
-# }}}
 
 =head2 DateTime
 
+Alias for L</Get> method. Arguments C<Date> and <Time>
+are fixed to true values, other arguments could be used
+as described in L</Get>.
+
 =cut
 
 sub DateTime {
@@ -553,8 +488,6 @@
     return $self->Get( @_, Date => 1, Time => 1 );
 }
 
-# {{{ sub Date
-
 =head2 Date
 
 Takes Format argument which allows you choose date formatter.
@@ -569,16 +502,8 @@
     return $self->Get( @_, Date => 1, Time => 0 );
 }
 
-# }}}}
-
-# {{{ sub Time
-
 =head2 Time
 
-Takes nothing
-
-Returns the object's time in hh:mm:ss format; this is the same as
-the ISO format without the date
 
 =cut
 
@@ -587,14 +512,19 @@
     return $self->Get( @_, Date => 0, Time => 1 );
 }
 
-# }}}}
+=head2 Get
+
+Returnsa a formatted and localized string that represets time of
+the current object.
+
+
+=cut
 
 sub Get
 {
     my $self = shift;
-    my %args = (Format => 'ISO',
-                @_);
-    my $formatter =$args{'Format'};
+    my %args = (Format => 'ISO', @_);
+    my $formatter = $args{'Format'};
     $formatter = 'ISO' unless $self->can($formatter);
     return $self->$formatter( %args );
 }
@@ -603,6 +533,7 @@
 
 Fomatter is a method that returns date and time in different configurable
 format.
+
 Each method takes several arguments:
 
 =over 1
@@ -639,26 +570,26 @@
     $mon = $self->GetMonth($mon);
     ($mday, $hour, $min, $sec) = map { sprintf "%02d", $_ } ($mday, $hour, $min, $sec);
 
-    if(!$args{'Date'} && !$args{'Time'}) {
-        return '';
-    } elsif($args{'Date'} && $args{'Time'}) {
-        return $self->loc('[_1] [_2] [_3] [_4]:[_5]:[_6] [_7]',
-                          $wday,$mon,$mday,$hour,$min,$sec,$year);
-    } elsif($args{'Date'}) {
+    if( $args{'Date'} && !$args{'Time'} ) {
         return $self->loc('[_1] [_2] [_3] [_4]',
                           $wday,$mon,$mday,$year);
-    } else {
+    } elsif( !$args{'Date'} && $args{'Time'} ) {
         return $self->loc('[_1]:[_2]:[_3]',
                           $hour,$min,$sec);
+    } else {
+        return $self->loc('[_1] [_2] [_3] [_4]:[_5]:[_6] [_7]',
+                          $wday,$mon,$mday,$hour,$min,$sec,$year);
     }
 }
 
 =head3 ISO
 
-Returns the object's date in ISO format
-Takes additional argument C<Seconds>.
-ISO format has no locale dependant elements so method
-ignore argument C<Localize>.
+Returns the object's date in ISO format C<YYYY-MM-DD mm:hh:ss>.
+ISO format is locale independant, but adding timezone offset info
+is not implemented yet.
+
+Supports arguments: C<Timezone>, C<Date>, C<Time> and C<Seconds>.
+See </Output formatters> for description of arguments.
 
 =cut
 
@@ -676,37 +607,71 @@
 
     #the month needs incrementing, as gmtime returns 0-11
     $mon++;
-    
+
     my $res = '';
     $res .= sprintf("%04d-%02d-%02d", $year, $mon, $mday) if $args{'Date'};
     $res .= sprintf(' %02d:%02d', $hour, $min) if $args{'Time'};
     $res .= sprintf(':%02d', $sec, $min) if $args{'Time'} && $args{'Seconds'};
     $res =~ s/^\s+//;
-    
+
     return $res;
 }
 
 =head3 W3CDTF
 
-Returns the object's date and time in W3C DTF format
+Returns the object's date and time in W3C date time format
+(L<http://www.w3.org/TR/NOTE-datetime>).
+
+Format is locale independand and is close enought to ISO, but
+note that date part is B<not optional> and output string
+has timezone offset mark in C<[+-]hh:mm> format.
+
+Supports arguments: C<Timezone>, C<Time> and C<Seconds>.
+See </Output formatters> for description of arguments.
 
 =cut
 
 sub W3CDTF {
     my $self = shift;
-    my %args = ( Time => 1,
-                 @_,
-               );
-    my $date = $self->ISO(%args);
-    $date .= 'Z' if $args{'Time'};
-    $date =~ s/ /T/;
-    return $date;
+    my %args = (
+        Time => 1,
+        Timezone => '',
+        Seconds => 1,
+        @_,
+        Date => 1,
+    );
+       #  0    1    2     3     4    5     6     7      8      9
+    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
+                            $self->Localtime( $args{'Timezone'} );
+
+    #the month needs incrementing, as gmtime returns 0-11
+    $mon++;
+
+    my $res = '';
+    $res .= sprintf("%04d-%02d-%02d", $year, $mon, $mday);
+    if ( $args{'Time'} ) {
+        $res .= sprintf('T%02d:%02d', $hour, $min);
+        $res .= sprintf(':%02d', $sec, $min) if $args{'Seconds'};
+        if ( $offset ) {
+            $res .= sprintf "%s%02d:%02d", $self->_SplitOffset( $offset );
+        } else {
+            $res .= 'Z';
+        }
+    }
+
+    return $res;
 };
 
 
 =head3 RFC2822
 
-Returns the object's date and time in RFC2822 format
+Returns the object's date and time in RFC2822 format.
+Format is locale independand as required by RFC. Time
+part always has timezone offset.
+
+Supports arguments: C<Timezone>, C<Date>, C<Time>, C<DayOfWeek>
+and C<Seconds>. See </Output formatters> for description of
+arguments.
 
 =cut
 
@@ -720,7 +685,6 @@
                  @_,
                );
 
-    # XXX: Don't know how to get timezone shift by timezone name
        #  0    1    2     3     4    5     6     7      8     9
     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
                             $self->Localtime($args{'Timezone'});
@@ -728,33 +692,40 @@
     my ($date, $time) = ('','');
     $date .= "$DAYS_OF_WEEK[$wday], " if $args{'DayOfWeek'} && $args{'Date'};
     $date .= "$mday $MONTHS[$mon] $year" if $args{'Date'};
-    
-    $time .= sprintf("%02d:%02d", $hour, $min) if $args{'Time'};
-    $time .= sprintf(":%02d", $sec) if $args{'Time'} && $args{'Seconds'};
-    if( $args{'Time'} ) {
-        my $sign = $offset<0? '-': '+';
-        $offset = abs $offset;
-        $offset = int($offset/60);
-        my $offset_mins = $offset % 60;
-        my $offset_hours = int($offset/60);
-        $time .= sprintf(" $sign%02d%02d", $offset_hours, $offset_mins);
+
+    if ( $args{'Time'} ) {
+        $time .= sprintf("%02d:%02d", $hour, $min);
+        $time .= sprintf(":%02d", $sec) if $args{'Seconds'};
+        $time .= sprintf " %s%02d%02d", $self->_SplitOffset( $offset );
     }
-    
+
     return join ' ', grep $_, ($date, $time);
 }
 
+sub _SplitOffset {
+    my ($self, $offset) = @_;
+    my $sign = $offset < 0? '-': '+';
+    $offset = int( (abs $offset) / 60 + 0.001 );
+    my $mins = $offset % 60;
+    my $hours = int( $offset/60 + 0.001 );
+    return $sign, $hours, $mins; 
+}
+
 =head2 Timezones handling
 
-=head3 Localtime $context
+=head3 Localtime $context [$time]
 
-Takes one argument C<$context>, which determines whether we want "user local", "system" or "UTC" time.
+Takes one mandatory argument C<$context>, which determines whether
+we want "user local", "system" or "UTC" time. Also, takes optional
+argument unix C<$time>, default value is the current unix time.
 
-Returns object's date and time in the format provided by perl's builtin function C<localtime>
-with two exceptions:
+Returns object's date and time in the format provided by perl's
+builtin functions C<localtime> and C<gmtime> with two exceptions:
 
 1) "Year" is a four-digit year, rather than "years since 1900"
 
-2) The last element of the array returned is C<offset>, which represents timezone offset against C<UTC> in seconds.
+2) The last element of the array returned is C<offset>, which
+represents timezone offset against C<UTC> in seconds.
 
 =cut
 
@@ -763,11 +734,11 @@
     my $self = shift;
     my $tz = $self->Timezone(shift);
 
-    my $unix = $self->Unix;
-    $unix = 0 if $unix < 0;
+    my $unix = shift || $self->Unix;
+    $unix = 0 unless $unix >= 0;
     
     my @local;
-    if ($tz eq 'GMT' or $tz eq 'UTC') {
+    if ($tz eq 'UTC') {
         @local = gmtime($unix);
     } else {
         local $ENV{'TZ'} = $tz;
@@ -778,10 +749,40 @@
     return @local, $offset;
 }
 
+=head3 Timelocal $context @time
+
+Takes argument C<$context>, which determines whether we should
+treat C<@time> as "user local", "system" or "UTC" time.
+
+C<@time> is array returned by L<Localtime> functions. Only first
+six elements are mandatory - $sec, $min, $hour, $mday, $mon and $year.
+You may pass $wday, $yday and $isdst, these are ignored.
+
+If you pass C<$offset> as ninth argument, it's used instead of
+C<$context>. It's done such way as code 
+C<$self->Timelocal('utc', $self->Localtime('server'))> doesn't
+makes much sense and most probably would produce unexpected
+result, so the method ignore 'utc' context and uses offset
+returned by L<Localtime> method.
+
+=cut
 
-# }}}
+sub Timelocal {
+    my $self = shift;
+    my $tz = shift;
+    if ( defined $_[9] ) {
+        return timegm(@_[0..5]) - $_[9];
+    } else {
+        $tz = $self->Timezone( $tz );
+        if ($tz eq 'UTC') {
+            return Time::Local::timegm(@_[0..5]);
+        } else {
+            local $ENV{'TZ'} = $tz;
+            return Time::Local::timelocal(@_[0..5]);
+        }
+    }
+}
 
-# {{{ sub Timezone
 
 =head3 Timezone $context
 
@@ -789,7 +790,7 @@
 
 Takes one argument, C<$context> argument which could be C<user>, C<server> or C<utc>.
 
-=over 
+=over
 
 =item user
 
@@ -797,7 +798,7 @@
 
 =item server
 
-If context is C<server> it returns value of the <$Timezone> RT config option.
+If context is C<server> it returns value of the C<Timezone> RT config option.
 
 =item  utc
 
@@ -805,7 +806,6 @@
 
 =back
 
-
 =cut
 
 sub Timezone {
@@ -822,11 +822,11 @@
     } else {
         $tz = 'UTC';
     }
-
-    return ($tz || RT->Config->Get('Timezone') || 'UTC');
+    $tz ||= RT->Config->Get('Timezone') || 'UTC';
+    $tz = 'UTC' if lc $tz eq 'gmt';
+    return $tz;
 }
 
-# }}}
 
 eval "require RT::Date_Vendor";
 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Date_Vendor.pm});

Modified: rt/branches/3.7-EXPERIMENTAL/lib/RT/Ticket_Overlay.pm
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/lib/RT/Ticket_Overlay.pm	(original)
+++ rt/branches/3.7-EXPERIMENTAL/lib/RT/Ticket_Overlay.pm	Thu Jun 22 00:59:39 2006
@@ -340,7 +340,7 @@
 ok ( my $id = $t->Id, "Got ticket id");
 ok ($t->RefersTo->First->Target =~ /fsck.com/, "Got refers to");
 ok ($t->ReferredToBy->First->Base =~ /cpan.org/, "Got referredtoby");
-ok ($t->ResolvedObj->Unix == -1, "It hasn't been resolved - ". $t->ResolvedObj->Unix);
+is ($t->ResolvedObj->Unix, 0, "It hasn't been resolved - ". $t->ResolvedObj->Unix);
 
 =end testing
 


More information about the Rt-commit mailing list