rt 4.0.23
[freeside.git] / rt / lib / RT / Date.pm
index 7a97fbe..4405b07 100644 (file)
@@ -2,7 +2,7 @@
 #
 # COPYRIGHT:
 #
-# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
+# This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
 #                                          <sales@bestpractical.com>
 #
 # (Except where explicitly superseded by other copyright notices)
@@ -208,7 +208,7 @@ sub Set {
         # should be applied to absolute times, so compensate shift in NOW
         my $now = time;
         $now += ($self->Localtime( $args{Timezone}, $now ))[9];
-        my $date = Time::ParseDate::parsedate(
+        my ($date, $error) = Time::ParseDate::parsedate(
             $args{'Value'},
             GMT           => 1,
             NOW           => $now,
@@ -216,6 +216,13 @@ sub Set {
             PREFER_PAST   => RT->Config->Get('AmbiguousDayInPast'),
             PREFER_FUTURE => RT->Config->Get('AmbiguousDayInFuture'),
         );
+        unless ( defined $date ) {
+            $RT::Logger->warning(
+                "Couldn't parse date '$args{'Value'}' by Time::ParseDate"
+            );
+            return $self->Unix(0);
+        }
+
         # apply timezone offset
         $date -= ($self->Localtime( $args{Timezone}, $date ))[9];
 
@@ -502,7 +509,8 @@ Returns new unix time.
 
 sub AddDays {
     my $self = shift;
-    my $days = shift || 1;
+    my $days = shift;
+    $days = 1 unless defined $days;
     return $self->AddSeconds( $days * $DAY );
 }
 
@@ -604,6 +612,10 @@ sub Get
     my $self = shift;
     my %args = (Format => 'ISO', @_);
     my $formatter = $args{'Format'};
+    unless ( $self->ValidFormatter($formatter) ) {
+        RT->Logger->warning("Invalid date formatter '$formatter', falling back to ISO");
+        $formatter = 'ISO';
+    }
     $formatter = 'ISO' unless $self->can($formatter);
     return $self->$formatter( %args );
 }
@@ -642,6 +654,20 @@ sub Formatters
     return @FORMATTERS;
 }
 
+=head3 ValidFormatter FORMAT
+
+Returns a true value if C<FORMAT> is a known formatter.  Otherwise returns
+false.
+
+=cut
+
+sub ValidFormatter {
+    my $self   = shift;
+    my $format = shift;
+    return (grep { $_ eq $format } $self->Formatters and $self->can($format))
+                ? 1 : 0;
+}
+
 =head3 DefaultFormat
 
 =cut
@@ -720,15 +746,19 @@ sub LocalizedDateTime
     my %args = ( Date => 1,
                  Time => 1,
                  Timezone => '',
-                 DateFormat => 'date_format_full',
-                 TimeFormat => 'time_format_medium',
+                 DateFormat => '',
+                 TimeFormat => '',
                  AbbrDay => 1,
                  AbbrMonth => 1,
                  @_,
                );
 
-    my $date_format = $args{'DateFormat'};
-    my $time_format = $args{'TimeFormat'};
+    # Require valid names for the format methods
+    my $date_format = $args{DateFormat} =~ /^\w+$/
+                    ? $args{DateFormat} : 'date_format_full';
+
+    my $time_format = $args{TimeFormat} =~ /^\w+$/
+                    ? $args{TimeFormat} : 'time_format_medium';
 
     my $formatter = $self->LocaleObj;
     $date_format = $formatter->$date_format;
@@ -873,7 +903,7 @@ sub RFC2822 {
 
     my ($date, $time) = ('','');
     $date .= "$DAYS_OF_WEEK[$wday], " if $args{'DayOfWeek'} && $args{'Date'};
-    $date .= "$mday $MONTHS[$mon] $year" if $args{'Date'};
+    $date .= sprintf("%02d %s %04d", $mday, $MONTHS[$mon], $year) if $args{'Date'};
 
     if ( $args{'Time'} ) {
         $time .= sprintf("%02d:%02d", $hour, $min);
@@ -938,21 +968,20 @@ sub iCal {
         Date => 1, Time => 1,
         @_,
     );
-    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
-        $self->Localtime( 'utc' );
-
-    #the month needs incrementing, as gmtime returns 0-11
-    $mon++;
 
     my $res;
     if ( $args{'Date'} && !$args{'Time'} ) {
-        $res = sprintf( '%04d%02d%02d', $year, $mon, $mday );
-    }
-    elsif ( !$args{'Date'} && $args{'Time'} ) {
+        my (undef, undef, undef, $mday, $mon, $year) =
+            $self->Localtime( 'user' );
+        $res = sprintf( '%04d%02d%02d', $year, $mon+1, $mday );
+    } elsif ( !$args{'Date'} && $args{'Time'} ) {
+        my ($sec, $min, $hour) =
+            $self->Localtime( 'utc' );
         $res = sprintf( 'T%02d%02d%02dZ', $hour, $min, $sec );
-    }
-    else {
-        $res = sprintf( '%04d%02d%02dT%02d%02d%02dZ', $year, $mon, $mday, $hour, $min, $sec );
+    } else {
+        my ($sec, $min, $hour, $mday, $mon, $year) =
+            $self->Localtime( 'utc' );
+        $res = sprintf( '%04d%02d%02dT%02d%02d%02dZ', $year, $mon+1, $mday, $hour, $min, $sec );
     }
     return $res;
 }