timezone.c 25 KB

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