time.c 47 KB

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