time.c 47 KB

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