mktime.c 9.0 KB

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