|
Lines 1-4
Link Here
|
| 1 |
package C4::Dates; |
1 |
package C4::Dates; |
|
|
2 |
|
| 2 |
# This file is part of Koha. |
3 |
# This file is part of Koha. |
| 3 |
# |
4 |
# |
| 4 |
# Koha is free software; you can redistribute it and/or modify it under the |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
|
Lines 26-179
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Link Here
|
| 26 |
use vars qw($debug $cgi_debug); |
27 |
use vars qw($debug $cgi_debug); |
| 27 |
|
28 |
|
| 28 |
BEGIN { |
29 |
BEGIN { |
| 29 |
$VERSION = 0.04; |
30 |
$VERSION = 0.04; |
| 30 |
@ISA = qw(Exporter); |
31 |
@ISA = qw(Exporter); |
| 31 |
@EXPORT_OK = qw(format_date_in_iso format_date); |
32 |
@EXPORT_OK = qw(format_date_in_iso format_date); |
| 32 |
} |
33 |
} |
| 33 |
|
34 |
|
| 34 |
use vars qw($prefformat); |
35 |
use vars qw($prefformat); |
|
|
36 |
|
| 35 |
sub _prefformat { |
37 |
sub _prefformat { |
| 36 |
unless (defined $prefformat) { |
38 |
unless ( defined $prefformat ) { |
| 37 |
$prefformat = C4::Context->preference('dateformat'); |
39 |
$prefformat = C4::Context->preference('dateformat'); |
| 38 |
} |
40 |
} |
| 39 |
return $prefformat; |
41 |
return $prefformat; |
| 40 |
} |
42 |
} |
| 41 |
|
43 |
|
| 42 |
our %format_map = ( |
44 |
our %format_map = ( |
| 43 |
iso => 'yyyy-mm-dd', # plus " HH:MM:SS" |
45 |
iso => 'yyyy-mm-dd', # plus " HH:MM:SS" |
| 44 |
metric => 'dd/mm/yyyy', # plus " HH:MM:SS" |
46 |
metric => 'dd/mm/yyyy', # plus " HH:MM:SS" |
| 45 |
us => 'mm/dd/yyyy', # plus " HH:MM:SS" |
47 |
us => 'mm/dd/yyyy', # plus " HH:MM:SS" |
| 46 |
sql => 'yyyymmdd HHMMSS', |
48 |
sql => 'yyyymmdd HHMMSS', |
|
|
49 |
rfc822 => 'a, dd b y HH:MM:SS z ', |
| 47 |
); |
50 |
); |
| 48 |
our %posix_map = ( |
51 |
our %posix_map = ( |
| 49 |
iso => '%Y-%m-%d', # or %F, "Full Date" |
52 |
iso => '%Y-%m-%d', # or %F, "Full Date" |
| 50 |
metric => '%d/%m/%Y', |
53 |
metric => '%d/%m/%Y', |
| 51 |
us => '%m/%d/%Y', |
54 |
us => '%m/%d/%Y', |
| 52 |
sql => '%Y%m%d %H%M%S', |
55 |
sql => '%Y%m%d %H%M%S', |
|
|
56 |
rfc822 => '%a, %d %b %Y %H:%M:%S %z', |
| 53 |
); |
57 |
); |
| 54 |
|
58 |
|
| 55 |
our %dmy_subs = ( # strings to eval (after using regular expression returned by regexp below) |
59 |
our %dmy_subs = ( # strings to eval (after using regular expression returned by regexp below) |
| 56 |
# make arrays for POSIX::strftime() |
60 |
# make arrays for POSIX::strftime() |
| 57 |
iso => '[(($6||0),($5||0),($4||0),$3, $2 - 1, $1 - 1900)]', |
61 |
iso => '[(($6||0),($5||0),($4||0),$3, $2 - 1, $1 - 1900)]', |
| 58 |
metric => '[(($6||0),($5||0),($4||0),$1, $2 - 1, $3 - 1900)]', |
62 |
metric => '[(($6||0),($5||0),($4||0),$1, $2 - 1, $3 - 1900)]', |
| 59 |
us => '[(($6||0),($5||0),($4||0),$2, $1 - 1, $3 - 1900)]', |
63 |
us => '[(($6||0),($5||0),($4||0),$2, $1 - 1, $3 - 1900)]', |
| 60 |
sql => '[(($6||0),($5||0),($4||0),$3, $2 - 1, $1 - 1900)]', |
64 |
sql => '[(($6||0),($5||0),($4||0),$3, $2 - 1, $1 - 1900)]', |
|
|
65 |
rfc822 => '[($7, $6, $5, $2, $3, $4 - 1900, $8)]', |
| 61 |
); |
66 |
); |
| 62 |
|
67 |
|
|
|
68 |
our @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); |
| 69 |
|
| 70 |
our @days = qw(Sun Mon Tue Wed Thu Fri Sat); |
| 71 |
|
| 63 |
sub regexp ($;$) { |
72 |
sub regexp ($;$) { |
| 64 |
my $self = shift; |
73 |
my $self = shift; |
| 65 |
my $delim = qr/:?\:|\/|-/; # "non memory" cluster: no backreference |
74 |
my $delim = qr/:?\:|\/|-/; # "non memory" cluster: no backreference |
| 66 |
my $format = (@_) ? _recognize_format(shift) : ($self->{'dateformat'} || _prefformat()); |
75 |
my $format = (@_) ? _recognize_format(shift) : ( $self->{'dateformat'} || _prefformat() ); |
| 67 |
|
76 |
|
| 68 |
# Extra layer of checking $self->{'dateformat'}. |
77 |
# Extra layer of checking $self->{'dateformat'}. |
| 69 |
# Why? Because it is assumed you might want to check regexp against an *instantiated* Dates object as a |
78 |
# Why? Because it is assumed you might want to check regexp against an *instantiated* Dates object as a |
| 70 |
# way of saying "does this string match *whatever* format that Dates object is?" |
79 |
# way of saying "does this string match *whatever* format that Dates object is?" |
| 71 |
|
80 |
|
| 72 |
($format eq 'sql') and |
81 |
( $format eq 'sql' ) |
| 73 |
return qr/^(\d{4})(\d{1,2})(\d{1,2})(?:\s{4}(\d{2})(\d{2})(\d{2}))?/; |
82 |
and return qr/^(\d{4})(\d{1,2})(\d{1,2})(?:\s{4}(\d{2})(\d{2})(\d{2}))?/; |
| 74 |
($format eq 'iso') and |
83 |
( $format eq 'iso' ) |
| 75 |
return qr/^(\d{4})$delim(\d{1,2})$delim(\d{1,2})(?:(?:\s{1}|T)(\d{2})\:?(\d{2})\:?(\d{2}))?Z?/; |
84 |
and return qr/^(\d{4})$delim(\d{1,2})$delim(\d{1,2})(?:(?:\s{1}|T)(\d{2})\:?(\d{2})\:?(\d{2}))?Z?/; |
| 76 |
return qr/^(\d{1,2})$delim(\d{1,2})$delim(\d{4})(?:\s{1}(\d{1,2})\:?(\d{1,2})\:?(\d{1,2}))?/; # everything else |
85 |
( $format eq 'rfc822' ) |
|
|
86 |
and return qr/^([a-zA-Z]{3}),\s{1}(\d{1,2})\s{1}([a-zA-Z]{3})\s{1}(\d{4})\s{1}(\d{1,2})\:(\d{1,2})\:(\d{1,2})\s{1}(([\-|\+]\d{4})|([A-Z]{3}))/; |
| 87 |
return qr/^(\d{1,2})$delim(\d{1,2})$delim(\d{4})(?:\s{1}(\d{1,2})\:?(\d{1,2})\:?(\d{1,2}))?/; # everything else |
| 77 |
} |
88 |
} |
| 78 |
|
89 |
|
| 79 |
sub dmy_map ($$) { |
90 |
sub dmy_map ($$) { |
| 80 |
my $self = shift; |
91 |
my $self = shift; |
| 81 |
my $val = shift or return undef; |
92 |
my $val = shift or return undef; |
| 82 |
my $dformat = $self->{'dateformat'} or return undef; |
93 |
my $dformat = $self->{'dateformat'} or return undef; |
| 83 |
my $re = $self->regexp(); |
94 |
my $re = $self->regexp(); |
| 84 |
my $xsub = $dmy_subs{$dformat}; |
95 |
my $xsub = $dmy_subs{$dformat}; |
| 85 |
$debug and print STDERR "xsub: $xsub \n"; |
96 |
$debug and print STDERR "xsub: $xsub \n"; |
| 86 |
if ($val =~ /$re/) { |
97 |
if ( $val =~ /$re/ ) { |
| 87 |
my $aref = eval $xsub; |
98 |
my $aref = eval $xsub; |
|
|
99 |
if ($dformat eq 'rfc822') { |
| 100 |
$aref = _abbr_to_numeric($aref, $dformat); |
| 101 |
pop(@{$aref}); #pop off tz offset because we are not setup to handle tz conversions just yet |
| 102 |
} |
| 88 |
_check_date_and_time($aref); |
103 |
_check_date_and_time($aref); |
| 89 |
return @{$aref}; |
104 |
push @{$aref}, (-1,-1,1); # for some reason unknown to me, setting isdst to -1 or undef causes strftime to fail to return the tz offset which is required in RFC822 format -chris_n |
| 90 |
} |
105 |
return @{$aref}; |
| 91 |
# $debug and |
106 |
} |
| 92 |
carp "Illegal Date '$val' does not match '$dformat' format: " . $self->visual(); |
107 |
|
| 93 |
return 0; |
108 |
# $debug and |
|
|
109 |
carp "Illegal Date '$val' does not match '$dformat' format: " . $self->visual(); |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
sub _abbr_to_numeric { |
| 114 |
my $aref = shift; |
| 115 |
my $dformat = shift; |
| 116 |
my ($month_abbr, $day_abbr) = ($aref->[4], $aref->[3]) if $dformat eq 'rfc822'; |
| 117 |
|
| 118 |
for( my $i = 0; $i < scalar(@months); $i++ ) { |
| 119 |
if ( $months[$i] =~ /$month_abbr/ ) { |
| 120 |
$aref->[4] = $i-1; |
| 121 |
last; |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
for( my $i = 0; $i < scalar(@days); $i++ ) { |
| 126 |
if ( $days[$i] =~ /$day_abbr/ ) { |
| 127 |
$aref->[3] = $i; |
| 128 |
last; |
| 129 |
} |
| 130 |
}; |
| 131 |
return $aref; |
| 94 |
} |
132 |
} |
| 95 |
|
133 |
|
| 96 |
sub _check_date_and_time { |
134 |
sub _check_date_and_time { |
| 97 |
my $chron_ref = shift; |
135 |
my $chron_ref = shift; |
| 98 |
my ($year, $month, $day) = _chron_to_ymd($chron_ref); |
136 |
my ( $year, $month, $day ) = _chron_to_ymd($chron_ref); |
| 99 |
unless (check_date($year, $month, $day)) { |
137 |
unless ( check_date( $year, $month, $day ) ) { |
| 100 |
carp "Illegal date specified (year = $year, month = $month, day = $day)"; |
138 |
carp "Illegal date specified (year = $year, month = $month, day = $day)"; |
| 101 |
} |
139 |
} |
| 102 |
my ($hour, $minute, $second) = _chron_to_hms($chron_ref); |
140 |
my ( $hour, $minute, $second ) = _chron_to_hms($chron_ref); |
| 103 |
unless (check_time($hour, $minute, $second)) { |
141 |
unless ( check_time( $hour, $minute, $second ) ) { |
| 104 |
carp "Illegal time specified (hour = $hour, minute = $minute, second = $second)"; |
142 |
carp "Illegal time specified (hour = $hour, minute = $minute, second = $second)"; |
| 105 |
} |
143 |
} |
| 106 |
} |
144 |
} |
| 107 |
|
145 |
|
| 108 |
sub _chron_to_ymd { |
146 |
sub _chron_to_ymd { |
| 109 |
my $chron_ref = shift; |
147 |
my $chron_ref = shift; |
| 110 |
return ($chron_ref->[5] + 1900, $chron_ref->[4] + 1, $chron_ref->[3]); |
148 |
return ( $chron_ref->[5] + 1900, $chron_ref->[4] + 1, $chron_ref->[3] ); |
| 111 |
} |
149 |
} |
| 112 |
|
150 |
|
| 113 |
sub _chron_to_hms { |
151 |
sub _chron_to_hms { |
| 114 |
my $chron_ref = shift; |
152 |
my $chron_ref = shift; |
| 115 |
return ($chron_ref->[2], $chron_ref->[1], $chron_ref->[0]); |
153 |
return ( $chron_ref->[2], $chron_ref->[1], $chron_ref->[0] ); |
| 116 |
} |
154 |
} |
| 117 |
|
155 |
|
| 118 |
sub new { |
156 |
sub new { |
| 119 |
my $this = shift; |
157 |
my $this = shift; |
| 120 |
my $class = ref($this) || $this; |
158 |
my $class = ref($this) || $this; |
| 121 |
my $self = {}; |
159 |
my $self = {}; |
| 122 |
bless $self, $class; |
160 |
bless $self, $class; |
| 123 |
return $self->init(@_); |
161 |
return $self->init(@_); |
| 124 |
} |
162 |
} |
|
|
163 |
|
| 125 |
sub init ($;$$) { |
164 |
sub init ($;$$) { |
| 126 |
my $self = shift; |
165 |
my $self = shift; |
| 127 |
my $dformat; |
166 |
my $dformat; |
| 128 |
$self->{'dateformat'} = $dformat = (scalar(@_) >= 2) ? $_[1] : _prefformat(); |
167 |
$self->{'dateformat'} = $dformat = ( scalar(@_) >= 2 ) ? $_[1] : _prefformat(); |
| 129 |
($format_map{$dformat}) or croak |
168 |
( $format_map{$dformat} ) or croak "Invalid date format '$dformat' from " . ( ( scalar(@_) >= 2 ) ? 'argument' : 'system preferences' ); |
| 130 |
"Invalid date format '$dformat' from " . ((scalar(@_) >= 2) ? 'argument' : 'system preferences'); |
169 |
$self->{'dmy_arrayref'} = [ ( (@_) ? $self->dmy_map(shift) : localtime ) ]; |
| 131 |
$self->{'dmy_arrayref'} = [((@_) ? $self->dmy_map(shift) : localtime )] ; |
170 |
$debug and warn "(during init) \@\$self->{'dmy_arrayref'}: " . join( ' ', @{ $self->{'dmy_arrayref'} } ) . "\n"; |
| 132 |
$debug and warn "(during init) \@\$self->{'dmy_arrayref'}: " . join(' ',@{$self->{'dmy_arrayref'}}) . "\n"; |
171 |
return $self; |
| 133 |
return $self; |
|
|
| 134 |
} |
172 |
} |
|
|
173 |
|
| 135 |
sub output ($;$) { |
174 |
sub output ($;$) { |
| 136 |
my $self = shift; |
175 |
my $self = shift; |
| 137 |
my $newformat = (@_) ? _recognize_format(shift) : _prefformat(); |
176 |
my $newformat = (@_) ? _recognize_format(shift) : _prefformat(); |
| 138 |
return (eval {POSIX::strftime($posix_map{$newformat}, @{$self->{'dmy_arrayref'}})} || undef); |
177 |
return ( eval { POSIX::strftime( $posix_map{$newformat}, @{ $self->{'dmy_arrayref'} } ) } || undef ); |
| 139 |
} |
178 |
} |
| 140 |
sub today ($;$) { # NOTE: sets date value to today (and returns it in the requested or current format) |
179 |
|
| 141 |
my $class = shift; |
180 |
sub today ($;$) { # NOTE: sets date value to today (and returns it in the requested or current format) |
| 142 |
$class = ref($class) || $class; |
181 |
my $class = shift; |
| 143 |
my $format = (@_) ? _recognize_format(shift) : _prefformat(); |
182 |
$class = ref($class) || $class; |
| 144 |
return $class->new()->output($format); |
183 |
my $format = (@_) ? _recognize_format(shift) : _prefformat(); |
|
|
184 |
return $class->new()->output($format); |
| 145 |
} |
185 |
} |
|
|
186 |
|
| 146 |
sub _recognize_format($) { |
187 |
sub _recognize_format($) { |
| 147 |
my $incoming = shift; |
188 |
my $incoming = shift; |
| 148 |
($incoming eq 'syspref') and return _prefformat(); |
189 |
( $incoming eq 'syspref' ) and return _prefformat(); |
| 149 |
(scalar grep (/^$incoming$/, keys %format_map) == 1) or croak "The format you asked for ('$incoming') is unrecognized."; |
190 |
( scalar grep ( /^$incoming$/, keys %format_map ) == 1 ) or croak "The format you asked for ('$incoming') is unrecognized."; |
| 150 |
return $incoming; |
191 |
return $incoming; |
| 151 |
} |
192 |
} |
| 152 |
sub DHTMLcalendar ($;$) { # interface to posix_map |
193 |
|
| 153 |
my $class = shift; |
194 |
sub DHTMLcalendar ($;$) { # interface to posix_map |
| 154 |
my $format = (@_) ? shift : _prefformat(); |
195 |
my $class = shift; |
| 155 |
return $posix_map{$format}; |
196 |
my $format = (@_) ? shift : _prefformat(); |
|
|
197 |
return $posix_map{$format}; |
| 156 |
} |
198 |
} |
| 157 |
sub format { # get or set dateformat: iso, metric, us, etc. |
199 |
|
| 158 |
my $self = shift; |
200 |
sub format { # get or set dateformat: iso, metric, us, etc. |
| 159 |
(@_) or return $self->{'dateformat'}; |
201 |
my $self = shift; |
| 160 |
$self->{'dateformat'} = _recognize_format(shift); |
202 |
(@_) or return $self->{'dateformat'}; |
|
|
203 |
$self->{'dateformat'} = _recognize_format(shift); |
| 161 |
} |
204 |
} |
|
|
205 |
|
| 162 |
sub visual { |
206 |
sub visual { |
| 163 |
my $self = shift; |
207 |
my $self = shift; |
| 164 |
if (@_) { |
208 |
if (@_) { |
| 165 |
return $format_map{ _recognize_format(shift) }; |
209 |
return $format_map{ _recognize_format(shift) }; |
| 166 |
} |
210 |
} |
| 167 |
$self eq __PACKAGE__ and return $format_map{_prefformat()}; |
211 |
$self eq __PACKAGE__ and return $format_map{ _prefformat() }; |
| 168 |
return $format_map{ eval { $self->{'dateformat'} } || _prefformat()} ; |
212 |
return $format_map{ eval { $self->{'dateformat'} } || _prefformat() }; |
| 169 |
} |
213 |
} |
| 170 |
|
214 |
|
| 171 |
# like the functions from the old C4::Date.pm |
215 |
# like the functions from the old C4::Date.pm |
| 172 |
sub format_date { |
216 |
sub format_date { |
| 173 |
return __PACKAGE__ -> new(shift,'iso')->output((@_) ? shift : _prefformat()); |
217 |
return __PACKAGE__->new( shift, 'iso' )->output( (@_) ? shift : _prefformat() ); |
| 174 |
} |
218 |
} |
|
|
219 |
|
| 175 |
sub format_date_in_iso { |
220 |
sub format_date_in_iso { |
| 176 |
return __PACKAGE__ -> new(shift,_prefformat())->output('iso'); |
221 |
return __PACKAGE__->new( shift, _prefformat() )->output('iso'); |
| 177 |
} |
222 |
} |
| 178 |
|
223 |
|
| 179 |
1; |
224 |
1; |
|
Lines 192-197
The formats supported by Koha are:
Link Here
|
| 192 |
us - U.S. standard |
237 |
us - U.S. standard |
| 193 |
metric - European standard (slight misnomer, not really decimalized metric) |
238 |
metric - European standard (slight misnomer, not really decimalized metric) |
| 194 |
sql - log format, not really for human consumption |
239 |
sql - log format, not really for human consumption |
|
|
240 |
rfc822 - Standard for using with RSS feeds, etc. |
| 195 |
|
241 |
|
| 196 |
=head2 ->new([string_date,][date_format]) |
242 |
=head2 ->new([string_date,][date_format]) |
| 197 |
|
243 |
|
|
Lines 200-218
used. If date_format is not supplied, the system preference from C4::Context is
Link Here
|
| 200 |
|
246 |
|
| 201 |
Examples: |
247 |
Examples: |
| 202 |
|
248 |
|
| 203 |
my $now = C4::Dates->new(); |
249 |
my $now = C4::Dates->new(); |
| 204 |
my $date1 = C4::Dates->new("09-21-1989","us"); |
250 |
my $date1 = C4::Dates->new("09-21-1989","us"); |
| 205 |
my $date2 = C4::Dates->new("19890921 143907","sql"); |
251 |
my $date2 = C4::Dates->new("19890921 143907","sql"); |
| 206 |
|
252 |
|
| 207 |
=head2 ->output([date_format]) |
253 |
=head2 ->output([date_format]) |
| 208 |
|
254 |
|
| 209 |
The date value is stored independent of any specific format. Therefore any format can be |
255 |
The date value is stored independent of any specific format. Therefore any format can be |
| 210 |
invoked when displaying it. |
256 |
invoked when displaying it. |
| 211 |
|
257 |
|
| 212 |
my $date = C4::Dates->new(); # say today is July 12th, 2010 |
258 |
my $date = C4::Dates->new(); # say today is July 12th, 2010 |
| 213 |
print $date->output("iso"); # prints "2010-07-12" |
259 |
print $date->output("iso"); # prints "2010-07-12" |
| 214 |
print "\n"; |
260 |
print "\n"; |
| 215 |
print $date->output("metric"); # prints "12-07-2010" |
261 |
print $date->output("metric"); # prints "12-07-2010" |
| 216 |
|
262 |
|
| 217 |
However, it is still necessary to know the format of any incoming date value (e.g., |
263 |
However, it is still necessary to know the format of any incoming date value (e.g., |
| 218 |
setting the value of an object with new()). Like new(), output() assumes the system preference |
264 |
setting the value of an object with new()). Like new(), output() assumes the system preference |
|
Lines 228-259
method/function to tell you whether or not a Dates.pm object is of the 'iso' typ
Link Here
|
| 228 |
can see by this example that such a test is trivial to accomplish, and not necessary to |
274 |
can see by this example that such a test is trivial to accomplish, and not necessary to |
| 229 |
include in the module: |
275 |
include in the module: |
| 230 |
|
276 |
|
| 231 |
sub is_iso { |
277 |
sub is_iso { |
| 232 |
my $self = shift; |
278 |
my $self = shift; |
| 233 |
return ($self->format() eq "iso"); |
279 |
return ($self->format() eq "iso"); |
| 234 |
} |
280 |
} |
| 235 |
|
281 |
|
| 236 |
Note: A similar function would need to be included for each format. |
282 |
Note: A similar function would need to be included for each format. |
| 237 |
|
283 |
|
| 238 |
Instead a dependent script can retrieve the format of the object directly and decide what to |
284 |
Instead a dependent script can retrieve the format of the object directly and decide what to |
| 239 |
do with it from there: |
285 |
do with it from there: |
| 240 |
|
286 |
|
| 241 |
my $date = C4::Dates->new(); |
287 |
my $date = C4::Dates->new(); |
| 242 |
my $format = $date->format(); |
288 |
my $format = $date->format(); |
| 243 |
($format eq "iso") or do_something($date); |
289 |
($format eq "iso") or do_something($date); |
| 244 |
|
290 |
|
| 245 |
Or if you just want to print a given value and format, no problem: |
291 |
Or if you just want to print a given value and format, no problem: |
| 246 |
|
292 |
|
| 247 |
my $date = C4::Dates->new("1989-09-21", "iso"); |
293 |
my $date = C4::Dates->new("1989-09-21", "iso"); |
| 248 |
print $date->output; |
294 |
print $date->output; |
| 249 |
|
295 |
|
| 250 |
Alternatively: |
296 |
Alternatively: |
| 251 |
|
297 |
|
| 252 |
print C4::Dates->new("1989-09-21", "iso")->output; |
298 |
print C4::Dates->new("1989-09-21", "iso")->output; |
| 253 |
|
299 |
|
| 254 |
Or even: |
300 |
Or even: |
| 255 |
|
301 |
|
| 256 |
print C4::Dates->new("21-09-1989", "metric")->output("iso"); |
302 |
print C4::Dates->new("21-09-1989", "metric")->output("iso"); |
| 257 |
|
303 |
|
| 258 |
=head2 "syspref" -- System Preference(s) |
304 |
=head2 "syspref" -- System Preference(s) |
| 259 |
|
305 |
|
|
Lines 263-286
psuedo-format argument "syspref".
Link Here
|
| 263 |
|
309 |
|
| 264 |
For example, to print an ISO date (from the database) in the <systempreference> format: |
310 |
For example, to print an ISO date (from the database) in the <systempreference> format: |
| 265 |
|
311 |
|
| 266 |
my $date = C4::Dates->new($date_from_database,"iso"); |
312 |
my $date = C4::Dates->new($date_from_database,"iso"); |
| 267 |
my $datestring_for_display = $date->output("syspref"); |
313 |
my $datestring_for_display = $date->output("syspref"); |
| 268 |
print $datestring_for_display; |
314 |
print $datestring_for_display; |
| 269 |
|
315 |
|
| 270 |
Or even: |
316 |
Or even: |
| 271 |
|
317 |
|
| 272 |
print C4::Dates->new($date_from_database,"iso")->output("syspref"); |
318 |
print C4::Dates->new($date_from_database,"iso")->output("syspref"); |
| 273 |
|
319 |
|
| 274 |
If you just want to know what the <systempreferece> is, a default Dates object can tell you: |
320 |
If you just want to know what the <systempreferece> is, a default Dates object can tell you: |
| 275 |
|
321 |
|
| 276 |
C4::Dates->new()->format(); |
322 |
C4::Dates->new()->format(); |
| 277 |
|
323 |
|
| 278 |
=head2 ->DHMTLcalendar([date_format]) |
324 |
=head2 ->DHMTLcalendar([date_format]) |
| 279 |
|
325 |
|
| 280 |
Returns the format string for DHTML Calendar Display based on date_format. |
326 |
Returns the format string for DHTML Calendar Display based on date_format. |
| 281 |
If date_format is not supplied, the return is based on system preference. |
327 |
If date_format is not supplied, the return is based on system preference. |
| 282 |
|
328 |
|
| 283 |
C4::Dates->DHTMLcalendar(); # e.g., returns "%m/%d/%Y" for 'us' system preference |
329 |
C4::Dates->DHTMLcalendar(); # e.g., returns "%m/%d/%Y" for 'us' system preference |
| 284 |
|
330 |
|
| 285 |
=head3 Error Handling |
331 |
=head3 Error Handling |
| 286 |
|
332 |
|
|
Lines 290-301
fatal error (because it is programmer error, not user error, typically).
Link Here
|
| 290 |
Scripts must still perform validation of user input. Attempting to set an invalid value will |
336 |
Scripts must still perform validation of user input. Attempting to set an invalid value will |
| 291 |
return 0 or undefined, so a script might check as follows: |
337 |
return 0 or undefined, so a script might check as follows: |
| 292 |
|
338 |
|
| 293 |
my $date = C4::Dates->new($input) or deal_with_it("$input didn't work"); |
339 |
my $date = C4::Dates->new($input) or deal_with_it("$input didn't work"); |
| 294 |
|
340 |
|
| 295 |
To validate before creating a new object, use the regexp method of the class: |
341 |
To validate before creating a new object, use the regexp method of the class: |
| 296 |
|
342 |
|
| 297 |
$input =~ C4::Dates->regexp("iso") or deal_with_it("input ($input) invalid as iso format"); |
343 |
$input =~ C4::Dates->regexp("iso") or deal_with_it("input ($input) invalid as iso format"); |
| 298 |
my $date = C4::Dates->new($input,"iso"); |
344 |
my $date = C4::Dates->new($input,"iso"); |
| 299 |
|
345 |
|
| 300 |
More verbose debugging messages are sent in the presence of non-zero $ENV{"DEBUG"}. |
346 |
More verbose debugging messages are sent in the presence of non-zero $ENV{"DEBUG"}. |
| 301 |
|
347 |
|