Source code of Windows XP (NT5)
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.

140 lines
4.1 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. =head1 NAME
  8. Time::Local - efficiently compute time from local and GMT time
  9. =head1 SYNOPSIS
  10. $time = timelocal($sec,$min,$hours,$mday,$mon,$year);
  11. $time = timegm($sec,$min,$hours,$mday,$mon,$year);
  12. =head1 DESCRIPTION
  13. These routines are quite efficient and yet are always guaranteed to
  14. agree with localtime() and gmtime(), the most notable points being
  15. that year is year-1900 and month is 0..11. We manage this by caching
  16. the start times of any months we've seen before. If we know the start
  17. time of the month, we can always calculate any time within the month.
  18. The start times themselves are guessed by successive approximation
  19. starting at the current time, since most dates seen in practice are
  20. close to the current date. Unlike algorithms that do a binary search
  21. (calling gmtime once for each bit of the time value, resulting in 32
  22. calls), this algorithm calls it at most 6 times, and usually only once
  23. or twice. If you hit the month cache, of course, it doesn't call it
  24. at all.
  25. timelocal is implemented using the same cache. We just assume that we're
  26. translating a GMT time, and then fudge it when we're done for the timezone
  27. and daylight savings arguments. The timezone is determined by examining
  28. the result of localtime(0) when the package is initialized. The daylight
  29. savings offset is currently assumed to be one hour.
  30. Both routines return -1 if the integer limit is hit. I.e. for dates
  31. after the 1st of January, 2038 on most machines.
  32. =cut
  33. BEGIN {
  34. $SEC = 1;
  35. $MIN = 60 * $SEC;
  36. $HR = 60 * $MIN;
  37. $DAY = 24 * $HR;
  38. $epoch = (localtime(2*$DAY))[5]; # Allow for bugs near localtime == 0.
  39. $YearFix = ((gmtime(946684800))[5] == 100) ? 100 : 0;
  40. }
  41. sub timegm {
  42. $ym = pack(C2, @_[5,4]);
  43. $cheat = $cheat{$ym} || &cheat;
  44. return -1 if $cheat<0 and $^O ne 'VMS';
  45. $cheat + $_[0] * $SEC + $_[1] * $MIN + $_[2] * $HR + ($_[3]-1) * $DAY;
  46. }
  47. sub timelocal {
  48. my $t = &timegm;
  49. my $tt = $t;
  50. my (@lt) = localtime($t);
  51. my (@gt) = gmtime($t);
  52. if ($t < $DAY and ($lt[5] >= 70 or $gt[5] >= 70 )) {
  53. # Wrap error, too early a date
  54. # Try a safer date
  55. $tt = $DAY;
  56. @lt = localtime($tt);
  57. @gt = gmtime($tt);
  58. }
  59. my $tzsec = ($gt[1] - $lt[1]) * $MIN + ($gt[2] - $lt[2]) * $HR;
  60. my($lday,$gday) = ($lt[7],$gt[7]);
  61. if($lt[5] > $gt[5]) {
  62. $tzsec -= $DAY;
  63. }
  64. elsif($gt[5] > $lt[5]) {
  65. $tzsec += $DAY;
  66. }
  67. else {
  68. $tzsec += ($gt[7] - $lt[7]) * $DAY;
  69. }
  70. $tzsec += $HR if($lt[8]);
  71. $time = $t + $tzsec;
  72. return -1 if $cheat<0 and $^O ne 'VMS';
  73. @test = localtime($time + ($tt - $t));
  74. $time -= $HR if $test[2] != $_[2];
  75. $time;
  76. }
  77. sub cheat {
  78. $year = $_[5];
  79. $year -= 1900
  80. if $year > 1900;
  81. $month = $_[4];
  82. croak "Month '$month' out of range 0..11" if $month > 11 || $month < 0;
  83. croak "Day '$_[3]' out of range 1..31" if $_[3] > 31 || $_[3] < 1;
  84. croak "Hour '$_[2]' out of range 0..23" if $_[2] > 23 || $_[2] < 0;
  85. croak "Minute '$_[1]' out of range 0..59" if $_[1] > 59 || $_[1] < 0;
  86. croak "Second '$_[0]' out of range 0..59" if $_[0] > 59 || $_[0] < 0;
  87. $guess = $^T;
  88. @g = gmtime($guess);
  89. $year += $YearFix if $year < $epoch;
  90. $lastguess = "";
  91. $counter = 0;
  92. while ($diff = $year - $g[5]) {
  93. croak "Can't handle date (".join(", ",@_).")" if ++$counter > 255;
  94. $guess += $diff * (363 * $DAY);
  95. @g = gmtime($guess);
  96. if (($thisguess = "@g") eq $lastguess){
  97. return -1; #date beyond this machine's integer limit
  98. }
  99. $lastguess = $thisguess;
  100. }
  101. while ($diff = $month - $g[4]) {
  102. croak "Can't handle date (".join(", ",@_).")" if ++$counter > 255;
  103. $guess += $diff * (27 * $DAY);
  104. @g = gmtime($guess);
  105. if (($thisguess = "@g") eq $lastguess){
  106. return -1; #date beyond this machine's integer limit
  107. }
  108. $lastguess = $thisguess;
  109. }
  110. @gfake = gmtime($guess-1); #still being sceptic
  111. if ("@gfake" eq $lastguess){
  112. return -1; #date beyond this machine's integer limit
  113. }
  114. $g[3]--;
  115. $guess -= $g[0] * $SEC + $g[1] * $MIN + $g[2] * $HR + $g[3] * $DAY;
  116. $cheat{$ym} = $guess;
  117. }
  118. 1;