home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _bf69e68bd6294bf1305e2e8c4da61ddb < prev    next >
Text File  |  2000-03-23  |  7KB  |  144 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Time::Local - efficiently compute time from local and GMT time</TITLE>
  5. <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> Time::Local - efficiently compute time from local and GMT time</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  24.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  25.     <LI><A HREF="#implementation">IMPLEMENTATION</A></LI>
  26.     <LI><A HREF="#bugs">BUGS</A></LI>
  27. </UL>
  28. <!-- INDEX END -->
  29.  
  30. <HR>
  31. <P>
  32. <H1><A NAME="name">NAME</A></H1>
  33. <P>Time::Local - efficiently compute time from local and GMT time</P>
  34. <P>
  35. <HR>
  36. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  37. <UL>
  38. <LI>Linux</LI>
  39. <LI>Solaris</LI>
  40. <LI>Windows</LI>
  41. </UL>
  42. <HR>
  43. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  44. <PRE>
  45.     $time = timelocal($sec,$min,$hours,$mday,$mon,$year);
  46.     $time = timegm($sec,$min,$hours,$mday,$mon,$year);</PRE>
  47. <P>
  48. <HR>
  49. <H1><A NAME="description">DESCRIPTION</A></H1>
  50. <P>These routines are the inverse of built-in perl fuctions <A HREF="../../lib/Pod/perlfunc.html#item_localtime"><CODE>localtime()</CODE></A>
  51. and gmtime().  They accept a date as a six-element array, and return
  52. the corresponding <A HREF="../../lib/Pod/perlfunc.html#item_time"><CODE>time(2)</CODE></A> value in seconds since the Epoch (Midnight,
  53. January 1, 1970).  This value can be positive or negative.</P>
  54. <P>It is worth drawing particular attention to the expected ranges for
  55. the values provided.  While the day of the month is expected to be in
  56. the range 1..31, the month should be in the range 0..11.  
  57. This is consistent with the values returned from <A HREF="../../lib/Pod/perlfunc.html#item_localtime"><CODE>localtime()</CODE></A> and gmtime().</P>
  58. <P>The <CODE>timelocal()</CODE> and <CODE>timegm()</CODE> functions perform range checking on the
  59. input $sec, $min, $hours, $mday, and $mon values by default.  If you'd
  60. rather they didn't, you can explicitly import the <CODE>timelocal_nocheck()</CODE>
  61. and <CODE>timegm_nocheck()</CODE> functions.</P>
  62. <PRE>
  63.         use Time::Local 'timelocal_nocheck';</PRE>
  64. <PRE>
  65.         {
  66.             # The 365th day of 1999
  67.             print scalar localtime timelocal_nocheck 0,0,0,365,0,99;</PRE>
  68. <PRE>
  69.             # The twenty thousandth day since 1970
  70.             print scalar localtime timelocal_nocheck 0,0,0,20000,0,70;</PRE>
  71. <PRE>
  72.             # And even the 10,000,000th second since 1999!
  73.             print scalar localtime timelocal_nocheck 10000000,0,0,1,0,99;
  74.         }</PRE>
  75. <P>Your mileage may vary when trying these with minutes and hours,
  76. and it doesn't work at all for months.</P>
  77. <P>Strictly speaking, the year should also be specified in a form consistent
  78. with localtime(), i.e. the offset from 1900.
  79. In order to make the interpretation of the year easier for humans,
  80. however, who are more accustomed to seeing years as two-digit or four-digit
  81. values, the following conventions are followed:</P>
  82. <UL>
  83. <LI>
  84. Years greater than 999 are interpreted as being the actual year,
  85. rather than the offset from 1900.  Thus, 1963 would indicate the year
  86. Martin Luther King won the Nobel prize, not the year 2863.
  87. <P></P>
  88. <LI>
  89. Years in the range 100..999 are interpreted as offset from 1900, 
  90. so that 112 indicates 2012.  This rule also applies to years less than zero
  91. (but see note below regarding date range).
  92. <P></P>
  93. <LI>
  94. Years in the range 0..99 are interpreted as shorthand for years in the
  95. rolling ``current century,'' defined as 50 years on either side of the current
  96. year.  Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045,
  97. but 55 would refer to 1955.  Twenty years from now, 55 would instead refer
  98. to 2055.  This is messy, but matches the way people currently think about
  99. two digit dates.  Whenever possible, use an absolute four digit year instead.
  100. <P></P></UL>
  101. <P>The scheme above allows interpretation of a wide range of dates, particularly
  102. if 4-digit years are used.</P>
  103. <P>Please note, however, that the range of dates that can be actually be handled
  104. depends on the size of an integer (time_t) on a given platform.  
  105. Currently, this is 32 bits for most systems, yielding an approximate range 
  106. from Dec 1901 to Jan 2038.</P>
  107. <P>Both <CODE>timelocal()</CODE> and <CODE>timegm()</CODE> croak if given dates outside the supported
  108. range.</P>
  109. <P>
  110. <HR>
  111. <H1><A NAME="implementation">IMPLEMENTATION</A></H1>
  112. <P>These routines are quite efficient and yet are always guaranteed to agree
  113. with <A HREF="../../lib/Pod/perlfunc.html#item_localtime"><CODE>localtime()</CODE></A> and gmtime().  We manage this by caching the start times
  114. of any months we've seen before.  If we know the start time of the month,
  115. we can always calculate any time within the month.  The start times
  116. themselves are guessed by successive approximation starting at the
  117. current time, since most dates seen in practice are close to the
  118. current date.  Unlike algorithms that do a binary search (calling gmtime
  119. once for each bit of the time value, resulting in 32 calls), this algorithm
  120. calls it at most 6 times, and usually only once or twice.  If you hit
  121. the month cache, of course, it doesn't call it at all.</P>
  122. <P><CODE>timelocal()</CODE> is implemented using the same cache.  We just assume that we're
  123. translating a GMT time, and then fudge it when we're done for the timezone
  124. and daylight savings arguments.  Note that the timezone is evaluated for
  125. each date because countries occasionally change their official timezones.
  126. Assuming that <A HREF="../../lib/Pod/perlfunc.html#item_localtime"><CODE>localtime()</CODE></A> corrects for these changes, this routine will
  127. also be correct.  The daylight savings offset is currently assumed 
  128. to be one hour.</P>
  129. <P>
  130. <HR>
  131. <H1><A NAME="bugs">BUGS</A></H1>
  132. <P>The whole scheme for interpreting two-digit years can be considered a bug.</P>
  133. <P>Note that the cache currently handles only years from 1900 through 2155.</P>
  134. <P>The proclivity to <CODE>croak()</CODE> is probably a bug.</P>
  135. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  136. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  137. <STRONG><P CLASS=block> Time::Local - efficiently compute time from local and GMT time</P></STRONG>
  138. </TD></TR>
  139. </TABLE>
  140.  
  141. </BODY>
  142.  
  143. </HTML>
  144.