[Rt-commit] rt branch, 4.2/rt-date-strftime, created. rt-4.2.12-2-gb53fa71

Shawn Moore shawn at bestpractical.com
Tue Nov 24 15:29:17 EST 2015


The branch, 4.2/rt-date-strftime has been created
        at  b53fa7120fc9735e43a1ba7e55c810b13c551acf (commit)

- Log -----------------------------------------------------------------
commit 99d8560299c4a985d664f1ce7b7ed8bf248e4fea
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Tue Nov 24 20:27:59 2015 +0000

    Factor a DateTimeObj method out of LocalizedDateTime

diff --git a/lib/RT/Date.pm b/lib/RT/Date.pm
index 4344865..e5914b7 100644
--- a/lib/RT/Date.pm
+++ b/lib/RT/Date.pm
@@ -762,6 +762,8 @@ sub LocalizedDateTime
                  @_,
                );
 
+    my $dt = $self->DateTimeObj;
+
     # Require valid names for the format methods
     my $date_format = $args{DateFormat} =~ /^\w+$/
                     ? $args{DateFormat} : 'date_format_full';
@@ -775,24 +777,6 @@ sub LocalizedDateTime
     $date_format =~ s/EEEE/EEE/g if ( $args{'AbbrDay'} );
     $date_format =~ s/MMMM/MMM/g if ( $args{'AbbrMonth'} );
 
-    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
-                            $self->Localtime($args{'Timezone'});
-    $mon++;
-    my $tz = $self->Timezone($args{'Timezone'});
-
-    # FIXME : another way to call this module without conflict with local
-    # DateTime method?
-    my $dt = DateTime::->new( locale => $formatter,
-                            time_zone => $tz,
-                            year => $year,
-                            month => $mon,
-                            day => $mday,
-                            hour => $hour,
-                            minute => $min,
-                            second => $sec,
-                            nanosecond => 0,
-                          );
-
     if ( $args{'Date'} && !$args{'Time'} ) {
         return $dt->format_cldr($date_format);
     } elsif ( !$args{'Date'} && $args{'Time'} ) {
@@ -1163,6 +1147,41 @@ sub IsSet {
 
 }
 
+=head3 DateTimeObj [Timezone => 'utc']
+
+Returns an L<DateTime> object representing the same time as this RT::Date. The
+DateTime object's locale is set up to match the user's language.
+
+Modifying this DateTime object will not change the corresponding RT::Date, and
+vice versa.
+
+=cut
+
+sub DateTimeObj {
+    my $self = shift;
+    my %args = ( 
+        Timezone => '',
+        @_,
+    );
+
+    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
+                            $self->Localtime($args{'Timezone'});
+    $mon++;
+
+    return DateTime::->new(
+        locale     => $self->LocaleObj,
+        time_zone  => $self->Timezone($args{'Timezone'}),
+        year       => $year,
+        month      => $mon,
+        day        => $mday,
+        hour       => $hour,
+        minute     => $min,
+        second     => $sec,
+        nanosecond => 0,
+    );
+
+    return $dt;
+}
 
 RT::Base->_ImportOverlays();
 

commit b53fa7120fc9735e43a1ba7e55c810b13c551acf
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Tue Nov 24 20:28:28 2015 +0000

    Add an RT::Date->Strftime method
    
        Fixes: I#31435

diff --git a/lib/RT/Date.pm b/lib/RT/Date.pm
index e5914b7..6998650 100644
--- a/lib/RT/Date.pm
+++ b/lib/RT/Date.pm
@@ -1183,6 +1183,25 @@ sub DateTimeObj {
     return $dt;
 }
 
+=head3 Strftime FORMAT, [Timezone => 'user']
+
+Stringify the RT::Date according to the specified format. See
+L<DateTime/strftime Patterns>.
+
+=cut
+
+sub Strftime {
+    my $self = shift;
+    my $format = shift;
+    my %args = (
+        Timezone => 'user',
+        @_,
+    );
+
+    my $dt = $self->DateTimeObj(%args);
+    return $dt->strftime($format);
+}
+
 RT::Base->_ImportOverlays();
 
 1;

-----------------------------------------------------------------------


More information about the rt-commit mailing list