mktime.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. extern const unsigned short int __mon_yday[2][13];
  54. static time_t ydhms_tm_diff
  55. __P((int, int, int, int, int, const struct tm *));
  56. time_t __mktime_internal
  57. __P((struct tm *, struct tm * (*)(const time_t *, struct tm *), time_t *));
  58. /* Yield the difference between (YEAR-YDAY HOUR:MIN:SEC) and (*TP),
  59. measured in seconds, ignoring leap seconds.
  60. YEAR uses the same numbering as TM->tm_year.
  61. All values are in range, except possibly YEAR.
  62. If overflow occurs, yield the low order bits of the correct answer. */
  63. static time_t ydhms_tm_diff(year, yday, hour, min, sec, tp)
  64. int year, yday, hour, min, sec;
  65. const struct tm *tp;
  66. {
  67. /* Compute intervening leap days correctly even if year is negative.
  68. Take care to avoid int overflow. time_t overflow is OK, since
  69. only the low order bits of the correct time_t answer are needed.
  70. Don't convert to time_t until after all divisions are done, since
  71. time_t might be unsigned. */
  72. int a4 = (year >> 2) + (TM_YEAR_BASE >> 2) - !(year & 3);
  73. int b4 = (tp->tm_year >> 2) + (TM_YEAR_BASE >> 2) - !(tp->tm_year & 3);
  74. int a100 = a4 / 25 - (a4 % 25 < 0);
  75. int b100 = b4 / 25 - (b4 % 25 < 0);
  76. int a400 = a100 >> 2;
  77. int b400 = b100 >> 2;
  78. int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
  79. time_t years = year - (time_t) tp->tm_year;
  80. time_t days = (365 * years + intervening_leap_days
  81. + (yday - tp->tm_yday));
  82. return (60 * (60 * (24 * days + (hour - tp->tm_hour))
  83. + (min - tp->tm_min))
  84. + (sec - tp->tm_sec));
  85. }
  86. /* This structure contains all the information about a
  87. timezone given in the POSIX standard TZ envariable. */
  88. typedef struct
  89. {
  90. const char *name;
  91. /* When to change. */
  92. enum { J0, J1, M } type; /* Interpretation of: */
  93. unsigned short int m, n, d; /* Month, week, day. */
  94. unsigned int secs; /* Time of day. */
  95. long int offset; /* Seconds east of GMT (west if < 0). */
  96. /* We cache the computed time of change for a
  97. given year so we don't have to recompute it. */
  98. time_t change; /* When to change to this zone. */
  99. int computed_for; /* Year above is computed for. */
  100. } tz_rule;
  101. /* tz_rules[0] is standard, tz_rules[1] is daylight. */
  102. static tz_rule tz_rules[2];
  103. /* Warning -- this function is a stub andd always does UTC
  104. * no matter what it is given */
  105. void tzset (void)
  106. {
  107. tz_rules[0].name = tz_rules[1].name = "UTC";
  108. tz_rules[0].type = tz_rules[1].type = J0;
  109. tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0;
  110. tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0;
  111. tz_rules[0].secs = tz_rules[1].secs = 0;
  112. tz_rules[0].offset = tz_rules[1].offset = 0L;
  113. tz_rules[0].change = tz_rules[1].change = (time_t) -1;
  114. tz_rules[0].computed_for = tz_rules[1].computed_for = 0;
  115. }
  116. static time_t localtime_offset;
  117. /* Convert *TP to a time_t value. */
  118. time_t mktime(tp)
  119. struct tm *tp;
  120. {
  121. #ifdef _LIBC
  122. /* POSIX.1 8.1.1 requires that whenever mktime() is called, the
  123. time zone names contained in the external variable `tzname' shall
  124. be set as if the tzset() function had been called. */
  125. tzset();
  126. #endif
  127. return __mktime_internal(tp, localtime_r, &localtime_offset);
  128. }
  129. /* Convert *TP to a time_t value, inverting
  130. the monotonic and mostly-unit-linear conversion function CONVERT.
  131. Use *OFFSET to keep track of a guess at the offset of the result,
  132. compared to what the result would be for UTC without leap seconds.
  133. If *OFFSET's guess is correct, only one CONVERT call is needed. */
  134. time_t __mktime_internal(tp, convert, offset)
  135. struct tm *tp;
  136. struct tm *(*convert) __P((const time_t *, struct tm *));
  137. time_t *offset;
  138. {
  139. time_t t, dt, t0;
  140. struct tm tm;
  141. /* The maximum number of probes (calls to CONVERT) should be enough
  142. to handle any combinations of time zone rule changes, solar time,
  143. and leap seconds. Posix.1 prohibits leap seconds, but some hosts
  144. have them anyway. */
  145. int remaining_probes = 4;
  146. /* Time requested. Copy it in case CONVERT modifies *TP; this can
  147. occur if TP is localtime's returned value and CONVERT is localtime. */
  148. int sec = tp->tm_sec;
  149. int min = tp->tm_min;
  150. int hour = tp->tm_hour;
  151. int mday = tp->tm_mday;
  152. int mon = tp->tm_mon;
  153. int year_requested = tp->tm_year;
  154. int isdst = tp->tm_isdst;
  155. /* Ensure that mon is in range, and set year accordingly. */
  156. int mon_remainder = mon % 12;
  157. int negative_mon_remainder = mon_remainder < 0;
  158. int mon_years = mon / 12 - negative_mon_remainder;
  159. int year = year_requested + mon_years;
  160. /* The other values need not be in range:
  161. the remaining code handles minor overflows correctly,
  162. assuming int and time_t arithmetic wraps around.
  163. Major overflows are caught at the end. */
  164. /* Calculate day of year from year, month, and day of month.
  165. The result need not be in range. */
  166. int yday = ((__mon_yday[__isleap(year + TM_YEAR_BASE)]
  167. [mon_remainder + 12 * negative_mon_remainder])
  168. + mday - 1);
  169. #if LEAP_SECONDS_POSSIBLE
  170. /* Handle out-of-range seconds specially,
  171. since ydhms_tm_diff assumes every minute has 60 seconds. */
  172. int sec_requested = sec;
  173. if (sec < 0)
  174. sec = 0;
  175. if (59 < sec)
  176. sec = 59;
  177. #endif
  178. /* Invert CONVERT by probing. First assume the same offset as last time.
  179. Then repeatedly use the error to improve the guess. */
  180. tm.tm_year = EPOCH_YEAR - TM_YEAR_BASE;
  181. tm.tm_yday = tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  182. t0 = ydhms_tm_diff(year, yday, hour, min, sec, &tm);
  183. for (t = t0 + *offset;
  184. (dt =
  185. ydhms_tm_diff(year, yday, hour, min, sec, (*convert) (&t, &tm)));
  186. t += dt)
  187. if (--remaining_probes == 0)
  188. return -1;
  189. /* Check whether tm.tm_isdst has the requested value, if any. */
  190. if (0 <= isdst && 0 <= tm.tm_isdst) {
  191. int dst_diff = (isdst != 0) - (tm.tm_isdst != 0);
  192. if (dst_diff) {
  193. /* Move two hours in the direction indicated by the disagreement,
  194. probe some more, and switch to a new time if found.
  195. The largest known fallback due to daylight savings is two hours:
  196. once, in Newfoundland, 1988-10-30 02:00 -> 00:00. */
  197. time_t ot = t - 2 * 60 * 60 * dst_diff;
  198. while (--remaining_probes != 0) {
  199. struct tm otm;
  200. if (!(dt = ydhms_tm_diff(year, yday, hour, min, sec,
  201. (*convert) (&ot, &otm)))) {
  202. t = ot;
  203. tm = otm;
  204. break;
  205. }
  206. if ((ot += dt) == t)
  207. break; /* Avoid a redundant probe. */
  208. }
  209. }
  210. }
  211. *offset = t - t0;
  212. #if LEAP_SECONDS_POSSIBLE
  213. if (sec_requested != tm.tm_sec) {
  214. /* Adjust time to reflect the tm_sec requested, not the normalized value.
  215. Also, repair any damage from a false match due to a leap second. */
  216. t += sec_requested - sec + (sec == 0 && tm.tm_sec == 60);
  217. (*convert) (&t, &tm);
  218. }
  219. #endif
  220. #if 0
  221. if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3) {
  222. /* time_t isn't large enough to rule out overflows in ydhms_tm_diff,
  223. so check for major overflows. A gross check suffices,
  224. since if t has overflowed, it is off by a multiple of
  225. TIME_T_MAX - TIME_T_MIN + 1. So ignore any component of
  226. the difference that is bounded by a small value. */
  227. double dyear = (double) year_requested + mon_years - tm.tm_year;
  228. double dday = 366 * dyear + mday;
  229. double dsec = 60 * (60 * (24 * dday + hour) + min) + sec_requested;
  230. if (TIME_T_MAX / 3 - TIME_T_MIN / 3 < (dsec < 0 ? -dsec : dsec))
  231. return -1;
  232. }
  233. #endif
  234. *tp = tm;
  235. return t;
  236. }