From d16e003e400bfad9e45acbf1fd0cee70dd68b848 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 3 Jun 2014 13:53:22 +0200 Subject: [PATCH] Locale::format_date_object: explizites Setzen der Formate; Millisekunden als neues Format MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit …und dazu Tests. --- SL/Locale.pm | 25 +++++++++++++++--- t/locale/format_date_object.t | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 t/locale/format_date_object.t diff --git a/SL/Locale.pm b/SL/Locale.pm index 4fc7a5ee0..a20620cdb 100644 --- a/SL/Locale.pm +++ b/SL/Locale.pm @@ -394,7 +394,8 @@ sub format_date_object_to_time { sub format_date_object { my ($self, $datetime, %params) = @_; - my $format = $::myconfig{dateformat} || 'yyyy-mm-dd'; + my $format = $params{dateformat} || $::myconfig{dateformat} || 'yyyy-mm-dd'; + my $num_separator = ($params{numberformat} || $::myconfig{numberformat} || '1,000.00') =~ m{,\d+$} ? ',' : '.'; $format =~ s/yy(?:yy)?/\%Y/; $format =~ s/mm/\%m/; $format =~ s/dd/\%d/; @@ -402,9 +403,10 @@ sub format_date_object { my $precision = $params{precision} || 'day'; $precision =~ s/s$//; my %precision_spec_map = ( - second => '%H:%M:%S', - minute => '%H:%M', - hour => '%H', + millisecond => '%H:%M:%S' . $num_separator . '%3N', + second => '%H:%M:%S', + minute => '%H:%M', + hour => '%H', ); $format .= ' ' . $precision_spec_map{$precision} if $precision_spec_map{$precision}; @@ -609,6 +611,21 @@ Add hour:minute to the date. Add hour:minute:second to the date. +=item * C + +Add hour:minute:second.millisecond to the date. The decimal separator +is derived from the number format. + +=item * C + +The number format to use, e.g. C<1,000.00>. If unset the user's +current number format is used. + +=item * C + +The date format to use, e.g. C. If unset the user's current +date format is used. + =back =item C diff --git a/t/locale/format_date_object.t b/t/locale/format_date_object.t new file mode 100644 index 000000000..2b227d7c0 --- /dev/null +++ b/t/locale/format_date_object.t @@ -0,0 +1,48 @@ +use strict; + +use Test::More; + +use lib 't'; +use Support::TestSetup; + +Support::TestSetup::login(); + +my $dt = DateTime->new(year => 2014, month => 5, day => 31, hour => 23, minute => 9, second => 8, nanosecond => 12000000); + +delete $::myconfig{numberformat}; +delete $::myconfig{dateformat}; + +is($::locale->format_date_object($dt), '2014-05-31', 'defaults, no precision'); +is($::locale->format_date_object($dt, precision => 'day'), '2014-05-31', 'defaults, precision day'); +is($::locale->format_date_object($dt, precision => 'hour'), '2014-05-31 23', 'defaults, precision hour'); +is($::locale->format_date_object($dt, precision => 'minute'), '2014-05-31 23:09', 'defaults, precision minute'); +is($::locale->format_date_object($dt, precision => 'second'), '2014-05-31 23:09:08', 'defaults, precision second'); +is($::locale->format_date_object($dt, precision => 'millisecond'), '2014-05-31 23:09:08.012', 'defaults, precision millisecond'); + +$::myconfig{numberformat} = '1.000,00'; +$::myconfig{dateformat} = 'dd.mm.yy'; + +is($::locale->format_date_object($dt), '31.05.2014', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, no precision'); +is($::locale->format_date_object($dt, precision => 'day'), '31.05.2014', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision day'); +is($::locale->format_date_object($dt, precision => 'hour'), '31.05.2014 23', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision hour'); +is($::locale->format_date_object($dt, precision => 'minute'), '31.05.2014 23:09', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision minute'); +is($::locale->format_date_object($dt, precision => 'second'), '31.05.2014 23:09:08', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision second'); +is($::locale->format_date_object($dt, precision => 'millisecond'), '31.05.2014 23:09:08,012', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision millisecond'); + +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy'), '05/31/2014', 'myconfig numberformat 1.000,00, explicit dateformat mm/dd/yy, no precision'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', precision => 'day'), '05/31/2014', 'myconfig numberformat 1.000,00, explicit dateformat mm/dd/yy, precision day'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', precision => 'hour'), '05/31/2014 23', 'myconfig numberformat 1.000,00, explicit dateformat mm/dd/yy, precision hour'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', precision => 'minute'), '05/31/2014 23:09', 'myconfig numberformat 1.000,00, explicit dateformat mm/dd/yy, precision minute'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', precision => 'second'), '05/31/2014 23:09:08', 'myconfig numberformat 1.000,00, explicit dateformat mm/dd/yy, precision second'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', precision => 'millisecond'), '05/31/2014 23:09:08,012', 'myconfig numberformat 1.000,00, explicit dateformat mm/dd/yy, precision millisecond'); + +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', numberformat => '1000.00'), '05/31/2014', 'explicit numberformat 1000.00 dateformat mm/dd/yy, no precision'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', numberformat => '1000.00', precision => 'day'), '05/31/2014', 'explicit numberformat 1000.00 dateformat mm/dd/yy, precision day'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', numberformat => '1000.00', precision => 'hour'), '05/31/2014 23', 'explicit numberformat 1000.00 dateformat mm/dd/yy, precision hour'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', numberformat => '1000.00', precision => 'minute'), '05/31/2014 23:09', 'explicit numberformat 1000.00 dateformat mm/dd/yy, precision minute'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', numberformat => '1000.00', precision => 'second'), '05/31/2014 23:09:08', 'explicit numberformat 1000.00 dateformat mm/dd/yy, precision second'); +is($::locale->format_date_object($dt, dateformat => 'mm/dd/yy', numberformat => '1000.00', precision => 'millisecond'), '05/31/2014 23:09:08.012', 'explicit numberformat 1000.00 dateformat mm/dd/yy, precision millisecond'); + +done_testing; + +1; -- 2.20.1