Revision 646cb2aa
Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt
SL/Helper/DateTime.pm | ||
---|---|---|
28 | 28 |
} |
29 | 29 |
|
30 | 30 |
sub from_kivitendo { |
31 |
return $::locale->parse_date_to_object(\%::myconfig, $_[1]);
|
|
31 |
return $::locale->parse_date_to_object($_[1]); |
|
32 | 32 |
} |
33 | 33 |
|
34 | 34 |
sub from_lxoffice { |
SL/Locale.pm | ||
---|---|---|
374 | 374 |
} |
375 | 375 |
|
376 | 376 |
sub parse_date_to_object { |
377 |
my $self = shift; |
|
378 |
my ($yy, $mm, $dd) = $self->parse_date(@_); |
|
377 |
my ($self, $string, %params) = @_; |
|
379 | 378 |
|
380 |
return $yy && $mm && $dd ? DateTime->new(year => $yy, month => $mm, day => $dd) : undef; |
|
379 |
$params{dateformat} ||= $::myconfig{dateformat} || 'yy-mm-dd'; |
|
380 |
$params{numberformat} ||= $::myconfig{numberformat} || '1,000.00'; |
|
381 |
my $num_separator = $params{numberformat} =~ m{,\d+$} ? ',' : '.'; |
|
382 |
|
|
383 |
my ($date_str, $time_str) = split m{\s+}, $string, 2; |
|
384 |
my ($yy, $mm, $dd) = $self->parse_date(\%params, $date_str); |
|
385 |
|
|
386 |
my $millisecond = 0; |
|
387 |
my ($hour, $minute, $second) = split m/:/, $time_str; |
|
388 |
($second, $millisecond) = split quotemeta($num_separator), $second, 2; |
|
389 |
$millisecond = substr $millisecond, 0, 3; |
|
390 |
$millisecond .= '0' x (3 - length $millisecond); |
|
391 |
|
|
392 |
return undef unless $yy && $mm && $dd; |
|
393 |
return DateTime->new(year => $yy, month => $mm, day => $dd, hour => $hour * 1, minute => $minute * 1, second => $second * 1, nanosecond => $millisecond * 1000000); |
|
381 | 394 |
} |
382 | 395 |
|
383 | 396 |
sub format_date_object_to_time { |
... | ... | |
644 | 657 |
|
645 | 658 |
TODO: Describe parse_date |
646 | 659 |
|
647 |
=item C<parse_date_to_object> |
|
660 |
=item C<parse_date_to_object $string, %params> |
|
661 |
|
|
662 |
Parses a date and optional timestamp in C<$string> and returns an |
|
663 |
instance of L<DateTime>. The date and number formats used are the ones |
|
664 |
the user has currently selected. They can be overriden by passing them |
|
665 |
in as parameters to this function, though. |
|
648 | 666 |
|
649 |
TODO: Describe parse_date_to_object
|
|
667 |
The time stamps can have up to millisecond precision.
|
|
650 | 668 |
|
651 | 669 |
=item C<quote_special_chars> |
652 | 670 |
|
SL/RP.pm | ||
---|---|---|
58 | 58 |
my $asofdate = shift; |
59 | 59 |
return unless $asofdate; |
60 | 60 |
|
61 |
$asofdate = $::locale->parse_date_to_object(\%::myconfig, $asofdate);
|
|
61 |
$asofdate = $::locale->parse_date_to_object($asofdate); |
|
62 | 62 |
|
63 | 63 |
my $form = $main::form; |
64 | 64 |
my $dbh = $::form->get_standard_dbh; |
... | ... | |
85 | 85 |
# default. |
86 | 86 |
my ($closedto) = selectfirst_array_query($form, $dbh, 'SELECT closedto FROM defaults'); |
87 | 87 |
if ($closedto) { |
88 |
$closedto = $::locale->parse_date_to_object(\%::myconfig, $closedto);
|
|
88 |
$closedto = $::locale->parse_date_to_object($closedto); |
|
89 | 89 |
$closedto->subtract(years => 1) while ($asofdate - $closedto)->is_negative; |
90 | 90 |
$closedto->add(days => 1); |
91 | 91 |
}; |
... | ... | |
93 | 93 |
my ($query, $startdate, $last_ob, $mindate); |
94 | 94 |
$query = qq|select max(transdate) from acc_trans where ob_transaction is true and transdate <= ?|; |
95 | 95 |
($last_ob) = selectrow_query($::form, $dbh, $query, $::locale->format_date(\%::myconfig, $asofdate)); |
96 |
$last_ob = $::locale->parse_date_to_object(\%::myconfig, $last_ob) if $last_ob;
|
|
96 |
$last_ob = $::locale->parse_date_to_object($last_ob) if $last_ob; |
|
97 | 97 |
|
98 | 98 |
$query = qq|select min(transdate) from acc_trans|; |
99 | 99 |
($mindate) = selectrow_query($::form, $dbh, $query); |
100 |
$mindate = $::locale->parse_date_to_object(\%::myconfig, $mindate);
|
|
100 |
$mindate = $::locale->parse_date_to_object($mindate); |
|
101 | 101 |
|
102 | 102 |
# the default method is to use all transactions ($mindate) |
103 | 103 |
|
bin/mozilla/rp.pl | ||
---|---|---|
1789 | 1789 |
# if (defined ($form->{fromdate|todate}=='..')) |
1790 | 1790 |
# immer wahr |
1791 | 1791 |
if ($form->{fromdate}){ |
1792 |
my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate}); |
|
1793 |
my $datetime = $locale->parse_date_to_object(\%myconfig, $form->{fromdate}); |
|
1792 |
my $datetime = $locale->parse_date_to_object($form->{fromdate}); |
|
1794 | 1793 |
$datetime->set( month => 1, |
1795 | 1794 |
day => 1); |
1796 | 1795 |
$form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime); |
t/locale/parse_date_to_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 |
sub is_dt { |
|
11 |
my ($string, $expected, $msg, %args) = @_; |
|
12 |
|
|
13 |
is($::locale->format_date_object($::locale->parse_date_to_object($string, %args), numberformat => '1000.00', dateformat => 'yy-mm-dd', precision => 'millisecond'), $expected, $msg); |
|
14 |
} |
|
15 |
|
|
16 |
is($::locale->parse_date_to_object('in-valid!'), undef, 'defaults, invalid'); |
|
17 |
|
|
18 |
delete $::myconfig{numberformat}; |
|
19 |
delete $::myconfig{dateformat}; |
|
20 |
|
|
21 |
is_dt('2014-05-31', '2014-05-31 00:00:00.000', 'defaults, no precision'); |
|
22 |
is_dt('2014-05-31 2', '2014-05-31 02:00:00.000', 'defaults, precision hour'); |
|
23 |
is_dt('2014-05-31 2:04', '2014-05-31 02:04:00.000', 'defaults, precision minute'); |
|
24 |
is_dt('2014-05-31 2:04:59', '2014-05-31 02:04:59.000', 'defaults, precision second'); |
|
25 |
is_dt('2014-05-31 02:4:59.098', '2014-05-31 02:04:59.098', 'defaults, precision millisecond'); |
|
26 |
is_dt('2014-05-31 02:4:59.09', '2014-05-31 02:04:59.090', 'defaults, precision centisecond'); |
|
27 |
|
|
28 |
$::myconfig{numberformat} = '1.000,00'; |
|
29 |
$::myconfig{dateformat} = 'dd.mm.yy'; |
|
30 |
|
|
31 |
is_dt('31.05.2014', '2014-05-31 00:00:00.000', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, no precision'); |
|
32 |
is_dt('31.05.2014 2', '2014-05-31 02:00:00.000', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision hour'); |
|
33 |
is_dt('31.05.2014 2:04', '2014-05-31 02:04:00.000', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision minute'); |
|
34 |
is_dt('31.05.2014 2:04:59', '2014-05-31 02:04:59.000', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision second'); |
|
35 |
is_dt('31.05.2014 02:4:59,098', '2014-05-31 02:04:59.098', 'myconfig numberformat 1.000,00 dateformat dd.mm.yy, precision millisecond'); |
|
36 |
|
|
37 |
is_dt('05/31/2014', '2014-05-31 00:00:00.000', 'myconfig numberformat 1.000,00 explicit dateformat mm/dd/yy, no precision', dateformat => 'mm/dd/yy'); |
|
38 |
is_dt('05/31/2014 2', '2014-05-31 02:00:00.000', 'myconfig numberformat 1.000,00 explicit dateformat mm/dd/yy, precision hour', dateformat => 'mm/dd/yy'); |
|
39 |
is_dt('05/31/2014 2:04', '2014-05-31 02:04:00.000', 'myconfig numberformat 1.000,00 explicit dateformat mm/dd/yy, precision minute', dateformat => 'mm/dd/yy'); |
|
40 |
is_dt('05/31/2014 2:04:59', '2014-05-31 02:04:59.000', 'myconfig numberformat 1.000,00 explicit dateformat mm/dd/yy, precision second', dateformat => 'mm/dd/yy'); |
|
41 |
is_dt('05/31/2014 02:4:59,098', '2014-05-31 02:04:59.098', 'myconfig numberformat 1.000,00 explicit dateformat mm/dd/yy, precision millisecond', dateformat => 'mm/dd/yy'); |
|
42 |
|
|
43 |
is_dt('05/31/2014', '2014-05-31 00:00:00.000', 'explicit numberformat 1000.00 explicit dateformat mm/dd/yy, no precision', dateformat => 'mm/dd/yy', numberformat => '1000.00'); |
|
44 |
is_dt('05/31/2014 2', '2014-05-31 02:00:00.000', 'explicit numberformat 1000.00 explicit dateformat mm/dd/yy, precision hour', dateformat => 'mm/dd/yy', numberformat => '1000.00'); |
|
45 |
is_dt('05/31/2014 2:04', '2014-05-31 02:04:00.000', 'explicit numberformat 1000.00 explicit dateformat mm/dd/yy, precision minute', dateformat => 'mm/dd/yy', numberformat => '1000.00'); |
|
46 |
is_dt('05/31/2014 2:04:59', '2014-05-31 02:04:59.000', 'explicit numberformat 1000.00 explicit dateformat mm/dd/yy, precision second', dateformat => 'mm/dd/yy', numberformat => '1000.00'); |
|
47 |
is_dt('05/31/2014 02:4:59.098', '2014-05-31 02:04:59.098', 'explicit numberformat 1000.00 explicit dateformat mm/dd/yy, precision millisecond', dateformat => 'mm/dd/yy', numberformat => '1000.00'); |
|
48 |
|
|
49 |
done_testing; |
|
50 |
|
|
51 |
1; |
Auch abrufbar als: Unified diff
Locale::parse_date_to_object: Unterstützung für volle Timestamps & explizite Formate
…und dafür Tests.