timezone.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. Copyright (c) 1990-2001 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2000-Apr-09 or later
  4. (the contents of which are also included in zip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /* Replacement time library functions, based on platform independent public
  9. * domain timezone code from ftp://elsie.nci.nih.gov/pub, with mktime and
  10. * mkgmtime from our own mktime.c in Zip.
  11. *
  12. * Contains: tzset()
  13. * __tzset()
  14. * gmtime()
  15. * localtime()
  16. * mktime()
  17. * mkgmtime()
  18. * GetPlatformLocalTimezone() [different versions]
  19. */
  20. /* HISTORY/CHANGES
  21. * 17 Jun 00, Paul Kienitz, added the PD-based tzset(), localtime(), and so on
  22. * to amiga/filedate.c, replacing GNU-based functions which had
  23. * replaced time_lib.c, both having been rejected for licensing
  24. * reasons. Support for timezone files and leap seconds was removed.
  25. *
  26. * 23 Aug 00, Paul Kienitz, split into separate timezone.c file, made platform
  27. * independent, copied in mktime() and mkgmtime() from Zip, renamed
  28. * locale_TZ as GetPlatformLocalTimezone(), for use as a generic
  29. * hook by other platforms.
  30. */
  31. #ifndef __timezone_c
  32. #define __timezone_c
  33. #include "zip.h"
  34. #include "timezone.h"
  35. #include <ctype.h>
  36. #include <errno.h>
  37. #ifdef IZTZ_DEFINESTDGLOBALS
  38. long timezone = 0;
  39. int daylight = 0;
  40. char *tzname[2];
  41. #endif
  42. #ifndef IZTZ_GETLOCALETZINFO
  43. # define IZTZ_GETLOCALETZINFO(ptzstruct, pgenrulefunct) (FALSE)
  44. #endif
  45. int real_timezone_is_set = FALSE; /* set by tzset() */
  46. #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
  47. #define TZDEFAULT "EST5EDT"
  48. #define SECSPERMIN 60
  49. #define MINSPERHOUR 60
  50. #define HOURSPERDAY 24
  51. #define DAYSPERWEEK 7
  52. #define DAYSPERNYEAR 365
  53. #define DAYSPERLYEAR 366
  54. #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
  55. #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
  56. #define MONSPERYEAR 12
  57. #define EPOCH_WDAY 4 /* Jan 1, 1970 was thursday */
  58. #define EPOCH_YEAR 1970
  59. #define TM_YEAR_BASE 1900
  60. #define FIRST_GOOD_YEAR ((time_t) -1 < (time_t) 1 ? EPOCH_YEAR-68 : EPOCH_YEAR)
  61. #define LAST_GOOD_YEAR (EPOCH_YEAR + ((time_t) -1 < (time_t) 1 ? 67 : 135))
  62. #define YDAYS(month, year) yr_days[leap(year)][month]
  63. /* Nonzero if `y' is a leap year, else zero. */
  64. #define leap(y) (((y) % 4 == 0 && (y) % 100 != 0) || (y) % 400 == 0)
  65. /* Number of leap years from EPOCH_YEAR to `y' (not including `y' itself). */
  66. #define _P4 ((EPOCH_YEAR / 4) * 4 + 1)
  67. #define _P100 ((EPOCH_YEAR / 100) * 100 + 1)
  68. #define _P400 ((EPOCH_YEAR / 400) * 400 + 1)
  69. #define nleap(y) (((y) - _P4) / 4 - ((y) - _P100) / 100 + ((y) - _P400) / 400)
  70. /* Length of month `m' (0 .. 11) */
  71. #define monthlen(m, y) (yr_days[0][(m)+1] - yr_days[0][m] + \
  72. ((m) == 1 && leap(y)))
  73. /* internal module-level constants */
  74. #ifndef IZ_MKTIME_ONLY
  75. static ZCONST char gmt[] = "GMT";
  76. static ZCONST int mon_lengths[2][MONSPERYEAR] = {
  77. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  78. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  79. };
  80. #endif /* !IZ_MKTIME_ONLY */
  81. static ZCONST int yr_days[2][MONSPERYEAR+1] = {
  82. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  83. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  84. };
  85. #ifndef IZ_MKTIME_ONLY
  86. static ZCONST int year_lengths[2] = {
  87. DAYSPERNYEAR, DAYSPERLYEAR
  88. };
  89. /* internal variables */
  90. static struct state statism;
  91. /* prototypes of static functions */
  92. static time_t transtime OF((ZCONST time_t janfirst, ZCONST int year,
  93. ZCONST struct rule * ZCONST rulep,
  94. ZCONST long offset));
  95. static void generate_transitions OF((register struct state * ZCONST sp,
  96. ZCONST struct rule * ZCONST start,
  97. ZCONST struct rule * ZCONST end));
  98. static ZCONST char *getzname OF((ZCONST char *strp));
  99. static ZCONST char *getnum OF((ZCONST char *strp, int * ZCONST nump,
  100. ZCONST int min, ZCONST int max));
  101. static ZCONST char *getsecs OF((ZCONST char *strp, long * ZCONST secsp));
  102. static ZCONST char *getoffset OF((ZCONST char *strp, long * ZCONST offsetp));
  103. static ZCONST char *getrule OF((ZCONST char *strp, struct rule * ZCONST rulep));
  104. static int Parse_TZ OF((ZCONST char *name, register struct state * ZCONST sp));
  105. static time_t transtime(janfirst, year, rulep, offset)
  106. ZCONST time_t janfirst;
  107. ZCONST int year;
  108. ZCONST struct rule * ZCONST rulep;
  109. ZCONST long offset;
  110. {
  111. register int leapyear;
  112. register time_t value;
  113. register int i;
  114. int d, m1, yy0, yy1, yy2, dow;
  115. value = 0;
  116. leapyear = leap(year);
  117. switch (rulep->r_type) {
  118. case JULIAN_DAY:
  119. /*
  120. ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
  121. ** years.
  122. ** In non-leap years, or if the day number is 59 or less, just
  123. ** add SECSPERDAY times the day number-1 to the time of
  124. ** January 1, midnight, to get the day.
  125. */
  126. value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
  127. if (leapyear && rulep->r_day >= 60)
  128. value += SECSPERDAY;
  129. break;
  130. case DAY_OF_YEAR:
  131. /*
  132. ** n - day of year.
  133. ** Just add SECSPERDAY times the day number to the time of
  134. ** January 1, midnight, to get the day.
  135. */
  136. value = janfirst + rulep->r_day * SECSPERDAY;
  137. break;
  138. case MONTH_NTH_DAY_OF_WEEK:
  139. /*
  140. ** Mm.n.d - nth "dth day" of month m.
  141. */
  142. value = janfirst;
  143. /*
  144. for (i = 0; i < rulep->r_mon - 1; ++i)
  145. value += mon_lengths[leapyear][i] * SECSPERDAY;
  146. */
  147. value += yr_days[leapyear][rulep->r_mon - 1] * SECSPERDAY;
  148. /*
  149. ** Use Zeller's Congruence to get day-of-week of first day of
  150. ** month.
  151. */
  152. m1 = (rulep->r_mon + 9) % 12 + 1;
  153. yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
  154. yy1 = yy0 / 100;
  155. yy2 = yy0 % 100;
  156. dow = ((26 * m1 - 2) / 10 +
  157. 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
  158. if (dow < 0)
  159. dow += DAYSPERWEEK;
  160. /*
  161. ** "dow" is the day-of-week of the first day of the month. Get
  162. ** the day-of-month (zero-origin) of the first "dow" day of the
  163. ** month.
  164. */
  165. d = rulep->r_day - dow;
  166. if (d < 0)
  167. d += DAYSPERWEEK;
  168. for (i = 1; i < rulep->r_week; ++i) {
  169. if (d + DAYSPERWEEK >= mon_lengths[leapyear][rulep->r_mon - 1])
  170. break;
  171. d += DAYSPERWEEK;
  172. }
  173. /*
  174. ** "d" is the day-of-month (zero-origin) of the day we want.
  175. */
  176. value += d * SECSPERDAY;
  177. break;
  178. }
  179. /*
  180. ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
  181. ** question. To get the Epoch-relative time of the specified local
  182. ** time on that day, add the transition time and the current offset
  183. ** from UTC.
  184. */
  185. return value + rulep->r_time + offset;
  186. }
  187. static void generate_transitions(sp, start, end)
  188. register struct state * ZCONST sp;
  189. ZCONST struct rule * ZCONST start;
  190. ZCONST struct rule * ZCONST end;
  191. {
  192. register int year;
  193. register time_t janfirst;
  194. time_t starttime;
  195. time_t endtime;
  196. long stdoffset = -sp->ttis[0].tt_gmtoff;
  197. long dstoffset = -sp->ttis[1].tt_gmtoff;
  198. register time_t * atp;
  199. register unsigned char * typep;
  200. /*
  201. ** Two transitions per year, from EPOCH_YEAR to LAST_GOOD_YEAR.
  202. */
  203. sp->timecnt = 2 * (LAST_GOOD_YEAR - EPOCH_YEAR + 1);
  204. atp = sp->ats;
  205. typep = sp->types;
  206. janfirst = 0;
  207. for (year = EPOCH_YEAR; year <= LAST_GOOD_YEAR; ++year) {
  208. starttime = transtime(janfirst, year, start, stdoffset);
  209. endtime = transtime(janfirst, year, end, dstoffset);
  210. if (starttime > endtime) {
  211. *atp++ = endtime;
  212. *typep++ = 0; /* DST ends */
  213. *atp++ = starttime;
  214. *typep++ = 1; /* DST begins */
  215. } else {
  216. *atp++ = starttime;
  217. *typep++ = 1; /* DST begins */
  218. *atp++ = endtime;
  219. *typep++ = 0; /* DST ends */
  220. }
  221. janfirst += year_lengths[leap(year)] * SECSPERDAY;
  222. }
  223. }
  224. static ZCONST char *getzname(strp)
  225. ZCONST char *strp;
  226. {
  227. register char c;
  228. while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' &&
  229. c != '+')
  230. ++strp;
  231. return strp;
  232. }
  233. static ZCONST char *getnum(strp, nump, min, max)
  234. ZCONST char *strp;
  235. int * ZCONST nump;
  236. ZCONST int min;
  237. ZCONST int max;
  238. {
  239. register char c;
  240. register int num;
  241. if (strp == NULL || !isdigit(c = *strp))
  242. return NULL;
  243. num = 0;
  244. do {
  245. num = num * 10 + (c - '0');
  246. if (num > max)
  247. return NULL; /* illegal value */
  248. c = *++strp;
  249. } while (isdigit(c));
  250. if (num < min)
  251. return NULL; /* illegal value */
  252. *nump = num;
  253. return strp;
  254. }
  255. static ZCONST char *getsecs(strp, secsp)
  256. ZCONST char *strp;
  257. long * ZCONST secsp;
  258. {
  259. int num;
  260. /*
  261. ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
  262. ** "M10.4.6/26", which does not conform to Posix,
  263. ** but which specifies the equivalent of
  264. ** ``02:00 on the first Sunday on or after 23 Oct''.
  265. */
  266. strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
  267. if (strp == NULL)
  268. return NULL;
  269. *secsp = num * (long) SECSPERHOUR;
  270. if (*strp == ':') {
  271. ++strp;
  272. strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
  273. if (strp == NULL)
  274. return NULL;
  275. *secsp += num * SECSPERMIN;
  276. if (*strp == ':') {
  277. ++strp;
  278. /* `SECSPERMIN' allows for leap seconds. */
  279. strp = getnum(strp, &num, 0, SECSPERMIN);
  280. if (strp == NULL)
  281. return NULL;
  282. *secsp += num;
  283. }
  284. }
  285. return strp;
  286. }
  287. static ZCONST char *getoffset(strp, offsetp)
  288. ZCONST char *strp;
  289. long * ZCONST offsetp;
  290. {
  291. register int neg = 0;
  292. if (*strp == '-') {
  293. neg = 1;
  294. ++strp;
  295. } else if (*strp == '+')
  296. ++strp;
  297. strp = getsecs(strp, offsetp);
  298. if (strp == NULL)
  299. return NULL; /* illegal time */
  300. if (neg)
  301. *offsetp = -*offsetp;
  302. return strp;
  303. }
  304. static ZCONST char *getrule(strp, rulep)
  305. ZCONST char *strp;
  306. struct rule * ZCONST rulep;
  307. {
  308. if (*strp == 'J') {
  309. /*
  310. ** Julian day.
  311. */
  312. rulep->r_type = JULIAN_DAY;
  313. ++strp;
  314. strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
  315. } else if (*strp == 'M') {
  316. /*
  317. ** Month, week, day.
  318. */
  319. rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
  320. ++strp;
  321. strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
  322. if (strp == NULL)
  323. return NULL;
  324. if (*strp++ != '.')
  325. return NULL;
  326. strp = getnum(strp, &rulep->r_week, 1, 5);
  327. if (strp == NULL)
  328. return NULL;
  329. if (*strp++ != '.')
  330. return NULL;
  331. strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
  332. } else if (isdigit(*strp)) {
  333. /*
  334. ** Day of year.
  335. */
  336. rulep->r_type = DAY_OF_YEAR;
  337. strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
  338. } else return NULL; /* invalid format */
  339. if (strp == NULL)
  340. return NULL;
  341. if (*strp == '/') {
  342. /*
  343. ** Time specified.
  344. */
  345. ++strp;
  346. strp = getsecs(strp, &rulep->r_time);
  347. } else
  348. rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
  349. return strp;
  350. }
  351. static int Parse_TZ(name, sp)
  352. ZCONST char *name;
  353. register struct state * ZCONST sp;
  354. {
  355. ZCONST char * stdname;
  356. ZCONST char * dstname;
  357. size_t stdlen;
  358. size_t dstlen;
  359. long stdoffset;
  360. long dstoffset;
  361. register char * cp;
  362. dstname = NULL;
  363. stdname = name;
  364. name = getzname(name);
  365. stdlen = name - stdname;
  366. if (stdlen < 3)
  367. return -1;
  368. if (*name == '\0')
  369. return -1;
  370. name = getoffset(name, &stdoffset);
  371. if (name == NULL)
  372. return -1;
  373. if (*name != '\0') {
  374. dstname = name;
  375. name = getzname(name);
  376. dstlen = name - dstname; /* length of DST zone name */
  377. if (dstlen < 3)
  378. return -1;
  379. if (*name != '\0' && *name != ',' && *name != ';') {
  380. name = getoffset(name, &dstoffset);
  381. if (name == NULL)
  382. return -1;
  383. } else
  384. dstoffset = stdoffset - SECSPERHOUR;
  385. if (*name == '\0')
  386. name = TZDEFRULESTRING;
  387. if (*name == ',' || *name == ';') {
  388. struct rule start;
  389. struct rule end;
  390. ++name;
  391. if ((name = getrule(name, &start)) == NULL)
  392. return -1;
  393. if (*name++ != ',')
  394. return -1;
  395. if ((name = getrule(name, &end)) == NULL)
  396. return -1;
  397. if (*name != '\0')
  398. return -1;
  399. sp->typecnt = 2; /* standard time and DST */
  400. sp->ttis[0].tt_gmtoff = -stdoffset;
  401. sp->ttis[0].tt_isdst = 0;
  402. sp->ttis[0].tt_abbrind = 0;
  403. sp->ttis[1].tt_gmtoff = -dstoffset;
  404. sp->ttis[1].tt_isdst = 1;
  405. sp->ttis[1].tt_abbrind = stdlen + 1;
  406. generate_transitions(sp, &start, &end);
  407. }
  408. } else {
  409. dstlen = 0;
  410. sp->typecnt = 1; /* only standard time */
  411. sp->timecnt = 0;
  412. sp->ttis[0].tt_gmtoff = -stdoffset;
  413. sp->ttis[0].tt_isdst = 0;
  414. sp->ttis[0].tt_abbrind = 0;
  415. }
  416. sp->charcnt = stdlen + 1;
  417. if (dstlen != 0)
  418. sp->charcnt += dstlen + 1;
  419. if ((size_t) sp->charcnt > sizeof(sp->chars))
  420. return -1;
  421. cp = sp->chars;
  422. (void) strncpy(cp, stdname, stdlen);
  423. cp += stdlen;
  424. *cp++ = '\0';
  425. if (dstlen != 0) {
  426. (void) strncpy(cp, dstname, dstlen);
  427. *(cp + dstlen) = '\0';
  428. }
  429. return 0;
  430. }
  431. void tzset()
  432. {
  433. char *TZstring;
  434. int dstfirst;
  435. static char *old_TZstring = NULL;
  436. TZstring = getenv("TZ"); /* read TZ envvar */
  437. if (old_TZstring && TZstring && !strcmp(old_TZstring, TZstring))
  438. /* do not repeatedly parse an unchanged TZ specification */
  439. return;
  440. if ((TZstring && TZstring[0] && Parse_TZ(TZstring, &statism) == 0)
  441. || IZTZ_GETLOCALETZINFO(&statism, generate_transitions)
  442. || Parse_TZ(gmt, &statism) == 0) {
  443. daylight = statism.typecnt > 1;
  444. dstfirst = daylight && statism.ttis[0].tt_isdst && !statism.ttis[1].tt_isdst;
  445. timezone = -statism.ttis[dstfirst].tt_gmtoff;
  446. tzname[0] = statism.chars + statism.ttis[dstfirst].tt_abbrind;
  447. tzname[1] = statism.chars + statism.ttis[!dstfirst].tt_abbrind;
  448. real_timezone_is_set = TRUE;
  449. if (TZstring) {
  450. if (old_TZstring)
  451. old_TZstring = realloc(old_TZstring, strlen(TZstring) + 1);
  452. else
  453. old_TZstring = malloc(strlen(TZstring) + 1);
  454. if (old_TZstring)
  455. strcpy(old_TZstring, TZstring);
  456. }
  457. } else {
  458. timezone = 0; /* default is GMT0 which means no offsets */
  459. daylight = 0; /* from local system time */
  460. real_timezone_is_set = FALSE;
  461. if (old_TZstring) {
  462. free(old_TZstring);
  463. old_TZstring = NULL;
  464. }
  465. }
  466. #ifdef IZTZ_SETLOCALTZINFO
  467. /* Some SAS/C library functions, e.g. stat(), call library */
  468. /* __tzset() themselves. So envvar TZ *must* exist in order to */
  469. /* to get the right offset from GMT. XXX TRY HARD to fix this! */
  470. set_TZ(timezone, daylight);
  471. #endif /* IZTZ_SETLOCALTZINFO */
  472. }
  473. /* XXX Does this also help SAS/C library work? */
  474. void __tzset()
  475. {
  476. if (!real_timezone_is_set) tzset();
  477. }
  478. static struct tm _tmbuf;
  479. struct tm *gmtime(when)
  480. ZCONST time_t *when;
  481. {
  482. long days = *when / SECSPERDAY;
  483. long secs = *when % SECSPERDAY;
  484. int isleap;
  485. memset(&_tmbuf, 0, sizeof(_tmbuf)); /* get any nonstandard fields */
  486. _tmbuf.tm_wday = (days + EPOCH_WDAY) % 7;
  487. _tmbuf.tm_year = EPOCH_YEAR - TM_YEAR_BASE;
  488. isleap = leap(_tmbuf.tm_year + TM_YEAR_BASE);
  489. while (days >= year_lengths[isleap]) {
  490. days -= year_lengths[isleap];
  491. _tmbuf.tm_year++;
  492. isleap = leap(_tmbuf.tm_year + TM_YEAR_BASE);
  493. }
  494. _tmbuf.tm_mon = 0;
  495. _tmbuf.tm_yday = days;
  496. while (days >= mon_lengths[isleap][_tmbuf.tm_mon])
  497. days -= mon_lengths[isleap][_tmbuf.tm_mon++];
  498. _tmbuf.tm_mday = days + 1;
  499. _tmbuf.tm_isdst = 0;
  500. _tmbuf.tm_sec = secs % SECSPERMIN;
  501. _tmbuf.tm_min = (secs / SECSPERMIN) % SECSPERMIN;
  502. _tmbuf.tm_hour = secs / SECSPERHOUR;
  503. return &_tmbuf;
  504. }
  505. struct tm *localtime(when)
  506. ZCONST time_t *when;
  507. {
  508. time_t localwhen = *when;
  509. int timetype;
  510. struct tm *ret;
  511. __tzset();
  512. if (statism.timecnt == 0 || localwhen < statism.ats[0])
  513. timetype = statism.ttis[0].tt_isdst && statism.typecnt > 1 &&
  514. !statism.ttis[1].tt_isdst;
  515. else {
  516. for (timetype = 1; timetype < statism.timecnt; ++timetype)
  517. if (localwhen < statism.ats[timetype])
  518. break;
  519. timetype = statism.types[timetype - 1];
  520. }
  521. localwhen += statism.ttis[timetype].tt_gmtoff;
  522. ret = gmtime(&localwhen);
  523. ret->tm_isdst = statism.ttis[timetype].tt_isdst;
  524. return ret;
  525. }
  526. #ifdef NEED__ISINDST
  527. int _isindst(tb)
  528. struct tm *tb;
  529. {
  530. time_t localt; /* time_t equivalent of given tm struct */
  531. time_t univt; /* assumed UTC value of given time */
  532. long tzoffset_adj; /* timezone-adjustment `remainder' */
  533. int bailout_cnt; /* counter of tries for tz correction */
  534. int timetype;
  535. __tzset();
  536. /* when DST is unsupported in current timezone, DST is always off */
  537. if (statism.typecnt <= 1) return FALSE;
  538. localt = mkgmtime(tb);
  539. if (localt == (time_t)-1)
  540. /* specified time is out-of-range, default to FALSE */
  541. return FALSE;
  542. univt = localt - statism.ttis[0].tt_gmtoff;
  543. bailout_cnt = 3;
  544. do {
  545. if (statism.timecnt == 0 || univt < statism.ats[0])
  546. timetype = statism.ttis[0].tt_isdst && statism.typecnt > 1 &&
  547. !statism.ttis[1].tt_isdst;
  548. else {
  549. for (timetype = 1; timetype < statism.timecnt; ++timetype)
  550. if (univt < statism.ats[timetype])
  551. break;
  552. timetype = statism.types[timetype - 1];
  553. }
  554. if ((tzoffset_adj = localt - univt - statism.ttis[timetype].tt_gmtoff)
  555. == 0L)
  556. break;
  557. univt += tzoffset_adj;
  558. } while (--bailout_cnt > 0);
  559. /* return TRUE when DST is active at given time */
  560. return (statism.ttis[timetype].tt_isdst);
  561. }
  562. #endif /* NEED__ISINDST */
  563. #endif /* !IZ_MKTIME_ONLY */
  564. /* Return the equivalent in seconds past 12:00:00 a.m. Jan 1, 1970 GMT
  565. of the local time and date in the exploded time structure `tm',
  566. adjust out of range fields in `tm' and set `tm->tm_yday', `tm->tm_wday'.
  567. If `tm->tm_isdst < 0' was passed to mktime(), the correct setting of
  568. tm_isdst is determined and returned. Otherwise, mktime() assumes this
  569. field as valid; its information is used when converting local time
  570. to UTC.
  571. Return -1 if time in `tm' cannot be represented as time_t value. */
  572. time_t mktime(tm)
  573. struct tm *tm;
  574. {
  575. struct tm *ltm; /* Local time. */
  576. time_t loctime; /* The time_t value of local time. */
  577. time_t then; /* The time to return. */
  578. long tzoffset_adj; /* timezone-adjustment `remainder' */
  579. int bailout_cnt; /* counter of tries for tz correction */
  580. int save_isdst; /* Copy of the tm->isdst input value */
  581. save_isdst = tm->tm_isdst;
  582. loctime = mkgmtime(tm);
  583. if (loctime == -1) {
  584. tm->tm_isdst = save_isdst;
  585. return (time_t)-1;
  586. }
  587. /* Correct for the timezone and any daylight savings time.
  588. The correction is verified and repeated when not correct, to
  589. take into account the rare case that a change to or from daylight
  590. savings time occurs between when it is the time in `tm' locally
  591. and when it is that time in Greenwich. After the second correction,
  592. the "timezone & daylight" offset should be correct in all cases. To
  593. be sure, we allow a third try, but then the loop is stopped. */
  594. bailout_cnt = 3;
  595. then = loctime;
  596. do {
  597. ltm = localtime(&then);
  598. if (ltm == (struct tm *)NULL ||
  599. (tzoffset_adj = loctime - mkgmtime(ltm)) == 0L)
  600. break;
  601. then += tzoffset_adj;
  602. } while (--bailout_cnt > 0);
  603. if (ltm == (struct tm *)NULL || tzoffset_adj != 0L) {
  604. /* Signal failure if timezone adjustment did not converge. */
  605. tm->tm_isdst = save_isdst;
  606. return (time_t)-1;
  607. }
  608. if (save_isdst >= 0) {
  609. if (ltm->tm_isdst && !save_isdst)
  610. {
  611. if (then + 3600 < then)
  612. then = (time_t)-1;
  613. else
  614. then += 3600;
  615. }
  616. else if (!ltm->tm_isdst && save_isdst)
  617. {
  618. if (then - 3600 > then)
  619. then = (time_t)-1;
  620. else
  621. then -= 3600;
  622. }
  623. ltm->tm_isdst = save_isdst;
  624. }
  625. if (tm != ltm) /* `tm' may already point to localtime's internal storage */
  626. *tm = *ltm;
  627. return then;
  628. }
  629. #ifndef NO_TIME_T_MAX
  630. /* Provide default values for the upper limit of the time_t range.
  631. These are the result of the decomposition into a `struct tm' for
  632. the time value 0xFFFFFFFEL ( = (time_t)-2 ).
  633. Note: `(time_t)-1' is reserved for "invalid time"! */
  634. # ifndef TM_YEAR_MAX
  635. # define TM_YEAR_MAX 2106
  636. # endif
  637. # ifndef TM_MON_MAX
  638. # define TM_MON_MAX 1 /* February */
  639. # endif
  640. # ifndef TM_MDAY_MAX
  641. # define TM_MDAY_MAX 7
  642. # endif
  643. # ifndef TM_HOUR_MAX
  644. # define TM_HOUR_MAX 6
  645. # endif
  646. # ifndef TM_MIN_MAX
  647. # define TM_MIN_MAX 28
  648. # endif
  649. # ifndef TM_SEC_MAX
  650. # define TM_SEC_MAX 14
  651. # endif
  652. #endif /* NO_TIME_T_MAX */
  653. /* Adjusts out-of-range values for `tm' field `tm_member'. */
  654. #define ADJUST_TM(tm_member, tm_carry, modulus) \
  655. if ((tm_member) < 0) { \
  656. tm_carry -= (1 - ((tm_member)+1) / (modulus)); \
  657. tm_member = (modulus-1) + (((tm_member)+1) % (modulus)); \
  658. } else if ((tm_member) >= (modulus)) { \
  659. tm_carry += (tm_member) / (modulus); \
  660. tm_member = (tm_member) % (modulus); \
  661. }
  662. /* Return the equivalent in seconds past 12:00:00 a.m. Jan 1, 1970 GMT
  663. of the Greenwich Mean time and date in the exploded time structure `tm'.
  664. This function does always put back normalized values into the `tm' struct,
  665. parameter, including the calculated numbers for `tm->tm_yday',
  666. `tm->tm_wday', and `tm->tm_isdst'.
  667. Returns -1 if the time in the `tm' parameter cannot be represented
  668. as valid `time_t' number. */
  669. time_t mkgmtime(tm)
  670. struct tm *tm;
  671. {
  672. int years, months, days, hours, minutes, seconds;
  673. years = tm->tm_year + TM_YEAR_BASE; /* year - 1900 -> year */
  674. months = tm->tm_mon; /* 0..11 */
  675. days = tm->tm_mday - 1; /* 1..31 -> 0..30 */
  676. hours = tm->tm_hour; /* 0..23 */
  677. minutes = tm->tm_min; /* 0..59 */
  678. seconds = tm->tm_sec; /* 0..61 in ANSI C. */
  679. ADJUST_TM(seconds, minutes, 60)
  680. ADJUST_TM(minutes, hours, 60)
  681. ADJUST_TM(hours, days, 24)
  682. ADJUST_TM(months, years, 12)
  683. if (days < 0)
  684. do {
  685. if (--months < 0) {
  686. --years;
  687. months = 11;
  688. }
  689. days += monthlen(months, years);
  690. } while (days < 0);
  691. else
  692. while (days >= monthlen(months, years)) {
  693. days -= monthlen(months, years);
  694. if (++months >= 12) {
  695. ++years;
  696. months = 0;
  697. }
  698. }
  699. /* Restore adjusted values in tm structure */
  700. tm->tm_year = years - TM_YEAR_BASE;
  701. tm->tm_mon = months;
  702. tm->tm_mday = days + 1;
  703. tm->tm_hour = hours;
  704. tm->tm_min = minutes;
  705. tm->tm_sec = seconds;
  706. /* Set `days' to the number of days into the year. */
  707. days += YDAYS(months, years);
  708. tm->tm_yday = days;
  709. /* Now calculate `days' to the number of days since Jan 1, 1970. */
  710. days = (unsigned)days + 365 * (unsigned)(years - EPOCH_YEAR) +
  711. (unsigned)(nleap (years));
  712. tm->tm_wday = ((unsigned)days + EPOCH_WDAY) % 7;
  713. tm->tm_isdst = 0;
  714. if (years < EPOCH_YEAR)
  715. return (time_t)-1;
  716. #if (defined(TM_YEAR_MAX) && defined(TM_MON_MAX) && defined(TM_MDAY_MAX))
  717. #if (defined(TM_HOUR_MAX) && defined(TM_MIN_MAX) && defined(TM_SEC_MAX))
  718. if (years > TM_YEAR_MAX ||
  719. (years == TM_YEAR_MAX &&
  720. (tm->tm_yday > (YDAYS(TM_MON_MAX, TM_YEAR_MAX) + (TM_MDAY_MAX - 1)) ||
  721. (tm->tm_yday == (YDAYS(TM_MON_MAX, TM_YEAR_MAX) + (TM_MDAY_MAX - 1)) &&
  722. (hours > TM_HOUR_MAX ||
  723. (hours == TM_HOUR_MAX &&
  724. (minutes > TM_MIN_MAX ||
  725. (minutes == TM_MIN_MAX && seconds > TM_SEC_MAX) )))))))
  726. return (time_t)-1;
  727. #endif
  728. #endif
  729. return (time_t)(SECSPERDAY * (unsigned long)(unsigned)days +
  730. SECSPERHOUR * (unsigned long)hours +
  731. (unsigned long)(SECSPERMIN * minutes + seconds));
  732. }
  733. #endif /* __timezone_c */