time.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  18. *
  19. * Besides uClibc, I'm using this code in my libc for elks, which is
  20. * a 16-bit environment with a fairly limited compiler. It would make
  21. * things much easier for me if this file isn't modified unnecessarily.
  22. * In particular, please put any new or replacement functions somewhere
  23. * else, and modify the makefile to use your version instead.
  24. * Thanks. Manuel
  25. *
  26. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  27. /* June 15, 2002 Initial Notes:
  28. *
  29. * Note: It is assumed throught that time_t is either long or unsigned long.
  30. * Similarly, clock_t is assumed to be long int.
  31. *
  32. * Warning: Assumptions are made about the layout of struct tm! It is
  33. * assumed that the initial fields of struct tm are (in order):
  34. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday
  35. *
  36. * Reached the inital goal of supporting the ANSI/ISO C99 time functions
  37. * as well as SUSv3's strptime. All timezone info is obtained from the
  38. * TZ env variable.
  39. *
  40. * Differences from glibc worth noting:
  41. *
  42. * Leap seconds are not considered here.
  43. *
  44. * glibc stores additional timezone info the struct tm, whereas we don't.
  45. *
  46. * Alternate digits and era handling are not currently implemented.
  47. * The modifiers are accepted, and tested for validity with the following
  48. * specifier, but are ignored otherwise.
  49. *
  50. * strftime does not implement glibc extension modifiers or widths for
  51. * conversion specifiers. However it does implement the glibc
  52. * extension specifiers %l, %k, and %s. It also recognizes %P, but
  53. * treats it as a synonym for %p; i.e. doesn't convert to lower case.
  54. *
  55. * strptime implements the glibc extension specifiers. However, it follows
  56. * SUSv3 in requiring at least one non-alphanumeric char between
  57. * conversion specifiers. Also, strptime only sets struct tm fields
  58. * for which format specifiers appear and does not try to infer other
  59. * fields (such as wday) as glibc's version does.
  60. *
  61. * TODO - Since glibc's %l and %k can space-pad their output in strftime,
  62. * it might be reasonable to eat whitespace first for those specifiers.
  63. * This could be done by pushing " %I" and " %H" respectively so that
  64. * leading whitespace is consumed. This is really only an issue if %l
  65. * or %k occurs at the start of the format string.
  66. *
  67. * TODO - Implement getdate? tzfile? struct tm extensions?
  68. *
  69. * TODO - Rework _time_mktime to remove the dependency on long long.
  70. *
  71. * TODO - Make tzset and _time_tzinfo refs threadsafe.
  72. */
  73. #define _GNU_SOURCE
  74. #define _STDIO_UTILITY
  75. #include <stdio.h>
  76. #include <stdlib.h>
  77. #include <stddef.h>
  78. #include <string.h>
  79. #include <time.h>
  80. #include <limits.h>
  81. #include <assert.h>
  82. #include <errno.h>
  83. #include <ctype.h>
  84. #include <langinfo.h>
  85. #include <locale.h>
  86. #ifndef __isleap
  87. #define __isleap(y) ( !((y) % 4) && ( ((y) % 100) || !((y) % 400) ) )
  88. #endif
  89. #ifndef TZNAME_MAX
  90. #define TZNAME_MAX _POSIX_TZNAME_MAX
  91. #endif
  92. /* TODO - This stuff belongs in some include/bits/ file. */
  93. #ifndef __BCC__
  94. #undef CLK_TCK
  95. #if (TARGET_ARCH == alpha) || (TARGET_ARCH == ia64)
  96. #define CLK_TCK 1024
  97. #else
  98. #define CLK_TCK 100
  99. #endif
  100. #endif
  101. /* The era code is currently unfinished. */
  102. /* #define ENABLE_ERA_CODE */
  103. extern struct tm __time_tm;
  104. typedef struct {
  105. long gmt_offset;
  106. long dst_offset;
  107. short day; /* for J or normal */
  108. short week;
  109. short month;
  110. short rule_type; /* J, M, \0 */
  111. char tzname[TZNAME_MAX+1];
  112. } rule_struct;
  113. extern rule_struct _time_tzinfo[2];
  114. extern struct tm *_time_t2tm(const time_t *__restrict timer,
  115. int offset, struct tm *__restrict result);
  116. extern time_t _time_mktime(struct tm *timeptr, int store_on_success);
  117. /**********************************************************************/
  118. #ifdef L_asctime
  119. static char __time_str[26];
  120. char *asctime(const struct tm *__restrict ptm)
  121. {
  122. return asctime_r(ptm, __time_str);
  123. }
  124. #endif
  125. /**********************************************************************/
  126. #ifdef L_asctime_r
  127. /* Strictly speaking, this implementation isn't correct. ANSI/ISO specifies
  128. * that the implementation of asctime() be equivalent to
  129. *
  130. * char *asctime(const struct tm *timeptr)
  131. * {
  132. * static char wday_name[7][3] = {
  133. * "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  134. * };
  135. * static char mon_name[12][3] = {
  136. * "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  137. * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  138. * };
  139. * static char result[26];
  140. *
  141. * sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
  142. * wday_name[timeptr->tm_wday],
  143. * mon_name[timeptr->tm_mon],
  144. * timeptr->tm_mday, timeptr->tm_hour,
  145. * timeptr->tm_min, timeptr->tm_sec,
  146. * 1900 + timeptr->tm_year);
  147. * return result;
  148. * }
  149. *
  150. * but the above is either inherently unsafe, or carries with it the implicit
  151. * assumption that all fields of timeptr fall within their usual ranges, and
  152. * that the tm_year value falls in the range [-2899,8099] to avoid overflowing
  153. * the static buffer.
  154. *
  155. * If we take the implicit assumption as given, then the implementation below
  156. * is still incorrect for tm_year values < -900, as there will be either
  157. * 0-padding and/or a missing negative sign for the year conversion . But given
  158. * the ususal use of asctime(), I think it isn't unreasonable to restrict correct
  159. * operation to the domain of years between 1000 and 9999.
  160. */
  161. /* This is generally a good thing, but if you're _sure_ any data passed will be
  162. * in range, you can #undef this. */
  163. #define SAFE_ASCTIME_R 1
  164. static const unsigned char at_data[] = {
  165. 'S', 'u', 'n', 'M', 'o', 'n', 'T', 'u', 'e', 'W', 'e', 'd',
  166. 'T', 'h', 'u', 'F', 'r', 'i', 'S', 'a', 't',
  167. 'J', 'a', 'n', 'F', 'e', 'b', 'M', 'a', 'r', 'A', 'p', 'r',
  168. 'M', 'a', 'y', 'J', 'u', 'n', 'J', 'u', 'l', 'A', 'u', 'g',
  169. 'S', 'e', 'p', 'O', 'c', 't', 'N', 'o', 'v', 'D', 'e', 'c',
  170. #ifdef SAFE_ASCTIME_R
  171. '?', '?', '?',
  172. #endif
  173. ' ', '?', '?', '?',
  174. ' ', '0',
  175. offsetof(struct tm, tm_mday),
  176. ' ', '0',
  177. offsetof(struct tm, tm_hour),
  178. ':', '0',
  179. offsetof(struct tm, tm_min),
  180. ':', '0',
  181. offsetof(struct tm, tm_sec),
  182. ' ', '?', '?', '?', '?', '\n', 0
  183. };
  184. char *asctime_r(register const struct tm *__restrict ptm,
  185. register char *__restrict buffer)
  186. {
  187. int tmp;
  188. assert(ptm);
  189. assert(buffer);
  190. #ifdef SAFE_ASCTIME_R
  191. memcpy(buffer, at_data + 3*(7 + 12), sizeof(at_data) - 3*(7 + 12));
  192. if (((unsigned int)(ptm->tm_wday)) <= 6) {
  193. memcpy(buffer, at_data + 3 * ptm->tm_wday, 3);
  194. }
  195. if (((unsigned int)(ptm->tm_mon)) <= 11) {
  196. memcpy(buffer + 4, at_data + 3*7 + 3 * ptm->tm_mon, 3);
  197. }
  198. #else
  199. assert(((unsigned int)(ptm->tm_wday)) <= 6);
  200. assert(((unsigned int)(ptm->tm_mon)) <= 11);
  201. memcpy(buffer, at_data + 3*(7 + 12) - 3, sizeof(at_data) + 3 - 3*(7 + 12));
  202. memcpy(buffer, at_data + 3 * ptm->tm_wday, 3);
  203. memcpy(buffer + 4, at_data + 3*7 + 3 * ptm->tm_mon, 3);
  204. #endif
  205. #ifdef SAFE_ASCTIME_R
  206. buffer += 19;
  207. tmp = ptm->tm_year + 1900;
  208. if (((unsigned int) tmp) < 10000) {
  209. buffer += 4;
  210. do {
  211. *buffer = '0' + (tmp % 10);
  212. tmp /= 10;
  213. } while (*--buffer == '?');
  214. }
  215. #else /* SAFE_ASCTIME_R */
  216. buffer += 23;
  217. tmp = ptm->tm_year + 1900;
  218. assert( ((unsigned int) tmp) < 10000 );
  219. do {
  220. *buffer = '0' + (tmp % 10);
  221. tmp /= 10;
  222. } while (*--buffer == '?');
  223. #endif /* SAFE_ASCTIME_R */
  224. do {
  225. --buffer;
  226. tmp = *((int *)(((const char *) ptm) + (int) *buffer));
  227. #ifdef SAFE_ASCTIME_R
  228. if (((unsigned int) tmp) >= 100) { /* Just check 2 digit non-neg. */
  229. buffer[-1] = *buffer = '?';
  230. } else
  231. #else /* SAFE_ASCTIME_R */
  232. assert(((unsigned int) tmp) < 100); /* Just check 2 digit non-neg. */
  233. #endif /* SAFE_ASCTIME_R */
  234. {
  235. *buffer = '0' + (tmp % 10);
  236. #ifdef __BCC__
  237. buffer[-1] = '0' + (tmp/10);
  238. #else /* __BCC__ */
  239. buffer[-1] += (tmp/10);
  240. #endif /* __BCC__ */
  241. }
  242. } while ((buffer -= 2)[-2] == '0');
  243. if (*++buffer == '0') { /* Space-pad day of month. */
  244. *buffer = ' ';
  245. }
  246. return buffer - 8;
  247. }
  248. #endif
  249. /**********************************************************************/
  250. #ifdef L_clock
  251. #include <sys/times.h>
  252. /* Note: According to glibc...
  253. * CAE XSH, Issue 4, Version 2: <time.h>
  254. * The value of CLOCKS_PER_SEC is required to be 1 million on all
  255. * XSI-conformant systems.
  256. */
  257. #ifndef __BCC__
  258. #if CLOCKS_PER_SEC != 1000000L
  259. #error unexpected value for CLOCKS_PER_SEC!
  260. #endif
  261. #endif
  262. clock_t clock(void)
  263. {
  264. struct tms xtms;
  265. unsigned long t;
  266. times(&xtms);
  267. t = ((unsigned long) xtms.tms_utime) + xtms.tms_stime;
  268. #if (CLK_TCK == CLOCKS_PER_SEC)
  269. return (t <= LONG_MAX) ? t : -1;
  270. #elif (CLK_TCK == 1) || (CLK_TCK == 10) || (CLK_TCK == 100) || (CLK_TCK == 1000)
  271. return (t <= (LONG_MAX / (CLOCKS_PER_SEC/CLK_TCK)))
  272. ? t * (CLOCKS_PER_SEC/CLK_TCK)
  273. : -1;
  274. #elif (CLK_TCK == 1024)
  275. return (t <= ((LONG_MAX / CLOCKS_PER_SEC) * CLK_TCK
  276. + ((LONG_MAX % CLOCKS_PER_SEC) * CLK_TCK) / CLOCKS_PER_SEC))
  277. ? ((t >> 10) * CLOCKS_PER_SEC) + (((t & 1023) * CLOCKS_PER_SEC) >> 10)
  278. : -1;
  279. #else
  280. #error fix for CLK_TCK
  281. #endif
  282. }
  283. #endif
  284. /**********************************************************************/
  285. #ifdef L_ctime
  286. char *ctime(const time_t *clock)
  287. {
  288. /* ANSI/ISO/SUSv3 say that ctime is equivalent to the following. */
  289. return asctime(localtime(clock));
  290. }
  291. #endif
  292. /**********************************************************************/
  293. #ifdef L_ctime_r
  294. char *ctime_r(const time_t *clock, char *buf)
  295. {
  296. struct tm xtms;
  297. return asctime_r(localtime_r(clock, &xtms), buf);
  298. }
  299. #endif
  300. /**********************************************************************/
  301. #ifdef L_difftime
  302. #include <float.h>
  303. #if FLT_RADIX != 2
  304. #error difftime implementation assumptions violated for you arch!
  305. #endif
  306. double difftime(time_t time1, time_t time0)
  307. {
  308. #if (LONG_MAX >> DBL_MANT_DIG) == 0
  309. /* time_t fits in the mantissa of a double. */
  310. return ((double) time1) - time0;
  311. #elif ((LONG_MAX >> DBL_MANT_DIG) >> DBL_MANT_DIG) == 0
  312. /* time_t can overflow the mantissa of a double. */
  313. time_t t1, t0, d;
  314. d = ((time_t) 1) << DBL_MANT_DIG;
  315. t1 = time1 / d;
  316. time1 -= (t1 * d);
  317. t0 = time0 / d;
  318. time0 -= (t0*d);
  319. /* Since FLT_RADIX==2 and d is a power of 2, the only possible
  320. * rounding error in the expression below would occur from the
  321. * addition. */
  322. return (((double) t1) - t0) * d + (((double) time1) - time0);
  323. #else
  324. #error difftime needs special implementation on your arch.
  325. #endif
  326. }
  327. #endif
  328. /**********************************************************************/
  329. #ifdef L_gmtime
  330. struct tm *gmtime(const time_t *timer)
  331. {
  332. register struct tm *ptm = &__time_tm;
  333. _time_t2tm(timer, 0, ptm); /* Can return NULL... */
  334. return ptm;
  335. }
  336. #endif
  337. /**********************************************************************/
  338. #ifdef L_gmtime_r
  339. struct tm *gmtime_r(const time_t *__restrict timer,
  340. struct tm *__restrict result)
  341. {
  342. return _time_t2tm(timer, 0, result);
  343. }
  344. #endif
  345. /**********************************************************************/
  346. #ifdef L_localtime
  347. struct tm *localtime(const time_t *timer)
  348. {
  349. register struct tm *ptm = &__time_tm;
  350. tzset();
  351. localtime_r(timer, ptm); /* Can return NULL... */
  352. return ptm;
  353. }
  354. #endif
  355. /**********************************************************************/
  356. #ifdef L_localtime_r
  357. static const unsigned char day_cor[] = { /* non-leap */
  358. 31, 31, 34, 34, 35, 35, 36, 36, 36, 37, 37, 38, 38
  359. /* 0, 0, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 7 */
  360. /* 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 */
  361. };
  362. static int tm_isdst(register const struct tm *__restrict ptm)
  363. {
  364. register rule_struct *r = _time_tzinfo;
  365. long sec;
  366. int i, isdst, isleap, day, day0, monlen, mday, oday;
  367. isdst = 0;
  368. if (r[1].tzname[0] != 0) {
  369. /* First, get the current seconds offset from the start of the year.
  370. * Fields of ptm are assumed to be in their normal ranges. */
  371. sec = ptm->tm_sec
  372. + 60 * (ptm->tm_min
  373. + 60 * (long)(ptm->tm_hour
  374. + 24 * ptm->tm_yday));
  375. /* Do some prep work. */
  376. i = (ptm->tm_year % 400) + 1900; /* Make sure we don't overflow. */
  377. isleap = __isleap(i);
  378. --i;
  379. day0 = (1
  380. + i /* Normal years increment 1 wday. */
  381. + (i/4)
  382. - (i/100)
  383. + (i/400) ) % 7;
  384. i = 0;
  385. do {
  386. day = r->day; /* Common for 'J' and # case. */
  387. if (r->rule_type == 'J') {
  388. if (!isleap || (day < (31+29))) {
  389. --day;
  390. }
  391. } else if (r->rule_type == 'M') {
  392. /* Find 0-based day number for 1st of the month. */
  393. day = 31*r->month - day_cor[r->month -1];
  394. if (isleap && (day >= 59)) {
  395. ++day;
  396. }
  397. monlen = 31 + day_cor[r->month -1] - day_cor[r->month];
  398. if (isleap && (r->month > 1)) {
  399. ++monlen;
  400. }
  401. /* Wweekday (0 is Sunday) of 1st of the month
  402. * is (day0 + day) % 7. */
  403. if ((mday = r->day - ((day0 + day) % 7)) >= 0) {
  404. mday -= 7; /* Back up into prev month since r->week>0. */
  405. }
  406. if ((mday += 7 * r->week) >= monlen) {
  407. mday -= 7;
  408. }
  409. /* So, 0-based day number is... */
  410. day += mday;
  411. }
  412. if (i != 0) {
  413. /* Adjust sec since dst->std change time is in dst. */
  414. sec += (r[-1].gmt_offset - r->gmt_offset);
  415. if (oday > day) {
  416. ++isdst; /* Year starts in dst. */
  417. }
  418. }
  419. oday = day;
  420. /* Now convert day to seconds and add offset and compare. */
  421. if (sec >= (day * 86400L) + r->dst_offset) {
  422. ++isdst;
  423. }
  424. ++r;
  425. } while (++i < 2);
  426. }
  427. return (isdst & 1);
  428. }
  429. struct tm *localtime_r(register const time_t *__restrict timer,
  430. register struct tm *__restrict result)
  431. {
  432. time_t x[1];
  433. long offset;
  434. int days, dst;
  435. dst = 0;
  436. do {
  437. days = -7;
  438. offset = 604800L - _time_tzinfo[dst].gmt_offset;
  439. if (*timer > (LONG_MAX - 604800L)) {
  440. days = -days;
  441. offset = -offset;
  442. }
  443. *x = *timer + offset;
  444. _time_t2tm(x, days, result);
  445. if (dst) {
  446. result->tm_isdst = dst;
  447. break;
  448. }
  449. ++dst;
  450. } while ((result->tm_isdst = tm_isdst(result)) != 0);
  451. return result;
  452. }
  453. #endif
  454. /**********************************************************************/
  455. #ifdef L_mktime
  456. time_t mktime(struct tm *timeptr)
  457. {
  458. return _time_mktime(timeptr, 1);
  459. }
  460. #endif
  461. /**********************************************************************/
  462. #ifdef L_strftime
  463. #define NO_E_MOD 0x80
  464. #define NO_O_MOD 0x40
  465. #define ILLEGAL_SPEC 0x3f
  466. #define INT_SPEC 0x00 /* must be 0x00!! */
  467. #define STRING_SPEC 0x10 /* must be 0x10!! */
  468. #define CALC_SPEC 0x20
  469. #define STACKED_SPEC 0x30
  470. #define MASK_SPEC 0x30
  471. /* Compatibility:
  472. *
  473. * No alternate digit (%O?) handling. Always uses 0-9.
  474. * Alternate locale format (%E?) handling is broken for nontrivial ERAs.
  475. * glibc's %P is currently faked by %p. This means it doesn't do lower case.
  476. * glibc's %k, %l, and %s are handled.
  477. * glibc apparently allows (and ignores) extraneous 'E' and 'O' modifiers,
  478. * while they are flagged as illegal conversions here.
  479. */
  480. /* Warning: Assumes ASCII values! (as do lots of other things in the lib...) */
  481. static const unsigned char spec[] = {
  482. /* A */ 0x03 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  483. /* B */ 0x04 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  484. /* C */ 0x0a | INT_SPEC | NO_O_MOD,
  485. /* D */ 0x02 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  486. /* E */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  487. /* F */ 0x03 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  488. /* G */ 0x03 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  489. /* H */ 0x0b | INT_SPEC | NO_E_MOD,
  490. /* I */ 0x0c | INT_SPEC | NO_E_MOD,
  491. /* J */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  492. /* K */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  493. /* L */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  494. /* M */ 0x0d | INT_SPEC | NO_E_MOD,
  495. /* N */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  496. /* O */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  497. /* P */ 0x05 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* glibc ; use %p */
  498. /* Q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  499. /* R */ 0x04 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  500. /* S */ 0x0e | INT_SPEC | NO_E_MOD,
  501. /* T */ 0x05 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  502. /* U */ 0x04 | CALC_SPEC | NO_E_MOD,
  503. /* V */ 0x05 | CALC_SPEC | NO_E_MOD,
  504. /* W */ 0x06 | CALC_SPEC | NO_E_MOD,
  505. /* X */ 0x0a | STACKED_SPEC | NO_O_MOD,
  506. /* Y */ 0x0f | INT_SPEC | NO_O_MOD,
  507. /* Z */ 0x01 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  508. '?', /* 26 */
  509. '?', /* 27 */
  510. '?', /* 28 */
  511. '?', /* 29 */
  512. 0, /* 30 */
  513. 0, /* 31 */
  514. /* a */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  515. /* b */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  516. /* c */ 0x08 | STACKED_SPEC | NO_O_MOD,
  517. /* d */ 0x00 | INT_SPEC | NO_E_MOD,
  518. /* e */ 0x01 | INT_SPEC | NO_E_MOD,
  519. /* f */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  520. /* g */ 0x02 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  521. /* h */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* same as b */
  522. /* i */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  523. /* j */ 0x08 | INT_SPEC | NO_E_MOD | NO_O_MOD,
  524. /* k */ 0x03 | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  525. /* l */ 0x04 | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  526. /* m */ 0x05 | INT_SPEC | NO_E_MOD,
  527. /* n */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  528. /* o */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  529. /* p */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  530. /* q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  531. /* r */ 0x0b | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  532. /* s */ 0x07 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  533. /* t */ 0x01 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  534. /* u */ 0x07 | INT_SPEC | NO_E_MOD,
  535. /* v */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  536. /* w */ 0x02 | INT_SPEC | NO_E_MOD,
  537. /* x */ 0x09 | STACKED_SPEC | NO_O_MOD,
  538. /* y */ 0x09 | INT_SPEC,
  539. /* z */ 0x00 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  540. /* WARNING!!! These are dependent on the layout of struct tm!!! */
  541. #define FIELD_MAX (26+6+26)
  542. 60 /* 61? */, 59, 23, 31, 11, 0 /* 9999 */, 6, 0 /* 365 */,
  543. #define TP_OFFSETS (FIELD_MAX+8)
  544. 3, /* d */
  545. 3, /* e */
  546. 6, /* w */
  547. 2, /* k */
  548. 2, /* l */
  549. 4, /* m */
  550. 0, /* CURRENTLY UNUSED */
  551. /* NOTE: u,j,y order must be preserved as 6,7,5 seq is used in the code! */
  552. #define CALC_OFFSETS (TP_OFFSETS + 7)
  553. 6, /* u */
  554. 7, /* j */
  555. 5, /* y */
  556. 5, /* C */
  557. 2, /* H */
  558. 2, /* I */
  559. 1, /* M */
  560. 0, /* S */
  561. 5, /* Y */
  562. 6, /* a */
  563. 4, /* b, h */
  564. 2, /* p */
  565. 6, /* A */
  566. 4, /* B */
  567. 2, /* P */
  568. #define TP_CODES (TP_OFFSETS + 16 + 6)
  569. 2 | 16, /* d */
  570. 2, /* e */
  571. 0 | 16, /* w */
  572. 2, /* k */
  573. 2 | 32 | 0, /* l */
  574. 2 | 16 | 1, /* m */
  575. 0, /* CURRENTLY UNUSED */
  576. 0 | 16 | 8 , /* u */
  577. 4 | 16 | 1, /* j */
  578. 2 | 128 | 32 | 16 , /* y */
  579. 2 | 128 | 64 | 32 | 16 , /* C */
  580. 2 | 16, /* H */
  581. 2 | 32 | 16 | 0, /* I */
  582. 2 | 16, /* M */
  583. 2 | 16, /* S */
  584. 6 | 16, /* Y */
  585. 2, /* a */
  586. 2, /* b, h */
  587. 2 | 64, /* p */
  588. 2, /* A */
  589. 2, /* B */
  590. 2 | 64, /* P */
  591. #define STRINGS_NL_ITEM_START (TP_CODES + 16 + 6)
  592. _NL_ITEM_INDEX(ABDAY_1), /* a */
  593. _NL_ITEM_INDEX(ABMON_1), /* b, h */
  594. _NL_ITEM_INDEX(AM_STR), /* p */
  595. _NL_ITEM_INDEX(DAY_1), /* A */
  596. _NL_ITEM_INDEX(MON_1), /* B */
  597. _NL_ITEM_INDEX(AM_STR), /* P -- wrong! need lower case */
  598. #define STACKED_STRINGS_START (STRINGS_NL_ITEM_START+6)
  599. 6, 7, 8, 16, 24, 29, /* 6 - offsets from offset-count to strings */
  600. '\n', 0, /* 2 */
  601. '\t', 0, /* 2 */
  602. '%', 'm', '/', '%', 'd', '/', '%', 'y', 0, /* 9 - %D */
  603. '%', 'Y', '-', '%', 'm', '-', '%', 'd', 0, /* 9 - %F (glibc extension) */
  604. '%', 'H', ':', '%', 'M', 0, /* 6 - %R*/
  605. '%', 'H', ':', '%', 'M', ':', '%', 'S', 0, /* 9 - %T */
  606. #define STACKED_STRINGS_NL_ITEM_START (STACKED_STRINGS_START + 43)
  607. _NL_ITEM_INDEX(D_T_FMT), /* c */
  608. _NL_ITEM_INDEX(D_FMT), /* x */
  609. _NL_ITEM_INDEX(T_FMT), /* X */
  610. _NL_ITEM_INDEX(T_FMT_AMPM), /* r */
  611. #ifdef ENABLE_ERA_CODE
  612. _NL_ITEM_INDEX(ERA_D_T_FMT), /* Ec */
  613. _NL_ITEM_INDEX(ERA_D_FMT), /* Ex */
  614. _NL_ITEM_INDEX(ERA_T_FMT), /* EX */
  615. #endif
  616. };
  617. static int load_field(int k, const struct tm *__restrict timeptr)
  618. {
  619. int r;
  620. int r_max;
  621. r = ((int *) timeptr)[k];
  622. r_max = spec[FIELD_MAX + k];
  623. if (k == 7) {
  624. r_max = 365;
  625. } else if (k == 5) {
  626. r += 1900;
  627. r_max = 9999;
  628. }
  629. if ((((unsigned int) r) > r_max) || ((k == 3) && !r)) {
  630. r = -1;
  631. }
  632. return r;
  633. }
  634. #define MAX_PUSH 4
  635. size_t strftime(char *__restrict s, size_t maxsize,
  636. const char *__restrict format,
  637. const struct tm *__restrict timeptr)
  638. {
  639. long tzo;
  640. register const char *p;
  641. register const char *o;
  642. const rule_struct *rsp;
  643. const char *stack[MAX_PUSH];
  644. size_t count;
  645. size_t o_count;
  646. int field_val, i, j, lvl;
  647. int x[3]; /* wday, yday, year */
  648. int isofm, days;
  649. char buf[__UIM_BUFLEN_LONG];
  650. unsigned char mod;
  651. unsigned char code;
  652. tzset(); /* We'll, let's get this out of the way. */
  653. lvl = 0;
  654. p = format;
  655. count = maxsize;
  656. LOOP:
  657. if (!count) {
  658. return 0;
  659. }
  660. if (!*p) {
  661. if (lvl == 0) {
  662. *s = 0; /* nul-terminate */
  663. return maxsize - count;
  664. }
  665. p = stack[--lvl];
  666. goto LOOP;
  667. }
  668. o_count = 1;
  669. if ((*(o = p) == '%') && (*++p != '%')) {
  670. o_count = 2;
  671. mod = ILLEGAL_SPEC;
  672. if ((*p == 'O') || (*p == 'E')) { /* modifier */
  673. mod |= ((*p == 'O') ? NO_O_MOD : NO_E_MOD);
  674. ++o_count;
  675. ++p;
  676. }
  677. if ((((unsigned char)(((*p) | 0x20) - 'a')) >= 26)
  678. || (((code = spec[(int)(*p - 'A')]) & mod) >= ILLEGAL_SPEC)
  679. ) {
  680. if (!*p) {
  681. --p;
  682. --o_count;
  683. }
  684. goto OUTPUT;
  685. }
  686. code &= ILLEGAL_SPEC; /* modifiers are preserved in mod var. */
  687. if ((code & MASK_SPEC) == STACKED_SPEC) {
  688. if (lvl == MAX_PUSH) {
  689. goto OUTPUT; /* Stack full so treat as illegal spec. */
  690. }
  691. stack[lvl++] = ++p;
  692. if ((code &= 0xf) < 8) {
  693. p = ((const char *) spec) + STACKED_STRINGS_START + code;
  694. p += *((unsigned char *)p);
  695. goto LOOP;
  696. }
  697. p = ((const char *) spec) + STACKED_STRINGS_NL_ITEM_START
  698. + (code & 7);
  699. #ifdef ENABLE_ERA_CODE
  700. if ((mod & NO_E_MOD) /* Actually, this means E modifier present. */
  701. && (*(o = nl_langinfo(_NL_ITEM(LC_TIME,
  702. (int)(((unsigned char *)p)[4]))
  703. )))
  704. ) {
  705. p = o;
  706. goto LOOP;
  707. }
  708. #endif
  709. p = nl_langinfo(_NL_ITEM(LC_TIME,
  710. (int)(*((unsigned char *)p))));
  711. goto LOOP;
  712. }
  713. o = spec + 26; /* set to "????" */
  714. if ((code & MASK_SPEC) == CALC_SPEC) {
  715. if (*p == 's') {
  716. time_t t;
  717. /* Use a cast to silence the warning since *timeptr won't
  718. * be changed. */
  719. if ((t = _time_mktime((struct tm *) timeptr, 0))
  720. == ((time_t) -1)
  721. ) {
  722. o_count = 1;
  723. goto OUTPUT;
  724. }
  725. #ifdef TIME_T_IS_UNSIGNED
  726. o = _uintmaxtostr(buf + sizeof(buf) - 1,
  727. (uintmax_t) t,
  728. 10, __UIM_DECIMAL);
  729. #else
  730. o = _uintmaxtostr(buf + sizeof(buf) - 1,
  731. (uintmax_t) t,
  732. -10, __UIM_DECIMAL);
  733. #endif
  734. o_count = sizeof(buf);
  735. goto OUTPUT;
  736. } else if (((*p) | 0x20) == 'z') { /* 'z' or 'Z' */
  737. if (timeptr->tm_isdst < 0) {
  738. /* SUSv3 specifies this behavior for 'z', but we'll also
  739. * treat it as "no timezone info" for 'Z' too. */
  740. o_count = 0;
  741. goto OUTPUT;
  742. }
  743. rsp = _time_tzinfo;
  744. if (timeptr->tm_isdst > 0) {
  745. ++rsp;
  746. }
  747. if (*p == 'Z') {
  748. o = rsp->tzname;
  749. assert(o != NULL);
  750. #if 0
  751. if (!o) { /* PARANOIA */
  752. o = spec+30; /* empty string */
  753. }
  754. #endif
  755. o_count = SIZE_MAX;
  756. goto OUTPUT;
  757. } else { /* z */
  758. *s = '+';
  759. if ((tzo = -rsp->gmt_offset) < 0) {
  760. tzo = -tzo;
  761. *s = '-';
  762. }
  763. ++s;
  764. --count;
  765. i = tzo / 60;
  766. field_val = ((i / 60) * 100) + (i % 60);
  767. i = 16 + 6; /* 0-fill, width = 4 */
  768. }
  769. } else {
  770. /* TODO: don't need year for U, W */
  771. for (i=0 ; i < 3 ; i++) {
  772. if ((x[i] = load_field(spec[CALC_OFFSETS+i],timeptr)) < 0) {
  773. goto OUTPUT;
  774. }
  775. }
  776. i = 16 + 2; /* 0-fill, width = 2 */
  777. if ((*p == 'U') || (*p == 'W')) {
  778. field_val = ((x[1] - x[0]) + 7);
  779. if (*p == 'W') {
  780. ++field_val;
  781. }
  782. field_val /= 7;
  783. if ((*p == 'W') && !x[0]) {
  784. --field_val;
  785. }
  786. } else { /* ((*p == 'g') || (*p == 'G') || (*p == 'V')) */
  787. ISO_LOOP:
  788. isofm = (((x[1] - x[0]) + 11) % 7) - 3; /* [-3,3] */
  789. if (x[1] < isofm) { /* belongs to previous year */
  790. --x[2];
  791. x[1] += 365 + __isleap(x[2]);
  792. goto ISO_LOOP;
  793. }
  794. field_val = ((x[1] - isofm) / 7) + 1; /* week # */
  795. days = 365 + __isleap(x[2]);
  796. isofm = ((isofm + 7*53 + 3 - days)) %7 + days - 3; /* next year */
  797. if (x[1] >= isofm) { /* next year */
  798. x[1] -= days;
  799. ++x[2];
  800. goto ISO_LOOP;
  801. }
  802. if (*p != 'V') { /* need year */
  803. field_val = x[2]; /* TODO: what if x[2] now 10000 ?? */
  804. if (*p == 'g') {
  805. field_val %= 100;
  806. } else {
  807. i = 16 + 6; /* 0-fill, width = 4 */
  808. }
  809. }
  810. }
  811. }
  812. } else {
  813. i = TP_OFFSETS + (code & 0x1f);
  814. if ((field_val = load_field(spec[i],timeptr)) < 0) {
  815. goto OUTPUT;
  816. }
  817. i = spec[i+(TP_CODES - TP_OFFSETS)];
  818. j = (i & 128) ? 100: 12;
  819. if (i & 64) {
  820. field_val /= j;;
  821. }
  822. if (i & 32) {
  823. field_val %= j;
  824. if (((i&128) + field_val) == 0) { /* mod 12? == 0 */
  825. field_val = j; /* set to 12 */
  826. }
  827. }
  828. field_val += (i & 1);
  829. if ((i & 8) && !field_val) {
  830. field_val += 7;
  831. }
  832. }
  833. if ((code & MASK_SPEC) == STRING_SPEC) {
  834. o_count = SIZE_MAX;
  835. field_val += spec[STRINGS_NL_ITEM_START + (code & 0xf)];
  836. o = nl_langinfo(_NL_ITEM(LC_TIME, field_val));
  837. } else {
  838. o_count = ((i >> 1) & 3) + 1;
  839. o = buf + o_count;
  840. do {
  841. *(char *)(--o) = '0' + (field_val % 10);
  842. field_val /= 10;
  843. } while (o > buf);
  844. if (*buf == '0') {
  845. *buf = ' ' + (i & 16);
  846. }
  847. }
  848. }
  849. OUTPUT:
  850. ++p;
  851. while (o_count && count && *o) {
  852. *s++ = *o++;
  853. --o_count;
  854. --count;
  855. }
  856. goto LOOP;
  857. }
  858. #endif
  859. /**********************************************************************/
  860. #ifdef L_strptime
  861. /* TODO:
  862. * 1) %l and %k are space-padded, so "%l" by itself fails while " %l" succeeds.
  863. * Both work for glibc. So, should we always strip spaces?
  864. * 2) %Z
  865. */
  866. /* Notes:
  867. * There are several differences between this strptime and glibc's strptime.
  868. * 1) glibc strips leading space before numeric conversions.
  869. * 2) glibc will read fields without whitespace in between. SUSv3 states
  870. * that you must have whitespace between conversion operators. Besides,
  871. * how do you know how long a number should be if there are leading 0s?
  872. * 3) glibc attempts to compute some the struct tm fields based on the
  873. * data retrieved; tm_wday in particular. I don't as I consider it
  874. * another glibc attempt at mind-reading...
  875. */
  876. #define NO_E_MOD 0x80
  877. #define NO_O_MOD 0x40
  878. #define ILLEGAL_SPEC 0x3f
  879. #define INT_SPEC 0x00 /* must be 0x00!! */
  880. #define STRING_SPEC 0x10 /* must be 0x10!! */
  881. #define CALC_SPEC 0x20
  882. #define STACKED_SPEC 0x30
  883. #define MASK_SPEC 0x30
  884. /* Warning: Assumes ASCII values! (as do lots of other things in the lib...) */
  885. static const unsigned char spec[] = {
  886. /* A */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  887. /* B */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  888. /* C */ 0x08 | INT_SPEC | NO_O_MOD,
  889. /* D */ 0x01 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  890. /* E */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  891. /* F */ 0x02 | STACKED_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  892. /* G */ 0x0f | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  893. /* H */ 0x06 | INT_SPEC | NO_E_MOD,
  894. /* I */ 0x07 | INT_SPEC | NO_E_MOD,
  895. /* J */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  896. /* K */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  897. /* L */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  898. /* M */ 0x04 | INT_SPEC | NO_E_MOD,
  899. /* N */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  900. /* O */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  901. /* P */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  902. /* Q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  903. /* R */ 0x03 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  904. /* S */ 0x05 | INT_SPEC | NO_E_MOD,
  905. /* T */ 0x04 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  906. /* U */ 0x0c | INT_SPEC | NO_E_MOD,
  907. /* V */ 0x0d | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  908. /* W */ 0x0c | INT_SPEC | NO_E_MOD,
  909. /* X */ 0x0a | STACKED_SPEC | NO_O_MOD,
  910. /* Y */ 0x0a | INT_SPEC | NO_O_MOD,
  911. /* Z */ 0x02 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  912. /* WARNING! This assumes orderings:
  913. * AM,PM
  914. * ABDAY_1-ABDAY-7,DAY_1-DAY_7
  915. * ABMON_1-ABMON_12,MON_1-MON12
  916. * Also, there are exactly 6 bytes between 'Z' and 'a'.
  917. */
  918. #define STRINGS_NL_ITEM_START (26)
  919. _NL_ITEM_INDEX(AM_STR), /* p (P) */
  920. _NL_ITEM_INDEX(ABMON_1), /* B, b */
  921. _NL_ITEM_INDEX(ABDAY_1), /* A, a */
  922. 2,
  923. 24,
  924. 14,
  925. /* a */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  926. /* b */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  927. /* c */ 0x08 | STACKED_SPEC | NO_O_MOD,
  928. /* d */ 0x00 | INT_SPEC | NO_E_MOD,
  929. /* e */ 0x00 | INT_SPEC | NO_E_MOD,
  930. /* f */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  931. /* g */ 0x0e | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  932. /* h */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  933. /* i */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  934. /* j */ 0x01 | INT_SPEC | NO_E_MOD | NO_O_MOD,
  935. /* k */ 0x06 | INT_SPEC | NO_E_MOD, /* glibc */
  936. /* l */ 0x07 | INT_SPEC | NO_E_MOD, /* glibc */
  937. /* m */ 0x02 | INT_SPEC | NO_E_MOD,
  938. /* n */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  939. /* o */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  940. /* p */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  941. /* q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  942. /* r */ 0x0b | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  943. /* s */ 0x00 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  944. /* t */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  945. /* u */ 0x0b | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  946. /* v */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  947. /* w */ 0x03 | INT_SPEC | NO_E_MOD,
  948. /* x */ 0x09 | STACKED_SPEC | NO_O_MOD,
  949. /* y */ 0x09 | INT_SPEC,
  950. /* z */ 0x01 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  951. #define INT_FIELD_START (26+6+26)
  952. /* (field #) << 3 + lower bound (0|1) + correction 0:none, 2:-1, 4:-1900
  953. * followed by upper bound prior to correction with 1=>366 and 2=>9999. */
  954. /* d, e */ (3 << 3) + 1 + 0, 31,
  955. /* j */ (7 << 3) + 1 + 2, /* 366 */ 1,
  956. /* m */ (4 << 3) + 1 + 2, 12,
  957. /* w */ (6 << 3) + 0 + 0, 6,
  958. /* M */ (1 << 3) + 0 + 0, 59,
  959. /* S */ 0 + 0 + 0, 60,
  960. /* H (k) */ (2 << 3) + 0 + 0, 23,
  961. /* I (l) */ (9 << 3) + 1 + 0, 12, /* goes with 8 -- am/pm */
  962. /* C */ (10<< 3) + 0 + 0, 99,
  963. /* y */ (11<< 3) + 0 + 0, 99,
  964. /* Y */ (5 << 3) + 0 + 4, /* 9999 */ 2,
  965. /* u */ (6 << 3) + 1 + 0, 7,
  966. /* The following are processed and range-checked, but ignored otherwise. */
  967. /* U, W */ (12<< 3) + 0 + 0, 53,
  968. /* V */ (12<< 3) + 1 + 0, 53,
  969. /* g */ (12<< 3) + 0 + 0, 99,
  970. /* G */ (12<< 3) + 0 /*+ 4*/, /* 9999 */ 2, /* Note: -1 or 10000? */
  971. #define STACKED_STRINGS_START (INT_FIELD_START+32)
  972. 5, 6, 14, 22, 27, /* 5 - offsets from offset-count to strings */
  973. ' ', 0, /* 2 - %n or %t */
  974. '%', 'm', '/', '%', 'd', '/', '%', 'y', 0, /* 9 - %D */
  975. '%', 'Y', '-', '%', 'm', '-', '%', 'd', 0, /* 9 - %F (glibc extension) */
  976. '%', 'H', ':', '%', 'M', 0, /* 6 - %R*/
  977. '%', 'H', ':', '%', 'M', ':', '%', 'S', 0, /* 9 - %T */
  978. #define STACKED_STRINGS_NL_ITEM_START (STACKED_STRINGS_START + 40)
  979. _NL_ITEM_INDEX(D_T_FMT), /* c */
  980. _NL_ITEM_INDEX(D_FMT), /* x */
  981. _NL_ITEM_INDEX(T_FMT), /* X */
  982. _NL_ITEM_INDEX(T_FMT_AMPM), /* r */
  983. #ifdef ENABLE_ERA_CODE
  984. _NL_ITEM_INDEX(ERA_D_T_FMT), /* Ec */
  985. _NL_ITEM_INDEX(ERA_D_FMT), /* Ex */
  986. _NL_ITEM_INDEX(ERA_T_FMT), /* EX */
  987. #endif
  988. };
  989. #define MAX_PUSH 4
  990. char *strptime(const char *__restrict buf, const char *__restrict format,
  991. struct tm *__restrict tm)
  992. {
  993. register const char *p;
  994. char *o;
  995. const char *stack[MAX_PUSH];
  996. int i, j, lvl;
  997. int fields[13];
  998. unsigned char mod;
  999. unsigned char code;
  1000. i = 0;
  1001. do {
  1002. fields[i] = INT_MIN;
  1003. } while (++i < 13);
  1004. lvl = 0;
  1005. p = format;
  1006. LOOP:
  1007. if (!*p) {
  1008. if (lvl == 0) { /* Done. */
  1009. if (fields[6] == 7) { /* Cleanup for %u here since just once. */
  1010. fields[6] = 0; /* Don't use mod in case unset. */
  1011. }
  1012. i = 0;
  1013. do { /* Store the values into tm. */
  1014. ((int *) tm)[i] = fields[i];
  1015. } while (++i < 8);
  1016. return (char *) buf; /* Success. */
  1017. }
  1018. p = stack[--lvl];
  1019. goto LOOP;
  1020. }
  1021. if ((*p == '%') && (*++p != '%')) {
  1022. mod = ILLEGAL_SPEC;
  1023. if ((*p == 'O') || (*p == 'E')) { /* Modifier? */
  1024. mod |= ((*p == 'O') ? NO_O_MOD : NO_E_MOD);
  1025. ++p;
  1026. }
  1027. if (!*p
  1028. || (((unsigned char)(((*p) | 0x20) - 'a')) >= 26)
  1029. || (((code = spec[(int)(*p - 'A')]) & mod) >= ILLEGAL_SPEC)
  1030. ) {
  1031. return NULL; /* Illegal spec. */
  1032. }
  1033. if ((code & MASK_SPEC) == STACKED_SPEC) {
  1034. if (lvl == MAX_PUSH) {
  1035. return NULL; /* Stack full so treat as illegal spec. */
  1036. }
  1037. stack[lvl++] = ++p;
  1038. if ((code &= 0xf) < 8) {
  1039. p = ((const char *) spec) + STACKED_STRINGS_START + code;
  1040. p += *((unsigned char *)p);
  1041. goto LOOP;
  1042. }
  1043. p = ((const char *) spec) + STACKED_STRINGS_NL_ITEM_START
  1044. + (code & 7);
  1045. #ifdef ENABLE_ERA_CODE
  1046. if ((mod & NO_E_MOD) /* Actually, this means E modifier present. */
  1047. && (*(o = nl_langinfo(_NL_ITEM(LC_TIME,
  1048. (int)(((unsigned char *)p)[4]))
  1049. )))
  1050. ) {
  1051. p = o;
  1052. goto LOOP;
  1053. }
  1054. #endif
  1055. p = nl_langinfo(_NL_ITEM(LC_TIME,
  1056. (int)(*((unsigned char *)p))));
  1057. goto LOOP;
  1058. }
  1059. ++p;
  1060. if ((code & MASK_SPEC) == STRING_SPEC) {
  1061. code &= 0xf;
  1062. j = spec[STRINGS_NL_ITEM_START + 3 + code];
  1063. i = _NL_ITEM(LC_TIME, spec[STRINGS_NL_ITEM_START + code]);
  1064. /* Go backwards to check full names before abreviations. */
  1065. do {
  1066. --j;
  1067. o = nl_langinfo(i+j);
  1068. if (!strncasecmp(buf,o,strlen(o)) && *o) { /* Found a match. */
  1069. do {
  1070. ++buf;
  1071. } while (*++o);
  1072. if (!code) { /* am/pm */
  1073. fields[8] = j * 12;
  1074. if (fields[9] >= 0) { /* We have a previous %I or %l. */
  1075. fields[2] = fields[9] + fields[8];
  1076. }
  1077. } else { /* day (4) or month (6) */
  1078. fields[2 + (code << 1)]
  1079. = j % (spec[STRINGS_NL_ITEM_START + 3 + code] >> 1);
  1080. }
  1081. goto LOOP;
  1082. }
  1083. } while (j);
  1084. return NULL; /* Failed to match. */
  1085. }
  1086. if ((code & MASK_SPEC) == CALC_SPEC) {
  1087. if ((code &= 0xf) < 1) { /* s or z*/
  1088. time_t t;
  1089. o = (char *) buf;
  1090. i = errno;
  1091. __set_errno(0);
  1092. if (!isspace(*buf)) { /* Signal an error if whitespace. */
  1093. #ifdef TIME_T_IS_UNSIGNED
  1094. t = strtoul(buf, &o, 10);
  1095. #else
  1096. t = strtol(buf, &o, 10);
  1097. #endif
  1098. }
  1099. if ((o == buf) || errno) { /* Not a number or overflow. */
  1100. return NULL;
  1101. }
  1102. __set_errno(i); /* Restore errno. */
  1103. buf = o;
  1104. if (!code) { /* s */
  1105. localtime_r(&t, tm); /* TODO: check for failure? */
  1106. i = 0;
  1107. do { /* Now copy values from tm to fields. */
  1108. fields[i] = ((int *) tm)[i];
  1109. } while (++i < 8);
  1110. }
  1111. }
  1112. /* TODO: glibc treats %Z as a nop. For now, do the same. */
  1113. goto LOOP;
  1114. }
  1115. assert((code & MASK_SPEC) == INT_SPEC);
  1116. {
  1117. register const unsigned char *x;
  1118. code &= 0xf;
  1119. x = spec + INT_FIELD_START + (code << 1);
  1120. if ((j = x[1]) < 3) { /* upper bound (inclusive) */
  1121. j = ((j==1) ? 366 : 9999);
  1122. }
  1123. i = -1;
  1124. while (isdigit(*buf)) {
  1125. if (i < 0) {
  1126. i = 0;
  1127. }
  1128. if ((i = 10*i + (*buf - '0')) > j) { /* Overflow. */
  1129. return NULL;
  1130. }
  1131. ++buf;
  1132. }
  1133. if (i < (*x & 1)) { /* This catches no-digit case too. */
  1134. return NULL;
  1135. }
  1136. if (*x & 2) {
  1137. --i;
  1138. }
  1139. if (*x & 4) {
  1140. i -= 1900;
  1141. }
  1142. if (*x == (9 << 3) + 1 + 0) { /* %I or %l */
  1143. if (i == 12) {
  1144. i = 0;
  1145. }
  1146. if (fields[8] >= 0) { /* We have a previous %p or %P. */
  1147. fields[2] = i + fields[8];
  1148. }
  1149. }
  1150. fields[(*x) >> 3] = i;
  1151. if (((unsigned char)(*x - (10<< 3) + 0 + 0)) <= 8) { /* %C or %y */
  1152. if ((j = fields[10]) < 0) { /* No %C, so i must be %y data. */
  1153. if (i <= 68) { /* Map [0-68] to 2000+i */
  1154. i += 100;
  1155. }
  1156. } else { /* Have %C data, but what about %y? */
  1157. if ((i = fields[11]) < 0) { /* No %y data. */
  1158. i = 0; /* Treat %y val as 0 following glibc's example. */
  1159. }
  1160. i += 100*(j - 19);
  1161. }
  1162. fields[5] = i;
  1163. }
  1164. }
  1165. goto LOOP;
  1166. } else if (isspace(*p)) {
  1167. ++p;
  1168. while (isspace(*buf)) {
  1169. ++buf;
  1170. }
  1171. goto LOOP;
  1172. } else if (*buf++ == *p++) {
  1173. goto LOOP;
  1174. }
  1175. return NULL;
  1176. }
  1177. #endif
  1178. /**********************************************************************/
  1179. #ifdef L_time
  1180. #ifndef __BCC__
  1181. #error The uClibc version of time is in sysdeps/linux/common.
  1182. #endif
  1183. time_t time(register time_t *tloc)
  1184. {
  1185. struct timeval tv;
  1186. register struct timeval *p = &tv;
  1187. gettimeofday(p, NULL); /* This should never fail... */
  1188. if (tloc) {
  1189. *tloc = p->tv_sec;
  1190. }
  1191. return p->tv_sec;
  1192. }
  1193. #endif
  1194. /**********************************************************************/
  1195. #ifdef L_tzset
  1196. static const char vals[] = {
  1197. 'T', 'Z', 0, /* 3 */
  1198. 'U', 'T', 'C', 0, /* 4 */
  1199. 25, 60, 60, 1, /* 4 */
  1200. '.', 1, /* M */
  1201. 5, '.', 1,
  1202. 6, 0, 0, /* Note: overloaded for non-M non-J case... */
  1203. 0, 1, 0, /* J */
  1204. ',', 'M', '4', '.', '1', '.', '0',
  1205. ',', 'M', '1', '0', '.', '5', '.', '0', 0
  1206. };
  1207. #define TZ vals
  1208. #define UTC (vals + 3)
  1209. #define RANGE (vals + 7)
  1210. #define RULE (vals + 11 - 1)
  1211. #define DEFAULT_RULES (vals + 22)
  1212. /* Initialize to UTC. */
  1213. int daylight = 0;
  1214. long timezone = 0;
  1215. char *tzname[2] = { (char *) UTC, (char *) (UTC-1) };
  1216. rule_struct _time_tzinfo[2];
  1217. static const char *getoffset(register const char *e, long *pn)
  1218. {
  1219. register const char *s = RANGE-1;
  1220. long n;
  1221. int f;
  1222. n = 0;
  1223. f = -1;
  1224. do {
  1225. ++s;
  1226. if (isdigit(*e)) {
  1227. f = *e++ - '0';
  1228. }
  1229. if (isdigit(*e)) {
  1230. f = 10 * f + (*e++ - '0');
  1231. }
  1232. if (((unsigned int)f) >= *s) {
  1233. return NULL;
  1234. }
  1235. n = (*s) * n + f;
  1236. f = 0;
  1237. if (*e == ':') {
  1238. ++e;
  1239. --f;
  1240. }
  1241. } while (*s > 1);
  1242. *pn = n;
  1243. return e;
  1244. }
  1245. static const char *getnumber(register const char *e, int *pn)
  1246. {
  1247. #ifdef __BCC__
  1248. /* bcc can optimize the counter if it thinks it is a pointer... */
  1249. register const char *n = (const char *) 3;
  1250. int f;
  1251. f = 0;
  1252. while (n && isdigit(*e)) {
  1253. f = 10 * f + (*e++ - '0');
  1254. --n;
  1255. }
  1256. *pn = f;
  1257. return (n == (const char *) 3) ? NULL : e;
  1258. #else /* __BCC__ */
  1259. int n, f;
  1260. n = 3;
  1261. f = 0;
  1262. while (n && isdigit(*e)) {
  1263. f = 10 * f + (*e++ - '0');
  1264. --n;
  1265. }
  1266. *pn = f;
  1267. return (n == 3) ? NULL : e;
  1268. #endif /* __BCC__ */
  1269. }
  1270. void tzset(void)
  1271. {
  1272. register const char *e;
  1273. register char *s;
  1274. long off;
  1275. short *p;
  1276. rule_struct new_rules[2];
  1277. int n, count, f;
  1278. char c;
  1279. if (!(e = getenv(TZ)) || !*e) { /* Not set or set to empty string. */
  1280. ILLEGAL: /* TODO: Clean up the following... */
  1281. s = _time_tzinfo[0].tzname;
  1282. *s = 'U';
  1283. *++s = 'T';
  1284. *++s = 'C';
  1285. *++s =
  1286. *_time_tzinfo[1].tzname = 0;
  1287. _time_tzinfo[0].gmt_offset = 0;
  1288. goto DONE;
  1289. }
  1290. if (*e == ':') { /* Ignore leading ':'. */
  1291. ++e;
  1292. }
  1293. count = 0;
  1294. new_rules[1].tzname[0] = 0;
  1295. LOOP:
  1296. /* Get std or dst name. */
  1297. c = 0;
  1298. if (*e == '<') {
  1299. ++e;
  1300. c = '>';
  1301. }
  1302. s = new_rules[count].tzname;
  1303. n = 0;
  1304. while (*e
  1305. && (isalpha(*e)
  1306. || (c && (isdigit(*e) || (*e == '+') || (*e == '-'))))
  1307. ) {
  1308. *s++ = *e++;
  1309. if (++n > TZNAME_MAX) {
  1310. goto ILLEGAL;
  1311. }
  1312. }
  1313. *s = 0;
  1314. if ((n < 3) /* Check for minimum length. */
  1315. || (c && (*e++ != c)) /* Match any quoting '<'. */
  1316. ) {
  1317. goto ILLEGAL;
  1318. }
  1319. /* Get offset */
  1320. s = (char *) e;
  1321. if ((*e != '-') && (*e != '+')) {
  1322. if (count && !isdigit(*e)) {
  1323. off -= 3600; /* Default to 1 hour ahead of std. */
  1324. goto SKIP_OFFSET;
  1325. }
  1326. --e;
  1327. }
  1328. ++e;
  1329. if (!(e = getoffset(e, &off))) {
  1330. goto ILLEGAL;
  1331. }
  1332. if (*s == '-') {
  1333. off = -off; /* Save off in case needed for dst default. */
  1334. }
  1335. SKIP_OFFSET:
  1336. new_rules[count].gmt_offset = off;
  1337. if (!count) {
  1338. if (*e) {
  1339. ++count;
  1340. goto LOOP;
  1341. }
  1342. } else { /* OK, we have dst, so get some rules. */
  1343. count = 0;
  1344. if (!*e) { /* No rules so default to US rules. */
  1345. e = DEFAULT_RULES;
  1346. }
  1347. do {
  1348. if (*e++ != ',') {
  1349. goto ILLEGAL;
  1350. }
  1351. n = 365;
  1352. s = (char *) RULE;
  1353. if ((c = *e++) == 'M') {
  1354. n = 12;
  1355. } else if (c == 'J') {
  1356. s += 8;
  1357. } else {
  1358. --e;
  1359. c = 0;
  1360. s += 6;
  1361. }
  1362. *(p = &new_rules[count].rule_type) = c;
  1363. if (c != 'M') {
  1364. p -= 2;
  1365. }
  1366. do {
  1367. ++s;
  1368. if (!(e = getnumber(e, &f))
  1369. || (((unsigned int)(f - s[1])) > n)
  1370. || (*s && (*e++ != *s))
  1371. ) {
  1372. goto ILLEGAL;
  1373. }
  1374. *--p = f;
  1375. } while ((n = *(s += 2)) > 0);
  1376. off = 2 * 60 * 60; /* Default to 2:00:00 */
  1377. if (*e == '/') {
  1378. ++e;
  1379. if (!(e = getoffset(e, &off))) {
  1380. goto ILLEGAL;
  1381. }
  1382. }
  1383. new_rules[count].dst_offset = off;
  1384. } while (++count < 2);
  1385. if (*e) {
  1386. goto ILLEGAL;
  1387. }
  1388. }
  1389. memcpy(_time_tzinfo, new_rules, sizeof(new_rules));
  1390. DONE:
  1391. tzname[0] = _time_tzinfo[0].tzname;
  1392. tzname[1] = _time_tzinfo[1].tzname;
  1393. daylight = !!new_rules[1].tzname[0];
  1394. timezone = new_rules[0].gmt_offset;
  1395. }
  1396. #endif
  1397. /**********************************************************************/
  1398. /* #ifdef L_utime */
  1399. /* utime is a syscall in both linux and elks. */
  1400. /* int utime(const char *path, const struct utimbuf *times) */
  1401. /* #endif */
  1402. /**********************************************************************/
  1403. /* Non-SUSv3 */
  1404. /**********************************************************************/
  1405. #ifdef L_utimes
  1406. #ifndef __BCC__
  1407. #error The uClibc version of utimes is in sysdeps/linux/common.
  1408. #endif
  1409. #include <utime.h>
  1410. #include <sys/time.h>
  1411. int utimes(const char *filename, register const struct timeval *tvp)
  1412. {
  1413. register struct utimbuf *p = NULL;
  1414. struct utimbuf utb;
  1415. if (tvp) {
  1416. p = &utb;
  1417. p->actime = tvp[0].tv_sec;
  1418. p->modtime = tvp[1].tv_sec;
  1419. }
  1420. return utime(filename, p);
  1421. }
  1422. #endif
  1423. /**********************************************************************/
  1424. #ifdef L__time_t2tm
  1425. static const uint16_t vals[] = {
  1426. 60, 60, 24, 7 /* special */, 36524, 1461, 365, 0
  1427. };
  1428. static const unsigned char days[] = {
  1429. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, /* non-leap */
  1430. 29,
  1431. };
  1432. /* Notes:
  1433. * If time_t is 32 bits, then no overflow is possible.
  1434. * It time_t is > 32 bits, this needs to be adjusted to deal with overflow.
  1435. */
  1436. /* Note: offset is the correction in _days_ to *timer! */
  1437. struct tm *_time_t2tm(const time_t *__restrict timer,
  1438. int offset, struct tm *__restrict result)
  1439. {
  1440. register int *p;
  1441. time_t t1, t, v;
  1442. int wday;
  1443. {
  1444. register const uint16_t *vp;
  1445. t = *timer;
  1446. p = (int *) result;
  1447. p[7] = 0;
  1448. vp = vals;
  1449. do {
  1450. if ((v = *vp) == 7) {
  1451. /* Overflow checking, assuming time_t is long int... */
  1452. #if (LONG_MAX > INT_MAX) && (LONG_MAX > 2147483647L)
  1453. #if (INT_MAX == 2147483647L) && (LONG_MAX == 9223372036854775807L)
  1454. /* Valid range for t is [-784223472856L, 784223421720L].
  1455. * Outside of this range, the tm_year field will overflow. */
  1456. if (((unsigned long)(t + offset- -784223472856L))
  1457. > (784223421720L - -784223472856L)
  1458. ) {
  1459. return NULL;
  1460. }
  1461. #else
  1462. #error overflow conditions unknown
  1463. #endif
  1464. #endif
  1465. /* We have days since the epoch, so caluclate the weekday. */
  1466. #if defined(__BCC__) && TIME_T_IS_UNSIGNED
  1467. wday = (t + 4) % (*vp); /* t is unsigned */
  1468. #else
  1469. wday = ((int)((t % (*vp)) + 11)) % ((int)(*vp)); /* help bcc */
  1470. #endif
  1471. /* Set divisor to days in 400 years. Be kind to bcc... */
  1472. v = ((time_t)(vp[1])) << 2;
  1473. ++v;
  1474. /* Change to days since 1/1/1601 so that for 32 bit time_t
  1475. * values, we'll have t >= 0. This should be changed for
  1476. * archs with larger time_t types.
  1477. * Also, correct for offset since a multiple of 7. */
  1478. /* TODO: Does this still work on archs with time_t > 32 bits? */
  1479. t += (135140L - 366) + offset; /* 146097 - (365*30 + 7) -366 */
  1480. }
  1481. #if defined(__BCC__) && TIME_T_IS_UNSIGNED
  1482. t -= ((t1 = t / v) * v);
  1483. #else
  1484. if ((t -= ((t1 = t / v) * v)) < 0) {
  1485. t += v;
  1486. --t1;
  1487. }
  1488. #endif
  1489. if ((*vp == 7) && (t == v-1)) {
  1490. --t; /* Correct for 400th year leap case */
  1491. ++p[4]; /* Stash the extra day... */
  1492. }
  1493. #if defined(__BCC__) && 0
  1494. *p = t1;
  1495. if (v <= 60) {
  1496. *p = t;
  1497. t = t1;
  1498. }
  1499. ++p;
  1500. #else
  1501. if (v <= 60) {
  1502. *p++ = t;
  1503. t = t1;
  1504. } else {
  1505. *p++ = t1;
  1506. }
  1507. #endif
  1508. } while (*++vp);
  1509. }
  1510. if (p[-1] == 4) {
  1511. --p[-1];
  1512. t = 365;
  1513. }
  1514. *p += ((int) t); /* result[7] .. tm_yday */
  1515. p -= 2; /* at result[5] */
  1516. #if (LONG_MAX > INT_MAX) && (LONG_MAX > 2147483647L)
  1517. /* Protect against overflow. TODO: Unecessary if int arith wraps? */
  1518. *p = ((((p[-2]<<2) + p[-1])*25 + p[0])<< 2) + (p[1] - 299); /* tm_year */
  1519. #else
  1520. *p = ((((p[-2]<<2) + p[-1])*25 + p[0])<< 2) + p[1] - 299; /* tm_year */
  1521. #endif
  1522. p[1] = wday; /* result[6] .. tm_wday */
  1523. {
  1524. register const unsigned char *d = days;
  1525. wday = 1900 + *p;
  1526. if (__isleap(wday)) {
  1527. d += 11;
  1528. }
  1529. wday = p[2] + 1; /* result[7] .. tm_yday */
  1530. *--p = 0; /* at result[4] .. tm_mon */
  1531. while (wday > *d) {
  1532. wday -= *d;
  1533. if (*d == 29) {
  1534. d -= 11; /* Backup to non-leap Feb. */
  1535. }
  1536. ++d;
  1537. ++*p; /* Increment tm_mon. */
  1538. }
  1539. p[-1] = wday; /* result[3] .. tm_mday */
  1540. }
  1541. /* TODO -- should this be 0? */
  1542. p[4] = 0; /* result[8] .. tm_isdst */
  1543. return result;
  1544. }
  1545. #endif
  1546. /**********************************************************************/
  1547. #ifdef L___time_tm
  1548. struct tm __time_tm; /* Global shared by gmtime() and localtime(). */
  1549. #endif
  1550. /**********************************************************************/
  1551. #ifdef L__time_mktime
  1552. static const unsigned char vals[] = {
  1553. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, /* non-leap */
  1554. 29,
  1555. };
  1556. time_t _time_mktime(struct tm *timeptr, int store_on_success)
  1557. {
  1558. #ifdef __BCC__
  1559. long days, secs;
  1560. #else
  1561. long long secs;
  1562. #endif
  1563. time_t t;
  1564. struct tm x;
  1565. /* 0:sec 1:min 2:hour 3:mday 4:mon 5:year 6:wday 7:yday 8:isdst */
  1566. register int *p = (int *) &x;
  1567. register const unsigned char *s;
  1568. int d;
  1569. tzset();
  1570. memcpy(p, timeptr, sizeof(struct tm));
  1571. d = 400;
  1572. p[5] = (p[5] - ((p[6] = p[5]/d) * d)) + (p[7] = p[4]/12);
  1573. if ((p[4] -= 12 * p[7]) < 0) {
  1574. p[4] += 12;
  1575. --p[5];
  1576. }
  1577. s = vals;
  1578. d = (p[5] += 1900); /* Correct year. Now between 1900 and 2300. */
  1579. if (__isleap(d)) {
  1580. s += 11;
  1581. }
  1582. p[7] = 0;
  1583. d = p[4];
  1584. while (d) {
  1585. p[7] += *s;
  1586. if (*s == 29) {
  1587. s -= 11; /* Backup to non-leap Feb. */
  1588. }
  1589. ++s;
  1590. --d;
  1591. }
  1592. #ifdef __BCC__
  1593. /* TODO - check */
  1594. d = p[5] - 1;
  1595. days = -719163L + d*365 + ((d/4) - (d/100) + (d/400) + p[3] + p[7]);
  1596. secs = p[0] + 60*( p[1] + 60*((long)(p[2])) );
  1597. if (secs < 0) {
  1598. secs += 120009600L;
  1599. days -= 1389;
  1600. }
  1601. if ( ((unsigned long)(days + secs/86400L)) > 49710L) {
  1602. return -1;
  1603. }
  1604. secs += (days * 86400L);
  1605. #else
  1606. d = p[5] - 1;
  1607. d = -719163L + d*365 + (d/4) - (d/100) + (d/400);
  1608. secs = p[0]
  1609. + _time_tzinfo[timeptr->tm_isdst > 0].gmt_offset
  1610. + 60*( p[1]
  1611. + 60*(p[2]
  1612. + 24*(((146073L * ((long long)(p[6])) + d)
  1613. + p[3]) + p[7])));
  1614. if (((unsigned long long)(secs - LONG_MIN))
  1615. > (((unsigned long long)LONG_MAX) - LONG_MIN)
  1616. ) {
  1617. return -1;
  1618. }
  1619. #endif
  1620. t = secs;
  1621. localtime_r(&t, (struct tm *)p);
  1622. if (store_on_success) {
  1623. memcpy(timeptr, p, sizeof(struct tm));
  1624. }
  1625. return t;
  1626. }
  1627. #endif
  1628. /**********************************************************************/