home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5344 < prev    next >
Encoding:
Text File  |  1992-08-17  |  3.8 KB  |  104 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!wupost!sdd.hp.com!mips!darwin.sura.net!Sirius.dfn.de!fauern!fauna!nessy!pruy
  3. From: pruy@nessy.informatik.uni-erlangen.de (Rainer Pruy)
  4. Subject: Re: help with timelocal.pl
  5. References: <1992Aug14.190949.10466@rock.concert.net>
  6. Message-ID: <Bt4yFx.8M2@immd4.informatik.uni-erlangen.de>
  7. Sender: news@immd4.informatik.uni-erlangen.de
  8. Organization: CSD., University of Erlangen
  9. Date: Mon, 17 Aug 1992 16:14:21 GMT
  10. Lines: 92
  11.  
  12. In <1992Aug14.190949.10466@rock.concert.net> cole@concert.net (Derrick C. Cole) writes:
  13.  
  14. > Greetings, again!
  15. > Does anyone have a good subroutine for computing the difference in seconds
  16. > between two time stamps?  I'm trying to use timelocal() in mine, but it never
  17. > returns:
  18. > require "/usr/local/lib/perl/timelocal.pl";
  19. > chop($year = `date +\"\%y\"`);
  20. > [perl code deleted]
  21. > I'm doing something wrong, but can't figure out what it is (don't use packages
  22. > very often, if that's it...)  Can anyone improve upon/fix this subroutine?
  23.  
  24. No, not you are doing something wrong, there is a bug in timelocal.pl.
  25.  
  26. gmlocal() and timelocal() will fail for any date before (19)70
  27. due to a bug in &timelocal'cheat.
  28.  
  29. My version of cheat looked like:
  30.  
  31. :00:sub cheat {
  32. :01:    $year = $_[5];
  33. :02:    $month = $_[4];
  34. :03:    $guess = $^T;
  35. :04:    @g = gmtime($guess);
  36. :05:    while ($diff = $year - $g[5]) {
  37. :06:        $guess += $diff * (364 * $DAYS);
  38. :07:        @g = gmtime($guess);
  39. :08:    }
  40. :09:    while ($diff = $month - $g[4]) {
  41. :10:        $guess += $diff * (28 * $DAYS);
  42. :11:        @g = gmtime($guess);
  43. :12:    }
  44. :13:    $g[3]--;
  45. :14:    $guess -= $g[0] * $SEC + $g[1] * $MIN + $g[2] * $HR + $g[3] * $DAYS;
  46. :15:    $cheat{$ym} = $guess;
  47. :16:}
  48.  
  49. This bug shows up as soon as your system interpretes dates 0-69 to be 2000-2069
  50. while cheat assumes them to be 1900-1969.
  51.  
  52. On my SS2 (SunOS 4.1.2) the loop at line 05 gets stuck at a difference of 1 year.
  53.  
  54. I fixed it the following way:
  55.  
  56. *** timelocal.pl.orig    Mon Aug 17 17:56:09 1992
  57. --- timelocal.pl    Thu Jul  2 23:47:42 1992
  58. ***************
  59. *** 35,40 ****
  60. --- 35,41 ----
  61.       $MIN = 60 * $SEC;
  62.       $HR = 60 * $MIN;
  63.       $DAYS = 24 * $HR;
  64. +     $YearFix = ((gmtime(946684800))[5] == 100) ? 100 : 0;
  65.   }
  66.   
  67.   sub timegm {
  68. ***************
  69. *** 61,67 ****
  70. --- 62,69 ----
  71.       $month = $_[4];
  72.       $guess = $^T;
  73.       @g = gmtime($guess);
  74. +     $year += $YearFix if $year < $epoch[5];
  75.       while ($diff = $year - $g[5]) {
  76.         $guess += $diff * (364 * $DAYS);
  77.         @g = gmtime($guess);
  78.       }
  79.  
  80. line 38 (Hunk 1) determines whether &chat and gmtime() dissagree on year
  81. interpretation and line 64 (Hunk 2) fixes the representation given.
  82.  
  83. > Any and all help greatly appreciated!
  84. > Derrick Cole
  85.  
  86. Hope this fixes your problem..
  87.  
  88. Rainer Pruy
  89.  
  90.  /----------------------------------------------------------------------------\
  91.  | Rainer Pruy           | email:                                             |
  92.  |                       |  DOMAIN: pruy@informatik.uni-erlangen.de           |
  93.  | Friedrich-Alexander   |  X400:   /C=de/A=dbp/P=uni-erlangen/OU=informatik/ |
  94.  | Universitaet          |          /S=Pruy/G=Rainer/                         |
  95.  |                       |  UUCP:   ...!uunet!fauern!pruy                     |
  96.  | CS Department IMMD IV |----------------------------------------------------|
  97.  | (operating systems)   | phone:  +49 9131 85-7909                           |
  98.  |                       |----------------------------------------------------|
  99.  | Martensstrasse 1      | ================================================== |
  100.  |                       |//   Programming Department:                      \\|
  101.  | D-8520 Erlangen       |\\           Mistakes made while you wait         //|
  102.  | Germany               | ================================================== |
  103.  \----------------------------------------------------------------------------/
  104.