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.

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