mktime.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* This is adapted from glibc */
  2. /* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc. */
  3. /* Assume that leap seconds are possible, unless told otherwise.
  4. If the host has a `zic' command with a -L leapsecondfilename' option,
  5. then it supports leap seconds; otherwise it probably doesn't. */
  6. #ifndef LEAP_SECONDS_POSSIBLE
  7. #define LEAP_SECONDS_POSSIBLE 1
  8. #endif
  9. #include <sys/types.h> /* Some systems define `time_t' here. */
  10. #include <time.h>
  11. #if __STDC__ || __GNU_LIBRARY__ || STDC_HEADERS
  12. #include <limits.h>
  13. #endif
  14. #if DEBUG
  15. #include <stdio.h>
  16. #if __STDC__ || __GNU_LIBRARY__ || STDC_HEADERS
  17. #include <stdlib.h>
  18. #endif
  19. /* Make it work even if the system's libc has its own mktime routine. */
  20. #define mktime my_mktime
  21. #endif /* DEBUG */
  22. #ifndef __P
  23. #if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
  24. #define __P(args) args
  25. #else
  26. #define __P(args) ()
  27. #endif /* GCC. */
  28. #endif /* Not __P. */
  29. #ifndef CHAR_BIT
  30. #define CHAR_BIT 8
  31. #endif
  32. #ifndef INT_MIN
  33. #define INT_MIN (~0 << (sizeof (int) * CHAR_BIT - 1))
  34. #endif
  35. #ifndef INT_MAX
  36. #define INT_MAX (~0 - INT_MIN)
  37. #endif
  38. #ifndef TIME_T_MIN
  39. #define TIME_T_MIN (0 < (time_t) -1 ? (time_t) 0 \
  40. : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
  41. #endif
  42. #ifndef TIME_T_MAX
  43. #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
  44. #endif
  45. #define TM_YEAR_BASE 1900
  46. #define EPOCH_YEAR 1970
  47. #ifndef __isleap
  48. /* Nonzero if YEAR is a leap year (every 4 years,
  49. except every 100th isn't, and every 400th is). */
  50. #define __isleap(year) \
  51. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  52. #endif
  53. /* How many days come before each month (0-12). */
  54. const unsigned short int __mon_yday[2][13] = {
  55. /* Normal years. */
  56. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
  57. /* Leap years. */
  58. {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
  59. };
  60. static time_t ydhms_tm_diff
  61. __P((int, int, int, int, int, const struct tm *));
  62. time_t __mktime_internal
  63. __P((struct tm *, struct tm * (*)(const time_t *, struct tm *), time_t *));
  64. /* Yield the difference between (YEAR-YDAY HOUR:MIN:SEC) and (*TP),
  65. measured in seconds, ignoring leap seconds.
  66. YEAR uses the same numbering as TM->tm_year.
  67. All values are in range, except possibly YEAR.
  68. If overflow occurs, yield the low order bits of the correct answer. */
  69. static time_t ydhms_tm_diff(year, yday, hour, min, sec, tp)
  70. int year, yday, hour, min, sec;
  71. const struct tm *tp;
  72. {
  73. /* Compute intervening leap days correctly even if year is negative.
  74. Take care to avoid int overflow. time_t overflow is OK, since
  75. only the low order bits of the correct time_t answer are needed.
  76. Don't convert to time_t until after all divisions are done, since
  77. time_t might be unsigned. */
  78. int a4 = (year >> 2) + (TM_YEAR_BASE >> 2) - !(year & 3);
  79. int b4 = (tp->tm_year >> 2) + (TM_YEAR_BASE >> 2) - !(tp->tm_year & 3);
  80. int a100 = a4 / 25 - (a4 % 25 < 0);
  81. int b100 = b4 / 25 - (b4 % 25 < 0);
  82. int a400 = a100 >> 2;
  83. int b400 = b100 >> 2;
  84. int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
  85. time_t years = year - (time_t) tp->tm_year;
  86. time_t days = (365 * years + intervening_leap_days
  87. + (yday - tp->tm_yday));
  88. return (60 * (60 * (24 * days + (hour - tp->tm_hour))
  89. + (min - tp->tm_min))
  90. + (sec - tp->tm_sec));
  91. }
  92. static time_t localtime_offset;
  93. /* Convert *TP to a time_t value. */
  94. time_t mktime(tp)
  95. struct tm *tp;
  96. {
  97. #ifdef _LIBC
  98. /* POSIX.1 8.1.1 requires that whenever mktime() is called, the
  99. time zone names contained in the external variable `tzname' shall
  100. be set as if the tzset() function had been called. */
  101. __tzset();
  102. #endif
  103. return __mktime_internal(tp, localtime_r, &localtime_offset);
  104. }
  105. /* Convert *TP to a time_t value, inverting
  106. the monotonic and mostly-unit-linear conversion function CONVERT.
  107. Use *OFFSET to keep track of a guess at the offset of the result,
  108. compared to what the result would be for UTC without leap seconds.
  109. If *OFFSET's guess is correct, only one CONVERT call is needed. */
  110. time_t __mktime_internal(tp, convert, offset)
  111. struct tm *tp;
  112. struct tm *(*convert) __P((const time_t *, struct tm *));
  113. time_t *offset;
  114. {
  115. time_t t, dt, t0;
  116. struct tm tm;
  117. /* The maximum number of probes (calls to CONVERT) should be enough
  118. to handle any combinations of time zone rule changes, solar time,
  119. and leap seconds. Posix.1 prohibits leap seconds, but some hosts
  120. have them anyway. */
  121. int remaining_probes = 4;
  122. /* Time requested. Copy it in case CONVERT modifies *TP; this can
  123. occur if TP is localtime's returned value and CONVERT is localtime. */
  124. int sec = tp->tm_sec;
  125. int min = tp->tm_min;
  126. int hour = tp->tm_hour;
  127. int mday = tp->tm_mday;
  128. int mon = tp->tm_mon;
  129. int year_requested = tp->tm_year;
  130. int isdst = tp->tm_isdst;
  131. /* Ensure that mon is in range, and set year accordingly. */
  132. int mon_remainder = mon % 12;
  133. int negative_mon_remainder = mon_remainder < 0;
  134. int mon_years = mon / 12 - negative_mon_remainder;
  135. int year = year_requested + mon_years;
  136. /* The other values need not be in range:
  137. the remaining code handles minor overflows correctly,
  138. assuming int and time_t arithmetic wraps around.
  139. Major overflows are caught at the end. */
  140. /* Calculate day of year from year, month, and day of month.
  141. The result need not be in range. */
  142. int yday = ((__mon_yday[__isleap(year + TM_YEAR_BASE)]
  143. [mon_remainder + 12 * negative_mon_remainder])
  144. + mday - 1);
  145. #if LEAP_SECONDS_POSSIBLE
  146. /* Handle out-of-range seconds specially,
  147. since ydhms_tm_diff assumes every minute has 60 seconds. */
  148. int sec_requested = sec;
  149. if (sec < 0)
  150. sec = 0;
  151. if (59 < sec)
  152. sec = 59;
  153. #endif
  154. /* Invert CONVERT by probing. First assume the same offset as last time.
  155. Then repeatedly use the error to improve the guess. */
  156. tm.tm_year = EPOCH_YEAR - TM_YEAR_BASE;
  157. tm.tm_yday = tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  158. t0 = ydhms_tm_diff(year, yday, hour, min, sec, &tm);
  159. for (t = t0 + *offset;
  160. (dt =
  161. ydhms_tm_diff(year, yday, hour, min, sec, (*convert) (&t, &tm)));
  162. t += dt)
  163. if (--remaining_probes == 0)
  164. return -1;
  165. /* Check whether tm.tm_isdst has the requested value, if any. */
  166. if (0 <= isdst && 0 <= tm.tm_isdst) {
  167. int dst_diff = (isdst != 0) - (tm.tm_isdst != 0);
  168. if (dst_diff) {
  169. /* Move two hours in the direction indicated by the disagreement,
  170. probe some more, and switch to a new time if found.
  171. The largest known fallback due to daylight savings is two hours:
  172. once, in Newfoundland, 1988-10-30 02:00 -> 00:00. */
  173. time_t ot = t - 2 * 60 * 60 * dst_diff;
  174. while (--remaining_probes != 0) {
  175. struct tm otm;
  176. if (!(dt = ydhms_tm_diff(year, yday, hour, min, sec,
  177. (*convert) (&ot, &otm)))) {
  178. t = ot;
  179. tm = otm;
  180. break;
  181. }
  182. if ((ot += dt) == t)
  183. break; /* Avoid a redundant probe. */
  184. }
  185. }
  186. }
  187. *offset = t - t0;
  188. #if LEAP_SECONDS_POSSIBLE
  189. if (sec_requested != tm.tm_sec) {
  190. /* Adjust time to reflect the tm_sec requested, not the normalized value.
  191. Also, repair any damage from a false match due to a leap second. */
  192. t += sec_requested - sec + (sec == 0 && tm.tm_sec == 60);
  193. (*convert) (&t, &tm);
  194. }
  195. #endif
  196. #if 0
  197. if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3) {
  198. /* time_t isn't large enough to rule out overflows in ydhms_tm_diff,
  199. so check for major overflows. A gross check suffices,
  200. since if t has overflowed, it is off by a multiple of
  201. TIME_T_MAX - TIME_T_MIN + 1. So ignore any component of
  202. the difference that is bounded by a small value. */
  203. double dyear = (double) year_requested + mon_years - tm.tm_year;
  204. double dday = 366 * dyear + mday;
  205. double dsec = 60 * (60 * (24 * dday + hour) + min) + sec_requested;
  206. if (TIME_T_MAX / 3 - TIME_T_MIN / 3 < (dsec < 0 ? -dsec : dsec))
  207. return -1;
  208. }
  209. #endif
  210. *tp = tm;
  211. return t;
  212. }