[Rt-devel] $DateDayBeforeMonth in DefaultFormat and enhance of date/time display

Jesse Vincent jesse at bestpractical.com
Mon Jan 12 08:34:36 EST 2009


That seems fairly reasonable. If it had tests, it would be incredibly
reasonable ;)


On Mon, Jan 12, 2009 at 10:28:40AM +0100, Emmanuel Lacour wrote:
> On Fri, Jan 09, 2009 at 12:23:15PM -0500, Jesse Vincent wrote:
> > 
> > Hm.  This makes me a little twitchy. I'd really rather keep it
> > parsing-only. I like your suggestion below :)
> > 
> 
> I agree, I just modified a bit the description in RT_Config.pm.in to
> say that it's only for parsing.
> 
> > 
> > > 
> > > But maybe we would better make use of DateTime::Locale for displaying
> > > date/times to be sure to cover most languages.
> > 
> > I like this plan better :)
> > 
> > > Here is an example of DateTime::Locale usage (as I understand this
> > > module):
> > >
> > 
> > Cool. Can we make it depend on DateTime::Locale only if it's installed?
> > 
> 
> See attached patch.
> 
> > Also, I'd probably only offer the datetime formats, since only-date and
> > only-time formats don't make a whole lot of sense in RT ;)
> >  
> 
> Thought, I prefer to stay consistent with current output formaters which
> supports this ;)
> 

> diff --git a/etc/RT_Config.pm.in b/etc/RT_Config.pm.in
> index 0ac4a30..9bc0c2e 100755
> --- a/etc/RT_Config.pm.in
> +++ b/etc/RT_Config.pm.in
> @@ -1284,6 +1284,7 @@ section in perldoc F<lib/RT/Date.pm> for more options.  This option can
>  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 });>
> diff --git a/lib/RT/Config.pm b/lib/RT/Config.pm
> index 0027656..710a2c9 100644
> --- a/lib/RT/Config.pm
> +++ b/lib/RT/Config.pm
> @@ -275,7 +275,11 @@ our %META = (
>              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
> +                              my @formats = qw(DefaultFormat RFC2822 ISO W3CDTF); #loc_qw
> +                              if ( eval 'use DateTime qw(); 1;' && eval 'use DateTime::Locale qw(); 1;' ) {
> +                                  push @formats, 'LocalizedDateTime'; #loc
> +                              }
> +                              foreach my $value (@formats) {
>                                   push @{$ret->{Values}}, $value;
>                                   $ret->{ValuesLabel}{$value} = $date->$value();
>                                }
> diff --git a/lib/RT/Date.pm b/lib/RT/Date.pm
> index c1354b7..b03f8a1 100755
> --- a/lib/RT/Date.pm
> +++ b/lib/RT/Date.pm
> @@ -586,6 +586,72 @@ sub DefaultFormat
>      }
>  }
>  
> +=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>.

> _______________________________________________
> List info: http://lists.bestpractical.com/cgi-bin/mailman/listinfo/rt-devel


-- 


More information about the Rt-devel mailing list