Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision d16e003e

Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt

  • ID d16e003e400bfad9e45acbf1fd0cee70dd68b848
  • Vorgänger df2fba09
  • Nachfolger 646cb2aa

Locale::format_date_object: explizites Setzen der Formate; Millisekunden als neues Format

…und dazu Tests.

Unterschiede anzeigen:

SL/Locale.pm
394 394
sub format_date_object {
395 395
  my ($self, $datetime, %params)    = @_;
396 396

  
397
  my $format             =  $::myconfig{dateformat} || 'yyyy-mm-dd';
397
  my $format             =   $params{dateformat}   || $::myconfig{dateformat}   || 'yyyy-mm-dd';
398
  my $num_separator      =  ($params{numberformat} || $::myconfig{numberformat} || '1,000.00') =~ m{,\d+$} ? ',' : '.';
398 399
  $format                =~ s/yy(?:yy)?/\%Y/;
399 400
  $format                =~ s/mm/\%m/;
400 401
  $format                =~ s/dd/\%d/;
......
402 403
  my $precision          =  $params{precision} || 'day';
403 404
  $precision             =~ s/s$//;
404 405
  my %precision_spec_map = (
405
    second => '%H:%M:%S',
406
    minute => '%H:%M',
407
    hour   => '%H',
406
    millisecond => '%H:%M:%S' . $num_separator . '%3N',
407
    second      => '%H:%M:%S',
408
    minute      => '%H:%M',
409
    hour        => '%H',
408 410
  );
409 411

  
410 412
  $format .= ' ' . $precision_spec_map{$precision} if $precision_spec_map{$precision};
......
609 611

  
610 612
Add hour:minute:second to the date.
611 613

  
614
=item * C<millisecond>
615

  
616
Add hour:minute:second.millisecond to the date. The decimal separator
617
is derived from the number format.
618

  
619
=item * C<numberformat>
620

  
621
The number format to use, e.g. C<1,000.00>. If unset the user's
622
current number format is used.
623

  
624
=item * C<dateformat>
625

  
626
The date format to use, e.g. C<mm/dd/yy>. If unset the user's current
627
date format is used.
628

  
612 629
=back
613 630

  
614 631
=item C<get_local_time_zone>
t/locale/format_date_object.t
1
use strict;
2

  
3
use Test::More;
4

  
5
use lib 't';
6
use Support::TestSetup;
7

  
8
Support::TestSetup::login();
9

  
10
my $dt = DateTime->new(year => 2014, month => 5, day => 31, hour => 23, minute => 9, second => 8, nanosecond => 12000000);
11

  
12
delete $::myconfig{numberformat};
13
delete $::myconfig{dateformat};
14

  
15
is($::locale->format_date_object($dt),                             '2014-05-31',              'defaults, no precision');
16
is($::locale->format_date_object($dt, precision => 'day'),         '2014-05-31',              'defaults, precision day');
17
is($::locale->format_date_object($dt, precision => 'hour'),        '2014-05-31 23',           'defaults, precision hour');
18
is($::locale->format_date_object($dt, precision => 'minute'),      '2014-05-31 23:09',        'defaults, precision minute');
19
is($::locale->format_date_object($dt, precision => 'second'),      '2014-05-31 23:09:08',     'defaults, precision second');
20
is($::locale->format_date_object($dt, precision => 'millisecond'), '2014-05-31 23:09:08.012', 'defaults, precision millisecond');
21

  
22
$::myconfig{numberformat} = '1.000,00';
23
$::myconfig{dateformat}   = 'dd.mm.yy';
24

  
25
is($::locale->format_date_object($dt),                             '31.05.2014',              'myconfig numberformat 1.000,00 dateformat dd.mm.yy, no precision');
26
is($::locale->format_date_object($dt, precision => 'day'),         '31.05.2014',              'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision day');
27
is($::locale->format_date_object($dt, precision => 'hour'),        '31.05.2014 23',           'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision hour');
28
is($::locale->format_date_object($dt, precision => 'minute'),      '31.05.2014 23:09',        'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision minute');
29
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');
30
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');
31

  
32
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');
33
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');
34
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');
35
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');
36
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');
37
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');
38

  
39
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');
40
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');
41
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');
42
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');
43
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');
44
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');
45

  
46
done_testing;
47

  
48
1;

Auch abrufbar als: Unified diff