[Rt-commit] r17740 - in rt/3.8/trunk: etc lib/RT
elacour at bestpractical.com
elacour at bestpractical.com
Tue Jan 13 14:07:16 EST 2009
Author: elacour
Date: Tue Jan 13 14:07:15 2009
New Revision: 17740
Modified:
rt/3.8/trunk/etc/RT_Config.pm.in
rt/3.8/trunk/lib/RT/Config.pm
rt/3.8/trunk/lib/RT/Date.pm
rt/3.8/trunk/t/api/date.t
Log:
Add a new optional date output formatters using DateTime::Locale: RT::Date::LocalizedDateTime
- lib/RT/Date.pm: add method "LocalizedDateTime" which output date using
DateTime and DateTime::Locale
- lib/RT/Date.pm: add a method "Formatters" which returns the list of available
Formatters (using a new @FORMATTERS constant)
- lib/RT/Config.pm: use RT::Date::Formatters to get available formatters for
Locale preference
- t/api/date.t: add optional tests for the new LocalizedDateTime formatter
Modified: rt/3.8/trunk/etc/RT_Config.pm.in
==============================================================================
--- rt/3.8/trunk/etc/RT_Config.pm.in (original)
+++ rt/3.8/trunk/etc/RT_Config.pm.in Tue Jan 13 14:07:15 2009
@@ -1284,6 +1284,7 @@
be overridden by users in their preferences.
Some examples:
+C<Set($DateTimeFormat, 'LocalizedDateTime');>
C<Set($DateTimeFormat, { Format => 'ISO', Seconds => 0 });>
C<Set($DateTimeFormat, 'RFC2822');>
C<Set($DateTimeFormat, { Format => 'RFC2822', Seconds => 0, DayOfWeek => 0 });>
Modified: rt/3.8/trunk/lib/RT/Config.pm
==============================================================================
--- rt/3.8/trunk/lib/RT/Config.pm (original)
+++ rt/3.8/trunk/lib/RT/Config.pm Tue Jan 13 14:07:15 2009
@@ -275,7 +275,7 @@
Callback => sub { my $ret = { Values => [], ValuesLabel => {}};
my $date = new RT::Date($HTML::Mason::Commands::session{'CurrentUser'});
$date->Set;
- foreach my $value (qw(DefaultFormat RFC2822 ISO W3CDTF)) { #loc_qw
+ foreach my $value ($date->Formatters) {
push @{$ret->{Values}}, $value;
$ret->{ValuesLabel}{$value} = $date->$value();
}
Modified: rt/3.8/trunk/lib/RT/Date.pm
==============================================================================
--- rt/3.8/trunk/lib/RT/Date.pm (original)
+++ rt/3.8/trunk/lib/RT/Date.pm Tue Jan 13 14:07:15 2009
@@ -109,6 +109,18 @@
'Sat', # loc
);
+our @FORMATTERS = (
+ 'DefaultFormat', # loc
+ 'ISO', # loc
+ 'W3CDTF', # loc
+ 'RFC2822', # loc
+ 'RFC2616', # loc
+ 'iCal', # loc
+);
+if ( eval 'use DateTime qw(); 1;' && eval 'use DateTime::Locale qw(); 1;' ) {
+ push @FORMATTERS, 'LocalizedDateTime'; # loc
+}
+
=head2 new
Object constructor takes one argument C<RT::CurrentUser> object.
@@ -554,6 +566,19 @@
in RFC2822 format day of time in output is optional so it
understand boolean argument C<DayOfTime>.
+=head3 Formatters
+
+Returns an array of available formatters.
+
+=cut
+
+sub Formatters
+{
+ my $self = shift;
+
+ return @FORMATTERS;
+}
+
=head3 DefaultFormat
=cut
@@ -586,6 +611,72 @@
}
}
+=head3 LocalizedDateTime
+
+Returns date and time as string, with user localization.
+
+Supports arguments: C<DateFormat> and C<TimeFormat> which may contains date and
+time format as specified in DateTime::Locale (default to full_date_format and
+medium_time_format), C<AbbrDay> and C<AbbrMonth> which may be set to 0 if
+you want full Day/Month names instead of abbreviated ones.
+
+Require optionnal DateTime::Locale module.
+
+=cut
+
+sub LocalizedDateTime
+{
+ my $self = shift;
+ my %args = ( Date => 1,
+ Time => 1,
+ Timezone => '',
+ DateFormat => 'full_date_format',
+ TimeFormat => 'medium_time_format',
+ AbbrDay => 1,
+ AbbrMonth => 1,
+ @_,
+ );
+
+ return $self->loc("DateTime module missing") unless ( eval 'use DateTime qw(); 1;' );
+ return $self->loc("DateTime::Locale module missing") unless ( eval 'use DateTime::Locale qw(); 1;' );
+ my $date_format = $args{'DateFormat'};
+ my $time_format = $args{'TimeFormat'};
+
+ my $lang = $self->CurrentUser->UserObj->Lang || 'en';
+
+ my $formatter = DateTime::Locale->load($lang);
+ $date_format = $formatter->$date_format;
+ $time_format = $formatter->$time_format;
+ $date_format =~ s/\%A/\%a/g if ( $args{'AbbrDay'} );
+ $date_format =~ s/\%B/\%b/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 = new DateTime::( locale => $lang,
+ time_zone => $tz,
+ year => $year,
+ month => $mon,
+ day => $mday,
+ hour => $hour,
+ minute => $min,
+ second => $sec,
+ nanosecond => 0,
+ );
+
+ if ( $args{'Date'} && !$args{'Time'} ) {
+ return $dt->strftime($date_format);
+ } elsif ( !$args{'Date'} && $args{'Time'} ) {
+ return $dt->strftime($time_format);
+ } else {
+ return $dt->strftime($date_format) . " " . $dt->strftime($time_format);
+ }
+}
+
=head3 ISO
Returns the object's date in ISO format C<YYYY-MM-DD mm:hh:ss>.
Modified: rt/3.8/trunk/t/api/date.t
==============================================================================
--- rt/3.8/trunk/t/api/date.t (original)
+++ rt/3.8/trunk/t/api/date.t Tue Jan 13 14:07:15 2009
@@ -2,7 +2,17 @@
use Test::MockTime qw(set_fixed_time restore_time);
-use Test::More tests => 167;
+use Test::More;
+my $tests = 167;
+my $localized_datetime_tests = eval { require DateTime; 1; } && eval { require DateTime::Locale; 1; };
+
+if ( $localized_datetime_tests ) {
+ # Include RT::Date::LocalizedDateTime tests
+ $tests += 7;
+}
+
+plan tests => $tests;
+
use warnings; use strict;
use RT::Test;
@@ -100,6 +110,9 @@
is($date->Get(Format =>'RFC2822'),
'Thu, 1 Jan 1970 00:00:00 +0000',
"RFC2822 format with defaults");
+ is($date->Get(Format =>'LocalizedDateTime'),
+ 'Thu, Jan 1, 1970 12:00:00 AM',
+ "LocalizedDateTime format with defaults") if ( $localized_datetime_tests );
is($date->ISO(Time => 0),
'1970-01-01',
@@ -110,6 +123,9 @@
is($date->RFC2822(Time => 0),
'Thu, 1 Jan 1970',
"RFC2822 format without time part");
+ is($date->LocalizedDateTime(Time => 0),
+ 'Thu, Jan 1, 1970',
+ "LocalizedDateTime format without time part") if ( $localized_datetime_tests );
is($date->ISO(Date => 0),
'00:00:00',
@@ -120,6 +136,9 @@
is($date->RFC2822(Date => 0),
'00:00:00 +0000',
"RFC2822 format without date part");
+ is($date->LocalizedDateTime(Date => 0),
+ '12:00:00 AM',
+ "LocalizedDateTime format without date part") if ( $localized_datetime_tests );
is($date->ISO(Date => 0, Seconds => 0),
'00:00',
@@ -138,6 +157,19 @@
'00:00:00 +0000',
"RFC2822 format without 'day of week' and date parts(corner case test)");
+ is($date->LocalizedDateTime(AbbrDay => 0),
+ 'Thursday, Jan 1, 1970 12:00:00 AM',
+ "LocalizedDateTime format without abbreviation of day") if ( $localized_datetime_tests );
+ is($date->LocalizedDateTime(AbbrMonth => 0),
+ 'Thu, January 1, 1970 12:00:00 AM',
+ "LocalizedDateTime format without abbreviation of month") if ( $localized_datetime_tests );
+ is($date->LocalizedDateTime(DateFormat => 'short_date_format'),
+ '1/1/70 12:00:00 AM',
+ "LocalizedDateTime format with non default DateFormat") if ( $localized_datetime_tests );
+ is($date->LocalizedDateTime(TimeFormat => 'short_time_format'),
+ 'Thu, Jan 1, 1970 12:00 AM',
+ "LocalizedDateTime format with non default TimeFormat") if ( $localized_datetime_tests );
+
is($date->Date,
'1970-01-01',
"the default format for the 'Date' method is ISO");
More information about the Rt-commit
mailing list