Lines 20-26
use strict;
Link Here
|
20 |
use warnings; |
20 |
use warnings; |
21 |
use 5.010; |
21 |
use 5.010; |
22 |
use DateTime; |
22 |
use DateTime; |
23 |
use DateTime::Format::DateParse; |
|
|
24 |
use C4::Context; |
23 |
use C4::Context; |
25 |
|
24 |
|
26 |
use base 'Exporter'; |
25 |
use base 'Exporter'; |
Lines 54-99
to the system preferences. If the date string is empty DateTime->now is returned
Link Here
|
54 |
sub dt_from_string { |
53 |
sub dt_from_string { |
55 |
my ( $date_string, $date_format, $tz ) = @_; |
54 |
my ( $date_string, $date_format, $tz ) = @_; |
56 |
|
55 |
|
57 |
# FIXME: see bug 13242 => no TZ for dates 'infinite' |
56 |
return if $date_string and $date_string =~ m|^0000-0|; |
58 |
return DateTime::Format::DateParse->parse_datetime($date_string) |
57 |
|
59 |
if $date_string and $date_string =~ /^9999-/; |
58 |
$tz = C4::Context->tz unless $tz;; |
|
|
59 |
|
60 |
return DateTime->now( time_zone => $tz ) unless $date_string; |
61 |
|
62 |
$date_format = C4::Context->preference('dateformat') unless $date_format; |
60 |
|
63 |
|
61 |
if ( !$tz ) { |
|
|
62 |
$tz = C4::Context->tz; |
63 |
} |
64 |
if ( !$date_format ) { |
65 |
$date_format = C4::Context->preference('dateformat'); |
66 |
} |
67 |
if ($date_string) { |
64 |
if ($date_string) { |
68 |
if ( ref($date_string) eq 'DateTime' ) { # already a dt return it |
65 |
if ( ref($date_string) eq 'DateTime' ) { # already a dt return it |
69 |
return $date_string; |
66 |
return $date_string; |
70 |
} |
67 |
} |
71 |
|
68 |
|
|
|
69 |
my $regex; |
72 |
if ( $date_format eq 'metric' ) { |
70 |
if ( $date_format eq 'metric' ) { |
73 |
$date_string =~ s#-#/#g; |
71 |
# metric format is "dd/mm/yyyy[ hh:mm:ss]" |
74 |
$date_string =~ s/^00/01/; # system allows the 0th of the month |
72 |
$regex = qr| |
75 |
$date_string =~ s#^(\d{1,2})/(\d{1,2})#$2/$1#; |
73 |
(?<day>\d{2}) |
76 |
} else { |
74 |
/ |
77 |
if ( $date_format eq 'iso' ) { |
75 |
(?<month>\d{2}) |
78 |
$date_string =~ s/-00/-01/; |
76 |
/ |
79 |
if ( $date_string =~ m/^0000-0/ ) { |
77 |
(?<year>\d{4}) |
80 |
return; # invalid date in db |
78 |
|xms; |
81 |
} |
|
|
82 |
} elsif ( $date_format eq 'us' ) { |
83 |
$date_string =~ s#-#/#g; |
84 |
$date_string =~ s[/00/][/01/]; |
85 |
} elsif ( $date_format eq 'sql' ) { |
86 |
$date_string =~ |
87 |
s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/; |
88 |
return if ($date_string =~ /^0000-00-00/); |
89 |
$date_string =~ s/00T/01T/; |
90 |
} |
91 |
} |
79 |
} |
92 |
return DateTime::Format::DateParse->parse_datetime( $date_string, |
80 |
elsif ( $date_format eq 'us' ) { |
93 |
$tz->name() ); |
81 |
# us format is "mm/dd/yyyy[ hh:mm:ss]" |
94 |
} |
82 |
$regex = qr| |
95 |
return DateTime->now( time_zone => $tz ); |
83 |
(?<month>\d{2}) |
|
|
84 |
/ |
85 |
(?<day>\d{2}) |
86 |
/ |
87 |
(?<year>\d{4}) |
88 |
|xms; |
89 |
} |
90 |
elsif ( $date_format eq 'iso' or $date_format eq 'sql' ) { |
91 |
# iso format is yyyy-dd-mm[ hh:mm:ss]" |
92 |
$regex = qr| |
93 |
(?<year>\d{4}) |
94 |
- |
95 |
(?<month>\d{2}) |
96 |
- |
97 |
(?<day>\d{2}) |
98 |
|xms; |
99 |
} |
100 |
else { |
101 |
die "Invalid dateformat parameter ($date_format)"; |
102 |
} |
103 |
|
104 |
# Add the faculative time part [hh:mm[:ss]] |
105 |
$regex .= qr| |
106 |
( |
107 |
\s* |
108 |
(?<hour>\d{2}) |
109 |
: |
110 |
(?<minute>\d{2}) |
111 |
( |
112 |
: |
113 |
(?<second>\d{2}) |
114 |
)? |
115 |
)? |
116 |
|xms; |
117 |
|
118 |
my %dt_params; |
119 |
if ( $date_string =~ $regex ) { |
120 |
%dt_params = ( |
121 |
year => $+{year}, |
122 |
month => $+{month}, |
123 |
day => $+{day}, |
124 |
hour => $+{hour}, |
125 |
minute => $+{minute}, |
126 |
second => $+{second}, |
127 |
); |
128 |
} |
129 |
else { |
130 |
die "The given date ($date_string) does not match the date format ($date_format)"; |
131 |
} |
132 |
|
133 |
# system allows the 0th of the month |
134 |
$dt_params{day} = '01' if $dt_params{day} eq '00'; |
96 |
|
135 |
|
|
|
136 |
# Set default hh:mm:ss to 00:00:00 |
137 |
$dt_params{hour} = 00 unless defined $dt_params{hour}; |
138 |
$dt_params{minute} = 00 unless defined $dt_params{minute}; |
139 |
$dt_params{second} = 00 unless defined $dt_params{second}; |
140 |
|
141 |
return DateTime->new( |
142 |
%dt_params, |
143 |
# No TZ for dates 'infinite' => see bug 13242 |
144 |
( $dt_params{year} < 9999 ? ( time_zone => $tz->name ) : () ), |
145 |
); |
146 |
} |
97 |
} |
147 |
} |
98 |
|
148 |
|
99 |
=head2 output_pref |
149 |
=head2 output_pref |
100 |
- |
|
|