time.c 47 KB

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