Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
11 KiB

  1. package HTTP::Date; # $Date: 2001/01/04 20:27:15 $
  2. $VERSION = sprintf("%d.%02d", q$Revision: 1.43 $ =~ /(\d+)\.(\d+)/);
  3. require 5.004;
  4. require Exporter;
  5. @ISA = qw(Exporter);
  6. @EXPORT = qw(time2str str2time);
  7. @EXPORT_OK = qw(parse_date time2iso time2isoz);
  8. use strict;
  9. require Time::Local;
  10. use vars qw(@DoW @MoY %MoY);
  11. @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
  12. @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  13. @MoY{@MoY} = (1..12);
  14. my %GMT_ZONE = (GMT => 1, UTC => 1, UT => 1, Z => 1);
  15. sub time2str (;$)
  16. {
  17. my $time = shift;
  18. $time = time unless defined $time;
  19. my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($time);
  20. sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
  21. $DoW[$wday],
  22. $mday, $MoY[$mon], $year+1900,
  23. $hour, $min, $sec);
  24. }
  25. sub str2time ($;$)
  26. {
  27. my $str = shift;
  28. return undef unless defined $str;
  29. # fast exit for strictly conforming string
  30. 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$/) {
  31. return eval {
  32. my $t = Time::Local::timegm($6, $5, $4, $1, $MoY{$2}-1, $3-1900);
  33. $t < 0 ? undef : $t;
  34. };
  35. }
  36. my @d = parse_date($str);
  37. return undef unless @d;
  38. $d[0] -= 1900; # year
  39. $d[1]--; # month
  40. my $tz = pop(@d);
  41. unless (defined $tz) {
  42. unless (defined($tz = shift)) {
  43. return eval { my $t = Time::Local::timelocal(reverse @d);
  44. $t < 0 ? undef : $t;
  45. };
  46. }
  47. }
  48. my $offset = 0;
  49. if ($GMT_ZONE{uc $tz}) {
  50. # offset already zero
  51. }
  52. elsif ($tz =~ /^([-+])?(\d\d?):?(\d\d)?$/) {
  53. $offset = 3600 * $2;
  54. $offset += 60 * $3 if $3;
  55. $offset *= -1 if $1 && $1 eq '-';
  56. }
  57. else {
  58. eval { require Time::Zone } || return undef;
  59. $offset = Time::Zone::tz_offset($tz);
  60. return undef unless defined $offset;
  61. }
  62. return eval { my $t = Time::Local::timegm(reverse @d);
  63. $t < 0 ? undef : $t - $offset;
  64. };
  65. }
  66. sub parse_date ($)
  67. {
  68. local($_) = shift;
  69. return unless defined;
  70. # More lax parsing below
  71. s/^\s+//; # kill leading space
  72. s/^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*//i; # Useless weekday
  73. my($day, $mon, $yr, $hr, $min, $sec, $tz, $ampm);
  74. # Then we are able to check for most of the formats with this regexp
  75. (($day,$mon,$yr,$hr,$min,$sec,$tz) =
  76. /^
  77. (\d\d?) # day
  78. (?:\s+|[-\/])
  79. (\w+) # month
  80. (?:\s+|[-\/])
  81. (\d+) # year
  82. (?:
  83. (?:\s+|:) # separator before clock
  84. (\d\d?):(\d\d) # hour:min
  85. (?::(\d\d))? # optional seconds
  86. )? # optional clock
  87. \s*
  88. ([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone
  89. \s*
  90. (?:\(\w+\))? # ASCII representation of timezone in parens.
  91. \s*$
  92. /x)
  93. ||
  94. # Try the ctime and asctime format
  95. (($mon, $day, $hr, $min, $sec, $tz, $yr) =
  96. /^
  97. (\w{1,3}) # month
  98. \s+
  99. (\d\d?) # day
  100. \s+
  101. (\d\d?):(\d\d) # hour:min
  102. (?::(\d\d))? # optional seconds
  103. \s+
  104. (?:([A-Za-z]+)\s+)? # optional timezone
  105. (\d+) # year
  106. \s*$ # allow trailing whitespace
  107. /x)
  108. ||
  109. # Then the Unix 'ls -l' date format
  110. (($mon, $day, $yr, $hr, $min, $sec) =
  111. /^
  112. (\w{3}) # month
  113. \s+
  114. (\d\d?) # day
  115. \s+
  116. (?:
  117. (\d\d\d\d) | # year
  118. (\d{1,2}):(\d{2}) # hour:min
  119. (?::(\d\d))? # optional seconds
  120. )
  121. \s*$
  122. /x)
  123. ||
  124. # ISO 8601 format '1996-02-29 12:00:00 -0100' and variants
  125. (($yr, $mon, $day, $hr, $min, $sec, $tz) =
  126. /^
  127. (\d{4}) # year
  128. [-\/]?
  129. (\d\d?) # numerical month
  130. [-\/]?
  131. (\d\d?) # day
  132. (?:
  133. (?:\s+|[-:Tt]) # separator before clock
  134. (\d\d?):?(\d\d) # hour:min
  135. (?::?(\d\d(?:\.\d*)?))? # optional seconds (and fractional)
  136. )? # optional clock
  137. \s*
  138. ([-+]?\d\d?:?(:?\d\d)?
  139. |Z|z)? # timezone (Z is "zero meridian", i.e. GMT)
  140. \s*$
  141. /x)
  142. ||
  143. # Windows 'dir' 11-12-96 03:52PM
  144. (($mon, $day, $yr, $hr, $min, $ampm) =
  145. /^
  146. (\d{2}) # numerical month
  147. -
  148. (\d{2}) # day
  149. -
  150. (\d{2}) # year
  151. \s+
  152. (\d\d?):(\d\d)([APap][Mm]) # hour:min AM or PM
  153. \s*$
  154. /x)
  155. ||
  156. return; # unrecognized format
  157. # Translate month name to number
  158. $mon = $MoY{$mon} ||
  159. $MoY{"\u\L$mon"} ||
  160. ($mon >= 1 && $mon <= 12 && int($mon)) ||
  161. return;
  162. # If the year is missing, we assume first date before the current,
  163. # because of the formats we support such dates are mostly present
  164. # on "ls -l" listings.
  165. unless (defined $yr) {
  166. my $cur_mon;
  167. ($cur_mon, $yr) = (localtime)[4, 5];
  168. $yr += 1900;
  169. $cur_mon++;
  170. $yr-- if $mon > $cur_mon;
  171. }
  172. elsif (length($yr) < 3) {
  173. # Find "obvious" year
  174. my $cur_yr = (localtime)[5] + 1900;
  175. my $m = $cur_yr % 100;
  176. my $tmp = $yr;
  177. $yr += $cur_yr - $m;
  178. $m -= $tmp;
  179. $yr += ($m > 0) ? 100 : -100
  180. if abs($m) > 50;
  181. }
  182. # Make sure clock elements are defined
  183. $hr = 0 unless defined($hr);
  184. $min = 0 unless defined($min);
  185. $sec = 0 unless defined($sec);
  186. # Compensate for AM/PM
  187. if ($ampm) {
  188. $ampm = uc $ampm;
  189. $hr = 0 if $hr == 12 && $ampm eq 'AM';
  190. $hr += 12 if $ampm eq 'PM' && $hr != 12;
  191. }
  192. return($yr, $mon, $day, $hr, $min, $sec, $tz)
  193. if wantarray;
  194. if (defined $tz) {
  195. $tz = "Z" if $tz =~ /^(GMT|UTC?|[-+]?0+)$/;
  196. } else {
  197. $tz = "";
  198. }
  199. return sprintf("%04d-%02d-%02d %02d:%02d:%02d%s",
  200. $yr, $mon, $day, $hr, $min, $sec, $tz);
  201. }
  202. sub time2iso (;$)
  203. {
  204. my $time = shift;
  205. $time = time unless defined $time;
  206. my($sec,$min,$hour,$mday,$mon,$year) = localtime($time);
  207. sprintf("%04d-%02d-%02d %02d:%02d:%02d",
  208. $year+1900, $mon+1, $mday, $hour, $min, $sec);
  209. }
  210. sub time2isoz (;$)
  211. {
  212. my $time = shift;
  213. $time = time unless defined $time;
  214. my($sec,$min,$hour,$mday,$mon,$year) = gmtime($time);
  215. sprintf("%04d-%02d-%02d %02d:%02d:%02dZ",
  216. $year+1900, $mon+1, $mday, $hour, $min, $sec);
  217. }
  218. 1;
  219. __END__
  220. =head1 NAME
  221. HTTP::Date - date conversion routines
  222. =head1 SYNOPSIS
  223. use HTTP::Date;
  224. $string = time2str($time); # Format as GMT ASCII time
  225. $time = str2time($string); # convert ASCII date to machine time
  226. =head1 DESCRIPTION
  227. This module provides functions that deal the date formats used by the
  228. HTTP protocol (and then some more). Only the first two functions,
  229. time2str() and str2time(), are exported by default.
  230. =over 4
  231. =item time2str( [$time] )
  232. The time2str() function converts a machine time (seconds since epoch)
  233. to a string. If the function is called without an argument, it will
  234. use the current time.
  235. The string returned is in the format preferred for the HTTP protocol.
  236. This is a fixed length subset of the format defined by RFC 1123,
  237. represented in Universal Time (GMT). An example of a time stamp
  238. in this format is:
  239. Sun, 06 Nov 1994 08:49:37 GMT
  240. =item str2time( $str [, $zone] )
  241. The str2time() function converts a string to machine time. It returns
  242. C<undef> if the format of $str is unrecognized, or the time is outside
  243. the representable range. The time formats recognized are the same as
  244. for parse_date().
  245. The function also takes an optional second argument that specifies the
  246. default time zone to use when converting the date. This parameter is
  247. ignored if the zone is found in the date string itself. If this
  248. parameter is missing, and the date string format does not contain any
  249. zone specification, then the local time zone is assumed.
  250. If the zone is not "C<GMT>" or numerical (like "C<-0800>" or
  251. "C<+0100>"), then the C<Time::Zone> module must be installed in order
  252. to get the date recognized.
  253. =item parse_date( $str )
  254. This function will try to parse a date string, and then return it as a
  255. list of numerical values followed by a (possible undefined) time zone
  256. specifier; ($year, $month, $day, $hour, $min, $sec, $tz). The $year
  257. returned will B<not> have the number 1900 subtracted from it and the
  258. $month numbers start with 1.
  259. In scalar context the numbers are interpolated in a string of the
  260. "YYYY-MM-DD hh:mm:ss TZ"-format and returned.
  261. If the date is unrecognized, then the empty list is returned.
  262. The function is able to parse the following formats:
  263. "Wed, 09 Feb 1994 22:23:32 GMT" -- HTTP format
  264. "Thu Feb 3 17:03:55 GMT 1994" -- ctime(3) format
  265. "Thu Feb 3 00:00:00 1994", -- ANSI C asctime() format
  266. "Tuesday, 08-Feb-94 14:15:29 GMT" -- old rfc850 HTTP format
  267. "Tuesday, 08-Feb-1994 14:15:29 GMT" -- broken rfc850 HTTP format
  268. "03/Feb/1994:17:03:55 -0700" -- common logfile format
  269. "09 Feb 1994 22:23:32 GMT" -- HTTP format (no weekday)
  270. "08-Feb-94 14:15:29 GMT" -- rfc850 format (no weekday)
  271. "08-Feb-1994 14:15:29 GMT" -- broken rfc850 format (no weekday)
  272. "1994-02-03 14:15:29 -0100" -- ISO 8601 format
  273. "1994-02-03 14:15:29" -- zone is optional
  274. "1994-02-03" -- only date
  275. "1994-02-03T14:15:29" -- Use T as separator
  276. "19940203T141529Z" -- ISO 8601 compact format
  277. "19940203" -- only date
  278. "08-Feb-94" -- old rfc850 HTTP format (no weekday, no time)
  279. "08-Feb-1994" -- broken rfc850 HTTP format (no weekday, no time)
  280. "09 Feb 1994" -- proposed new HTTP format (no weekday, no time)
  281. "03/Feb/1994" -- common logfile format (no time, no offset)
  282. "Feb 3 1994" -- Unix 'ls -l' format
  283. "Feb 3 17:03" -- Unix 'ls -l' format
  284. "11-15-96 03:52PM" -- Windows 'dir' format
  285. The parser ignores leading and trailing whitespace. It also allow the
  286. seconds to be missing and the month to be numerical in most formats.
  287. If the year is missing, then we assume that the date is the first
  288. matching date I<before> current month. If the year is given with only
  289. 2 digits, then parse_date() will select the century that makes the
  290. year closest to the current date.
  291. =item time2iso( [$time] )
  292. Same as time2str(), but returns a "YYYY-MM-DD hh:mm:ss"-formatted
  293. string representing time in the local time zone.
  294. =item time2isoz( [$time] )
  295. Same as time2str(), but returns a "YYYY-MM-DD hh:mm:ssZ"-formatted
  296. string representing Universal Time.
  297. =back
  298. =head1 SEE ALSO
  299. L<perlfunc/time>, L<Time::Zone>
  300. =head1 COPYRIGHT
  301. Copyright 1995-1999, Gisle Aas
  302. This library is free software; you can redistribute it and/or
  303. modify it under the same terms as Perl itself.
  304. =cut