8 @EXPORT = qw(time2str str2time);
9 @EXPORT_OK = qw(parse_date time2iso time2isoz);
14 use vars qw(@DoW @MoY %MoY);
15 @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
16 @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
19 my %GMT_ZONE = (GMT => 1, UTC => 1, UT => 1, Z => 1);
25 $time = time unless defined $time;
26 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($time);
27 sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
29 $mday, $MoY[$mon], $year+1900,
37 return undef unless defined $str;
39 # fast exit for strictly conforming string
40 if ($str =~ /^[SMTWF][a-z][a-z], (\d\d) ([JFMAJSOND][a-z][a-z]) (\d\d\d\d) (\d\d):(\d\d):(\d\d) GMT$/) {
42 my $t = Time::Local::timegm($6, $5, $4, $1, $MoY{$2}-1, $3);
47 my @d = parse_date($str);
48 return undef unless @d;
52 unless (defined $tz) {
53 unless (defined($tz = shift)) {
54 return eval { my $frac = $d[-1]; $frac -= ($d[-1] = int($frac));
55 my $t = Time::Local::timelocal(reverse @d) + $frac;
62 if ($GMT_ZONE{uc $tz}) {
65 elsif ($tz =~ /^([-+])?(\d\d?):?(\d\d)?$/) {
67 $offset += 60 * $3 if $3;
68 $offset *= -1 if $1 && $1 eq '-';
71 eval { require Time::Zone } || return undef;
72 $offset = Time::Zone::tz_offset($tz);
73 return undef unless defined $offset;
76 return eval { my $frac = $d[-1]; $frac -= ($d[-1] = int($frac));
77 my $t = Time::Local::timegm(reverse @d) + $frac;
78 $t < 0 ? undef : $t - $offset;
86 return unless defined;
88 # More lax parsing below
89 s/^\s+//; # kill leading space
90 s/^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*//i; # Useless weekday
92 my($day, $mon, $yr, $hr, $min, $sec, $tz, $ampm);
94 # Then we are able to check for most of the formats with this regexp
95 (($day,$mon,$yr,$hr,$min,$sec,$tz) =
103 (?:\s+|:) # separator before clock
104 (\d\d?):(\d\d) # hour:min
105 (?::(\d\d))? # optional seconds
108 ([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone
110 (?:\(\w+\))? # ASCII representation of timezone in parens.
116 # Try the ctime and asctime format
117 (($mon, $day, $hr, $min, $sec, $tz, $yr) =
123 (\d\d?):(\d\d) # hour:min
124 (?::(\d\d))? # optional seconds
126 (?:([A-Za-z]+)\s+)? # optional timezone
128 \s*$ # allow trailing whitespace
133 # Then the Unix 'ls -l' date format
134 (($mon, $day, $yr, $hr, $min, $sec) =
142 (\d{1,2}):(\d{2}) # hour:min
143 (?::(\d\d))? # optional seconds
150 # ISO 8601 format '1996-02-29 12:00:00 -0100' and variants
151 (($yr, $mon, $day, $hr, $min, $sec, $tz) =
155 (\d\d?) # numerical month
159 (?:\s+|[-:Tt]) # separator before clock
160 (\d\d?):?(\d\d) # hour:min
161 (?::?(\d\d(?:\.\d*)?))? # optional seconds (and fractional)
164 ([-+]?\d\d?:?(:?\d\d)?
165 |Z|z)? # timezone (Z is "zero meridian", i.e. GMT)
171 # Windows 'dir' 11-12-96 03:52PM
172 (($mon, $day, $yr, $hr, $min, $ampm) =
174 (\d{2}) # numerical month
180 (\d\d?):(\d\d)([APap][Mm]) # hour:min AM or PM
185 return; # unrecognized format
187 # Translate month name to number
190 ($mon =~ /^\d\d?$/ && $mon >= 1 && $mon <= 12 && int($mon)) ||
193 # If the year is missing, we assume first date before the current,
194 # because of the formats we support such dates are mostly present
195 # on "ls -l" listings.
196 unless (defined $yr) {
198 ($cur_mon, $yr) = (localtime)[4, 5];
201 $yr-- if $mon > $cur_mon;
203 elsif (length($yr) < 3) {
204 # Find "obvious" year
205 my $cur_yr = (localtime)[5] + 1900;
206 my $m = $cur_yr % 100;
210 $yr += ($m > 0) ? 100 : -100
214 # Make sure clock elements are defined
215 $hr = 0 unless defined($hr);
216 $min = 0 unless defined($min);
217 $sec = 0 unless defined($sec);
219 # Compensate for AM/PM
222 $hr = 0 if $hr == 12 && $ampm eq 'AM';
223 $hr += 12 if $ampm eq 'PM' && $hr != 12;
226 return($yr, $mon, $day, $hr, $min, $sec, $tz)
230 $tz = "Z" if $tz =~ /^(GMT|UTC?|[-+]?0+)$/;
235 return sprintf("%04d-%02d-%02d %02d:%02d:%02d%s",
236 $yr, $mon, $day, $hr, $min, $sec, $tz);
243 $time = time unless defined $time;
244 my($sec,$min,$hour,$mday,$mon,$year) = localtime($time);
245 sprintf("%04d-%02d-%02d %02d:%02d:%02d",
246 $year+1900, $mon+1, $mday, $hour, $min, $sec);
253 $time = time unless defined $time;
254 my($sec,$min,$hour,$mday,$mon,$year) = gmtime($time);
255 sprintf("%04d-%02d-%02d %02d:%02d:%02dZ",
256 $year+1900, $mon+1, $mday, $hour, $min, $sec);
266 HTTP::Date - date conversion routines
272 $string = time2str($time); # Format as GMT ASCII time
273 $time = str2time($string); # convert ASCII date to machine time
277 This module provides functions that deal the date formats used by the
278 HTTP protocol (and then some more). Only the first two functions,
279 time2str() and str2time(), are exported by default.
283 =item time2str( [$time] )
285 The time2str() function converts a machine time (seconds since epoch)
286 to a string. If the function is called without an argument, it will
287 use the current time.
289 The string returned is in the format preferred for the HTTP protocol.
290 This is a fixed length subset of the format defined by RFC 1123,
291 represented in Universal Time (GMT). An example of a time stamp
294 Sun, 06 Nov 1994 08:49:37 GMT
296 =item str2time( $str [, $zone] )
298 The str2time() function converts a string to machine time. It returns
299 C<undef> if the format of $str is unrecognized, otherwise whatever the
300 C<Time::Local> functions can make out of the parsed time. Dates
301 before the system's epoch may not work on all operating systems. The
302 time formats recognized are the same as for parse_date().
304 The function also takes an optional second argument that specifies the
305 default time zone to use when converting the date. This parameter is
306 ignored if the zone is found in the date string itself. If this
307 parameter is missing, and the date string format does not contain any
308 zone specification, then the local time zone is assumed.
310 If the zone is not "C<GMT>" or numerical (like "C<-0800>" or
311 "C<+0100>"), then the C<Time::Zone> module must be installed in order
312 to get the date recognized.
314 =item parse_date( $str )
316 This function will try to parse a date string, and then return it as a
317 list of numerical values followed by a (possible undefined) time zone
318 specifier; ($year, $month, $day, $hour, $min, $sec, $tz). The $year
319 returned will B<not> have the number 1900 subtracted from it and the
320 $month numbers start with 1.
322 In scalar context the numbers are interpolated in a string of the
323 "YYYY-MM-DD hh:mm:ss TZ"-format and returned.
325 If the date is unrecognized, then the empty list is returned.
327 The function is able to parse the following formats:
329 "Wed, 09 Feb 1994 22:23:32 GMT" -- HTTP format
330 "Thu Feb 3 17:03:55 GMT 1994" -- ctime(3) format
331 "Thu Feb 3 00:00:00 1994", -- ANSI C asctime() format
332 "Tuesday, 08-Feb-94 14:15:29 GMT" -- old rfc850 HTTP format
333 "Tuesday, 08-Feb-1994 14:15:29 GMT" -- broken rfc850 HTTP format
335 "03/Feb/1994:17:03:55 -0700" -- common logfile format
336 "09 Feb 1994 22:23:32 GMT" -- HTTP format (no weekday)
337 "08-Feb-94 14:15:29 GMT" -- rfc850 format (no weekday)
338 "08-Feb-1994 14:15:29 GMT" -- broken rfc850 format (no weekday)
340 "1994-02-03 14:15:29 -0100" -- ISO 8601 format
341 "1994-02-03 14:15:29" -- zone is optional
342 "1994-02-03" -- only date
343 "1994-02-03T14:15:29" -- Use T as separator
344 "19940203T141529Z" -- ISO 8601 compact format
345 "19940203" -- only date
347 "08-Feb-94" -- old rfc850 HTTP format (no weekday, no time)
348 "08-Feb-1994" -- broken rfc850 HTTP format (no weekday, no time)
349 "09 Feb 1994" -- proposed new HTTP format (no weekday, no time)
350 "03/Feb/1994" -- common logfile format (no time, no offset)
352 "Feb 3 1994" -- Unix 'ls -l' format
353 "Feb 3 17:03" -- Unix 'ls -l' format
355 "11-15-96 03:52PM" -- Windows 'dir' format
357 The parser ignores leading and trailing whitespace. It also allow the
358 seconds to be missing and the month to be numerical in most formats.
360 If the year is missing, then we assume that the date is the first
361 matching date I<before> current month. If the year is given with only
362 2 digits, then parse_date() will select the century that makes the
363 year closest to the current date.
365 =item time2iso( [$time] )
367 Same as time2str(), but returns a "YYYY-MM-DD hh:mm:ss"-formatted
368 string representing time in the local time zone.
370 =item time2isoz( [$time] )
372 Same as time2str(), but returns a "YYYY-MM-DD hh:mm:ssZ"-formatted
373 string representing Universal Time.
380 L<perlfunc/time>, L<Time::Zone>
384 Copyright 1995-1999, Gisle Aas
386 This library is free software; you can redistribute it and/or
387 modify it under the same terms as Perl itself.