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.

246 lines
7.8 KiB

  1. package Time::Local;
  2. require 5.000;
  3. require Exporter;
  4. use Carp;
  5. @ISA = qw( Exporter );
  6. @EXPORT = qw( timegm timelocal );
  7. @EXPORT_OK = qw( timegm_nocheck timelocal_nocheck );
  8. # Set up constants
  9. $SEC = 1;
  10. $MIN = 60 * $SEC;
  11. $HR = 60 * $MIN;
  12. $DAY = 24 * $HR;
  13. # Determine breakpoint for rolling century
  14. my $thisYear = (localtime())[5];
  15. $nextCentury = int($thisYear / 100) * 100;
  16. $breakpoint = ($thisYear + 50) % 100;
  17. $nextCentury += 100 if $breakpoint < 50;
  18. my %options;
  19. sub timegm {
  20. my (@date) = @_;
  21. if ($date[5] > 999) {
  22. $date[5] -= 1900;
  23. }
  24. elsif ($date[5] >= 0 && $date[5] < 100) {
  25. $date[5] -= 100 if $date[5] > $breakpoint;
  26. $date[5] += $nextCentury;
  27. }
  28. $ym = pack(C2, @date[5,4]);
  29. $cheat = $cheat{$ym} || &cheat(@date);
  30. $cheat
  31. + $date[0] * $SEC
  32. + $date[1] * $MIN
  33. + $date[2] * $HR
  34. + ($date[3]-1) * $DAY;
  35. }
  36. sub timegm_nocheck {
  37. local $options{no_range_check} = 1;
  38. &timegm;
  39. }
  40. sub timelocal {
  41. my $t = &timegm;
  42. my $tt = $t;
  43. my (@lt) = localtime($t);
  44. my (@gt) = gmtime($t);
  45. if ($t < $DAY and ($lt[5] >= 70 or $gt[5] >= 70 )) {
  46. # Wrap error, too early a date
  47. # Try a safer date
  48. $tt += $DAY;
  49. @lt = localtime($tt);
  50. @gt = gmtime($tt);
  51. }
  52. my $tzsec = ($gt[1] - $lt[1]) * $MIN + ($gt[2] - $lt[2]) * $HR;
  53. if($lt[5] > $gt[5]) {
  54. $tzsec -= $DAY;
  55. }
  56. elsif($gt[5] > $lt[5]) {
  57. $tzsec += $DAY;
  58. }
  59. else {
  60. $tzsec += ($gt[7] - $lt[7]) * $DAY;
  61. }
  62. $tzsec += $HR if($lt[8]);
  63. $time = $t + $tzsec;
  64. @test = localtime($time + ($tt - $t));
  65. $time -= $HR if $test[2] != $_[2];
  66. $time;
  67. }
  68. sub timelocal_nocheck {
  69. local $options{no_range_check} = 1;
  70. &timelocal;
  71. }
  72. sub cheat {
  73. $year = $_[5];
  74. $month = $_[4];
  75. unless ($options{no_range_check}) {
  76. croak "Month '$month' out of range 0..11" if $month > 11 || $month < 0;
  77. croak "Day '$_[3]' out of range 1..31" if $_[3] > 31 || $_[3] < 1;
  78. croak "Hour '$_[2]' out of range 0..23" if $_[2] > 23 || $_[2] < 0;
  79. croak "Minute '$_[1]' out of range 0..59" if $_[1] > 59 || $_[1] < 0;
  80. croak "Second '$_[0]' out of range 0..59" if $_[0] > 59 || $_[0] < 0;
  81. }
  82. $guess = $^T;
  83. @g = gmtime($guess);
  84. $lastguess = "";
  85. $counter = 0;
  86. while ($diff = $year - $g[5]) {
  87. croak "Can't handle date (".join(", ",@_).")" if ++$counter > 255;
  88. $guess += $diff * (363 * $DAY);
  89. @g = gmtime($guess);
  90. if (($thisguess = "@g") eq $lastguess){
  91. croak "Can't handle date (".join(", ",@_).")";
  92. #date beyond this machine's integer limit
  93. }
  94. $lastguess = $thisguess;
  95. }
  96. while ($diff = $month - $g[4]) {
  97. croak "Can't handle date (".join(", ",@_).")" if ++$counter > 255;
  98. $guess += $diff * (27 * $DAY);
  99. @g = gmtime($guess);
  100. if (($thisguess = "@g") eq $lastguess){
  101. croak "Can't handle date (".join(", ",@_).")";
  102. #date beyond this machine's integer limit
  103. }
  104. $lastguess = $thisguess;
  105. }
  106. @gfake = gmtime($guess-1); #still being sceptic
  107. if ("@gfake" eq $lastguess){
  108. croak "Can't handle date (".join(", ",@_).")";
  109. #date beyond this machine's integer limit
  110. }
  111. $g[3]--;
  112. $guess -= $g[0] * $SEC + $g[1] * $MIN + $g[2] * $HR + $g[3] * $DAY;
  113. $cheat{$ym} = $guess;
  114. }
  115. 1;
  116. __END__
  117. =head1 NAME
  118. Time::Local - efficiently compute time from local and GMT time
  119. =head1 SYNOPSIS
  120. $time = timelocal($sec,$min,$hours,$mday,$mon,$year);
  121. $time = timegm($sec,$min,$hours,$mday,$mon,$year);
  122. =head1 DESCRIPTION
  123. These routines are the inverse of built-in perl fuctions localtime()
  124. and gmtime(). They accept a date as a six-element array, and return
  125. the corresponding time(2) value in seconds since the Epoch (Midnight,
  126. January 1, 1970). This value can be positive or negative.
  127. It is worth drawing particular attention to the expected ranges for
  128. the values provided. While the day of the month is expected to be in
  129. the range 1..31, the month should be in the range 0..11.
  130. This is consistent with the values returned from localtime() and gmtime().
  131. The timelocal() and timegm() functions perform range checking on the
  132. input $sec, $min, $hours, $mday, and $mon values by default. If you'd
  133. rather they didn't, you can explicitly import the timelocal_nocheck()
  134. and timegm_nocheck() functions.
  135. use Time::Local 'timelocal_nocheck';
  136. {
  137. # The 365th day of 1999
  138. print scalar localtime timelocal_nocheck 0,0,0,365,0,99;
  139. # The twenty thousandth day since 1970
  140. print scalar localtime timelocal_nocheck 0,0,0,20000,0,70;
  141. # And even the 10,000,000th second since 1999!
  142. print scalar localtime timelocal_nocheck 10000000,0,0,1,0,99;
  143. }
  144. Your mileage may vary when trying these with minutes and hours,
  145. and it doesn't work at all for months.
  146. Strictly speaking, the year should also be specified in a form consistent
  147. with localtime(), i.e. the offset from 1900.
  148. In order to make the interpretation of the year easier for humans,
  149. however, who are more accustomed to seeing years as two-digit or four-digit
  150. values, the following conventions are followed:
  151. =over 4
  152. =item *
  153. Years greater than 999 are interpreted as being the actual year,
  154. rather than the offset from 1900. Thus, 1963 would indicate the year
  155. Martin Luther King won the Nobel prize, not the year 2863.
  156. =item *
  157. Years in the range 100..999 are interpreted as offset from 1900,
  158. so that 112 indicates 2012. This rule also applies to years less than zero
  159. (but see note below regarding date range).
  160. =item *
  161. Years in the range 0..99 are interpreted as shorthand for years in the
  162. rolling "current century," defined as 50 years on either side of the current
  163. year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045,
  164. but 55 would refer to 1955. Twenty years from now, 55 would instead refer
  165. to 2055. This is messy, but matches the way people currently think about
  166. two digit dates. Whenever possible, use an absolute four digit year instead.
  167. =back
  168. The scheme above allows interpretation of a wide range of dates, particularly
  169. if 4-digit years are used.
  170. Please note, however, that the range of dates that can be actually be handled
  171. depends on the size of an integer (time_t) on a given platform.
  172. Currently, this is 32 bits for most systems, yielding an approximate range
  173. from Dec 1901 to Jan 2038.
  174. Both timelocal() and timegm() croak if given dates outside the supported
  175. range.
  176. =head1 IMPLEMENTATION
  177. These routines are quite efficient and yet are always guaranteed to agree
  178. with localtime() and gmtime(). We manage this by caching the start times
  179. of any months we've seen before. If we know the start time of the month,
  180. we can always calculate any time within the month. The start times
  181. themselves are guessed by successive approximation starting at the
  182. current time, since most dates seen in practice are close to the
  183. current date. Unlike algorithms that do a binary search (calling gmtime
  184. once for each bit of the time value, resulting in 32 calls), this algorithm
  185. calls it at most 6 times, and usually only once or twice. If you hit
  186. the month cache, of course, it doesn't call it at all.
  187. timelocal() is implemented using the same cache. We just assume that we're
  188. translating a GMT time, and then fudge it when we're done for the timezone
  189. and daylight savings arguments. Note that the timezone is evaluated for
  190. each date because countries occasionally change their official timezones.
  191. Assuming that localtime() corrects for these changes, this routine will
  192. also be correct. The daylight savings offset is currently assumed
  193. to be one hour.
  194. =head1 BUGS
  195. The whole scheme for interpreting two-digit years can be considered a bug.
  196. Note that the cache currently handles only years from 1900 through 2155.
  197. The proclivity to croak() is probably a bug.
  198. =cut