time.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366
  1. /* Copyright (C) 2002-2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. /* June 15, 2002 Initial Notes:
  8. *
  9. * Note: It is assumed throught that time_t is either long or unsigned long.
  10. * Similarly, clock_t is assumed to be long int.
  11. *
  12. * Warning: Assumptions are made about the layout of struct tm! It is
  13. * assumed that the initial fields of struct tm are (in order):
  14. * tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday
  15. *
  16. * Reached the inital goal of supporting the ANSI/ISO C99 time functions
  17. * as well as SUSv3's strptime. All timezone info is obtained from the
  18. * TZ env variable.
  19. *
  20. * Differences from glibc worth noting:
  21. *
  22. * Leap seconds are not considered here.
  23. *
  24. * glibc stores additional timezone info the struct tm, whereas we don't.
  25. *
  26. * Alternate digits and era handling are not currently implemented.
  27. * The modifiers are accepted, and tested for validity with the following
  28. * specifier, but are ignored otherwise.
  29. *
  30. * strftime does not implement glibc extension modifiers or widths for
  31. * conversion specifiers. However it does implement the glibc
  32. * extension specifiers %l, %k, and %s. It also recognizes %P, but
  33. * treats it as a synonym for %p; i.e. doesn't convert to lower case.
  34. *
  35. * strptime implements the glibc extension specifiers. However, it follows
  36. * SUSv3 in requiring at least one non-alphanumeric char between
  37. * conversion specifiers. Also, strptime only sets struct tm fields
  38. * for which format specifiers appear and does not try to infer other
  39. * fields (such as wday) as glibc's version does.
  40. *
  41. * TODO - Since glibc's %l and %k can space-pad their output in strftime,
  42. * it might be reasonable to eat whitespace first for those specifiers.
  43. * This could be done by pushing " %I" and " %H" respectively so that
  44. * leading whitespace is consumed. This is really only an issue if %l
  45. * or %k occurs at the start of the format string.
  46. *
  47. * TODO - Implement getdate? tzfile? struct tm extensions?
  48. *
  49. * TODO - Rework _time_mktime to remove the dependency on long long.
  50. */
  51. /* Oct 28, 2002
  52. *
  53. * Fixed allowed char check for std and dst TZ fields.
  54. *
  55. * Added several options concerned with timezone support. The names will
  56. * probably change once Erik gets the new config system in place.
  57. *
  58. * Defining __TIME_TZ_FILE causes tzset() to attempt to read the TZ value
  59. * from the file /etc/TZ if the TZ env variable isn't set. The file contents
  60. * must be the intended value of TZ, followed by a newline. No other chars,
  61. * spacing, etc is allowed. As an example, an easy way for me to init
  62. * /etc/TZ appropriately would be: echo CST6CDT > /etc/TZ
  63. *
  64. * Defining __TIME_TZ_FILE_ONCE will cause all further accesses of /etc/TZ
  65. * to be skipped once a legal value has been read.
  66. *
  67. * Defining __TIME_TZ_OPT_SPEED will cause a tzset() to keep a copy of the
  68. * last TZ setting string and do a "fast out" if the current string is the
  69. * same.
  70. *
  71. * Nov 21, 2002 Fix an error return case in _time_mktime.
  72. *
  73. * Nov 26, 2002 Fix bug in setting daylight and timezone when no (valid) TZ.
  74. * Bug reported by Arne Bernin <arne@alamut.de> in regards to freeswan.
  75. *
  76. * July 27, 2003 Adjust the struct tm extension field support.
  77. * Change __tm_zone back to a ptr and add the __tm_tzname[] buffer for
  78. * __tm_zone to point to. This gets around complaints from g++.
  79. * Who knows... it might even fix the PPC timezone init problem.
  80. *
  81. * July 29, 2003 Fix a bug in mktime behavior when tm_isdst was -1.
  82. * Bug reported by "Sid Wade" <sid@vivato.net> in regards to busybox.
  83. *
  84. * NOTE: uClibc mktime behavior is different than glibc's when
  85. * the struct tm has tm_isdst == -1 and also had fields outside of
  86. * the normal ranges.
  87. *
  88. * Apparently, glibc examines (at least) tm_sec and guesses the app's
  89. * intention of assuming increasing or decreasing time when entering an
  90. * ambiguous time period at the dst<->st boundaries.
  91. *
  92. * The uClibc behavior is to always normalize the struct tm and then
  93. * try to determing the dst setting.
  94. *
  95. * As long as tm_isdst != -1 or the time specifiec by struct tm is
  96. * unambiguous (not falling in the dst<->st transition region) both
  97. * uClibc and glibc should produce the same result for mktime.
  98. *
  99. * Oct 31, 2003 Kill the seperate __tm_zone and __tm_tzname[] and which
  100. * doesn't work if you want the memcpy the struct. Sigh... I didn't
  101. * think about that. So now, when the extensions are enabled, we
  102. * malloc space when necessary and keep the timezone names in a linked
  103. * list.
  104. *
  105. * Fix a dst-related bug which resulted in use of uninitialized data.
  106. *
  107. * Nov 15, 2003 I forgot to update the thread locking in the last dst fix.
  108. *
  109. * Dec 14, 2003 Fix some dst issues in _time_mktime().
  110. * Normalize the tm_isdst value to -1, 0, or 1.
  111. * If no dst for this timezone, then reset tm_isdst to 0.
  112. *
  113. * May 7, 2004
  114. * Change clock() to allow wrapping.
  115. * Add timegm() function.
  116. * Make lookup_tzname() static (as it should have been).
  117. * Have strftime() get timezone information from the passed struct
  118. * for the %z and %Z conversions when using struct tm extensions.
  119. *
  120. * Jul 24, 2004
  121. * Fix 2 bugs in strftime related to glibc struct tm extensions.
  122. * 1) Need to negate tm_gmtoff field value when used. (bug 336).
  123. * 2) Deal with NULL ptr case for tm_zone field, which was causing
  124. * segfaults in both the NIST/PCTS tests and the Python 2.4.1
  125. * self-test suite.
  126. * NOTE: We set uninitialized timezone names to "???", and this
  127. * differs (intentionally) from glibc's behavior.
  128. */
  129. #define strnlen __strnlen
  130. #define _GNU_SOURCE
  131. #include <stdio.h>
  132. #include <stdlib.h>
  133. #include <stddef.h>
  134. #include <string.h>
  135. #include <time.h>
  136. #include <limits.h>
  137. #include <assert.h>
  138. #include <errno.h>
  139. #include <ctype.h>
  140. #include <langinfo.h>
  141. #include <locale.h>
  142. #include <bits/uClibc_uintmaxtostr.h>
  143. #ifdef __UCLIBC_HAS_XLOCALE__
  144. #include <xlocale.h>
  145. extern int __strncasecmp_l (__const char *__s1, __const char *__s2,
  146. size_t __n, __locale_t __loc)
  147. __THROW __attribute_pure__ __nonnull ((1, 2, 4)) attribute_hidden;
  148. extern size_t __strftime_l (char *__restrict __s, size_t __maxsize,
  149. __const char *__restrict __format,
  150. __const struct tm *__restrict __tp,
  151. __locale_t __loc) __THROW attribute_hidden;
  152. extern char *__strptime_l (__const char *__restrict __s,
  153. __const char *__restrict __fmt, struct tm *__tp,
  154. __locale_t __loc) __THROW attribute_hidden;
  155. #endif
  156. #ifndef __isleap
  157. #define __isleap(y) ( !((y) % 4) && ( ((y) % 100) || !((y) % 400) ) )
  158. #endif
  159. #ifndef TZNAME_MAX
  160. #define TZNAME_MAX _POSIX_TZNAME_MAX
  161. #endif
  162. /**********************************************************************/
  163. /* The era code is currently unfinished. */
  164. /* #define ENABLE_ERA_CODE */
  165. #define TZ_BUFLEN (2*TZNAME_MAX + 56)
  166. #ifdef __UCLIBC_HAS_TZ_FILE__
  167. #include <sys/stat.h>
  168. #include <fcntl.h>
  169. #include <unistd.h>
  170. #include "paths.h"
  171. /* ":<tzname>+hh:mm:ss<tzname>+hh:mm:ss,Mmm.w.d/hh:mm:ss,Mmm.w.d/hh:mm:ss" + nul */
  172. /* 1 + 2*(1+TZNAME_MAX+1 + 9 + 7 + 9) + 1 = 2*TZNAME_MAX + 56 */
  173. #else /* __UCLIBC_HAS_TZ_FILE__ */
  174. /* Probably no longer needed. */
  175. #undef __UCLIBC_HAS_TZ_FILE_READ_MANY__
  176. #endif /* __UCLIBC_HAS_TZ_FILE__ */
  177. /**********************************************************************/
  178. extern struct tm __time_tm;
  179. extern struct tm *__localtime_r (__const time_t *__restrict __timer,
  180. struct tm *__restrict __tp) attribute_hidden;
  181. extern struct tm *__localtime (__const time_t *__timer) attribute_hidden;
  182. typedef struct {
  183. long gmt_offset;
  184. long dst_offset;
  185. short day; /* for J or normal */
  186. short week;
  187. short month;
  188. short rule_type; /* J, M, \0 */
  189. char tzname[TZNAME_MAX+1];
  190. } rule_struct;
  191. #ifdef __UCLIBC_HAS_THREADS__
  192. # include <pthread.h>
  193. extern pthread_mutex_t _time_tzlock;
  194. #endif
  195. #define TZLOCK __pthread_mutex_lock(&_time_tzlock)
  196. #define TZUNLOCK __pthread_mutex_unlock(&_time_tzlock)
  197. extern rule_struct _time_tzinfo[2];
  198. extern struct tm *_time_t2tm(const time_t *__restrict timer,
  199. int offset, struct tm *__restrict result) attribute_hidden;
  200. extern time_t _time_mktime(struct tm *timeptr, int store_on_success) attribute_hidden;
  201. extern struct tm *__time_localtime_tzi(const time_t *__restrict timer,
  202. struct tm *__restrict result,
  203. rule_struct *tzi) attribute_hidden;
  204. extern time_t _time_mktime_tzi(struct tm *timeptr, int store_on_success,
  205. rule_struct *tzi) attribute_hidden;
  206. extern char *__asctime (__const struct tm *__tp) attribute_hidden;
  207. extern char *__asctime_r (__const struct tm *__restrict __tp,
  208. char *__restrict __buf) attribute_hidden;
  209. /**********************************************************************/
  210. #ifdef L_asctime
  211. static char __time_str[26];
  212. char attribute_hidden *__asctime(const struct tm *ptm)
  213. {
  214. return __asctime_r(ptm, __time_str);
  215. }
  216. strong_alias(__asctime,asctime)
  217. #endif
  218. /**********************************************************************/
  219. #ifdef L_asctime_r
  220. /* Strictly speaking, this implementation isn't correct. ANSI/ISO specifies
  221. * that the implementation of asctime() be equivalent to
  222. *
  223. * char *asctime(const struct tm *timeptr)
  224. * {
  225. * static char wday_name[7][3] = {
  226. * "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  227. * };
  228. * static char mon_name[12][3] = {
  229. * "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  230. * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  231. * };
  232. * static char result[26];
  233. *
  234. * sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
  235. * wday_name[timeptr->tm_wday],
  236. * mon_name[timeptr->tm_mon],
  237. * timeptr->tm_mday, timeptr->tm_hour,
  238. * timeptr->tm_min, timeptr->tm_sec,
  239. * 1900 + timeptr->tm_year);
  240. * return result;
  241. * }
  242. *
  243. * but the above is either inherently unsafe, or carries with it the implicit
  244. * assumption that all fields of timeptr fall within their usual ranges, and
  245. * that the tm_year value falls in the range [-2899,8099] to avoid overflowing
  246. * the static buffer.
  247. *
  248. * If we take the implicit assumption as given, then the implementation below
  249. * is still incorrect for tm_year values < -900, as there will be either
  250. * 0-padding and/or a missing negative sign for the year conversion . But given
  251. * the ususal use of asctime(), I think it isn't unreasonable to restrict correct
  252. * operation to the domain of years between 1000 and 9999.
  253. */
  254. /* This is generally a good thing, but if you're _sure_ any data passed will be
  255. * in range, you can #undef this. */
  256. #define SAFE_ASCTIME_R 1
  257. static const unsigned char at_data[] = {
  258. 'S', 'u', 'n', 'M', 'o', 'n', 'T', 'u', 'e', 'W', 'e', 'd',
  259. 'T', 'h', 'u', 'F', 'r', 'i', 'S', 'a', 't',
  260. 'J', 'a', 'n', 'F', 'e', 'b', 'M', 'a', 'r', 'A', 'p', 'r',
  261. 'M', 'a', 'y', 'J', 'u', 'n', 'J', 'u', 'l', 'A', 'u', 'g',
  262. 'S', 'e', 'p', 'O', 'c', 't', 'N', 'o', 'v', 'D', 'e', 'c',
  263. #ifdef SAFE_ASCTIME_R
  264. '?', '?', '?',
  265. #endif
  266. ' ', '?', '?', '?',
  267. ' ', '0',
  268. offsetof(struct tm, tm_mday),
  269. ' ', '0',
  270. offsetof(struct tm, tm_hour),
  271. ':', '0',
  272. offsetof(struct tm, tm_min),
  273. ':', '0',
  274. offsetof(struct tm, tm_sec),
  275. ' ', '?', '?', '?', '?', '\n', 0
  276. };
  277. char attribute_hidden *__asctime_r(register const struct tm *__restrict ptm,
  278. register char *__restrict buffer)
  279. {
  280. int tmp;
  281. assert(ptm);
  282. assert(buffer);
  283. #ifdef SAFE_ASCTIME_R
  284. __memcpy(buffer, at_data + 3*(7 + 12), sizeof(at_data) - 3*(7 + 12));
  285. if (((unsigned int)(ptm->tm_wday)) <= 6) {
  286. __memcpy(buffer, at_data + 3 * ptm->tm_wday, 3);
  287. }
  288. if (((unsigned int)(ptm->tm_mon)) <= 11) {
  289. __memcpy(buffer + 4, at_data + 3*7 + 3 * ptm->tm_mon, 3);
  290. }
  291. #else
  292. assert(((unsigned int)(ptm->tm_wday)) <= 6);
  293. assert(((unsigned int)(ptm->tm_mon)) <= 11);
  294. __memcpy(buffer, at_data + 3*(7 + 12) - 3, sizeof(at_data) + 3 - 3*(7 + 12));
  295. __memcpy(buffer, at_data + 3 * ptm->tm_wday, 3);
  296. __memcpy(buffer + 4, at_data + 3*7 + 3 * ptm->tm_mon, 3);
  297. #endif
  298. #ifdef SAFE_ASCTIME_R
  299. buffer += 19;
  300. tmp = ptm->tm_year + 1900;
  301. if (((unsigned int) tmp) < 10000) {
  302. buffer += 4;
  303. do {
  304. *buffer = '0' + (tmp % 10);
  305. tmp /= 10;
  306. } while (*--buffer == '?');
  307. }
  308. #else /* SAFE_ASCTIME_R */
  309. buffer += 23;
  310. tmp = ptm->tm_year + 1900;
  311. assert( ((unsigned int) tmp) < 10000 );
  312. do {
  313. *buffer = '0' + (tmp % 10);
  314. tmp /= 10;
  315. } while (*--buffer == '?');
  316. #endif /* SAFE_ASCTIME_R */
  317. do {
  318. --buffer;
  319. tmp = *((int *)(((const char *) ptm) + (int) *buffer));
  320. #ifdef SAFE_ASCTIME_R
  321. if (((unsigned int) tmp) >= 100) { /* Just check 2 digit non-neg. */
  322. buffer[-1] = *buffer = '?';
  323. } else
  324. #else /* SAFE_ASCTIME_R */
  325. assert(((unsigned int) tmp) < 100); /* Just check 2 digit non-neg. */
  326. #endif /* SAFE_ASCTIME_R */
  327. {
  328. *buffer = '0' + (tmp % 10);
  329. #ifdef __BCC__
  330. buffer[-1] = '0' + (tmp/10);
  331. #else /* __BCC__ */
  332. buffer[-1] += (tmp/10);
  333. #endif /* __BCC__ */
  334. }
  335. } while ((buffer -= 2)[-2] == '0');
  336. if (*++buffer == '0') { /* Space-pad day of month. */
  337. *buffer = ' ';
  338. }
  339. return buffer - 8;
  340. }
  341. strong_alias(__asctime_r,asctime_r)
  342. #endif
  343. /**********************************************************************/
  344. #ifdef L_clock
  345. #define times __times
  346. #include <sys/times.h>
  347. #ifndef __BCC__
  348. #if CLOCKS_PER_SEC != 1000000L
  349. #error unexpected value for CLOCKS_PER_SEC!
  350. #endif
  351. #endif
  352. #ifdef __UCLIBC_CLK_TCK_CONST
  353. # if __UCLIBC_CLK_TCK_CONST > CLOCKS_PER_SEC
  354. # error __UCLIBC_CLK_TCK_CONST > CLOCKS_PER_SEC!
  355. # elif __UCLIBC_CLK_TCK_CONST < 1
  356. # error __UCLIBC_CLK_TCK_CONST < 1!
  357. # endif
  358. #endif
  359. /* Note: SUSv3 notes
  360. *
  361. * On XSI-conformant systems, CLOCKS_PER_SEC is defined to be one million.
  362. *
  363. * The value returned by clock() may wrap around on some implementations.
  364. * For example, on a machine with 32-bit values for clock_t, it wraps
  365. * after 2147 seconds.
  366. *
  367. * This implies that we should bitwise and with LONG_MAX.
  368. */
  369. clock_t clock(void)
  370. {
  371. struct tms xtms;
  372. unsigned long t;
  373. times(&xtms);
  374. t = ((unsigned long) xtms.tms_utime) + xtms.tms_stime;
  375. #ifndef __UCLIBC_CLK_TCK_CONST
  376. # error __UCLIBC_CLK_TCK_CONST not defined!
  377. #elif ((CLOCKS_PER_SEC % __UCLIBC_CLK_TCK_CONST) == 0)
  378. /* CLOCKS_PER_SEC == k * __UCLIBC_CLK_TCK_CONST for some integer k >= 1. */
  379. return ((t * (CLOCKS_PER_SEC/__UCLIBC_CLK_TCK_CONST)) & LONG_MAX);
  380. #else
  381. /* Unlike the previous case, the scaling factor is not an integer.
  382. * So when tms_utime, tms_stime, or their sum wraps, some of the
  383. * "visible" bits in the return value are affected. Nothing we
  384. * can really do about this though other than handle tms_utime and
  385. * tms_stime seperately and then sum. But since that doesn't really
  386. * buy us much, we don't bother. */
  387. return ((((t / __UCLIBC_CLK_TCK_CONST) * CLOCKS_PER_SEC)
  388. + ((((t % __UCLIBC_CLK_TCK_CONST) * CLOCKS_PER_SEC)
  389. / __UCLIBC_CLK_TCK_CONST))
  390. ) & LONG_MAX);
  391. #endif
  392. }
  393. #endif
  394. /**********************************************************************/
  395. #ifdef L_ctime
  396. char attribute_hidden *__ctime(const time_t *clock)
  397. {
  398. /* ANSI/ISO/SUSv3 say that ctime is equivalent to the following. */
  399. return __asctime(__localtime(clock));
  400. }
  401. strong_alias(__ctime,ctime)
  402. #endif
  403. /**********************************************************************/
  404. #ifdef L_ctime_r
  405. char *ctime_r(const time_t *clock, char *buf)
  406. {
  407. struct tm xtm;
  408. return __asctime_r(__localtime_r(clock, &xtm), buf);
  409. }
  410. #endif
  411. /**********************************************************************/
  412. #ifdef L_difftime
  413. #include <float.h>
  414. #if FLT_RADIX != 2
  415. #error difftime implementation assumptions violated for you arch!
  416. #endif
  417. double difftime(time_t time1, time_t time0)
  418. {
  419. #if (LONG_MAX >> DBL_MANT_DIG) == 0
  420. /* time_t fits in the mantissa of a double. */
  421. return ((double) time1) - time0;
  422. #elif ((LONG_MAX >> DBL_MANT_DIG) >> DBL_MANT_DIG) == 0
  423. /* time_t can overflow the mantissa of a double. */
  424. time_t t1, t0, d;
  425. d = ((time_t) 1) << DBL_MANT_DIG;
  426. t1 = time1 / d;
  427. time1 -= (t1 * d);
  428. t0 = time0 / d;
  429. time0 -= (t0*d);
  430. /* Since FLT_RADIX==2 and d is a power of 2, the only possible
  431. * rounding error in the expression below would occur from the
  432. * addition. */
  433. return (((double) t1) - t0) * d + (((double) time1) - time0);
  434. #else
  435. #error difftime needs special implementation on your arch.
  436. #endif
  437. }
  438. #endif
  439. /**********************************************************************/
  440. #ifdef L_gmtime
  441. struct tm *gmtime(const time_t *timer)
  442. {
  443. register struct tm *ptm = &__time_tm;
  444. _time_t2tm(timer, 0, ptm); /* Can return NULL... */
  445. return ptm;
  446. }
  447. #endif
  448. /**********************************************************************/
  449. #ifdef L_gmtime_r
  450. struct tm *gmtime_r(const time_t *__restrict timer,
  451. struct tm *__restrict result)
  452. {
  453. return _time_t2tm(timer, 0, result);
  454. }
  455. #endif
  456. /**********************************************************************/
  457. #ifdef L_localtime
  458. struct tm attribute_hidden *__localtime(const time_t *timer)
  459. {
  460. register struct tm *ptm = &__time_tm;
  461. /* In this implementation, tzset() is called by localtime_r(). */
  462. __localtime_r(timer, ptm); /* Can return NULL... */
  463. return ptm;
  464. }
  465. strong_alias(__localtime,localtime)
  466. #endif
  467. /**********************************************************************/
  468. #ifdef L_localtime_r
  469. struct tm attribute_hidden *__localtime_r(register const time_t *__restrict timer,
  470. register struct tm *__restrict result)
  471. {
  472. TZLOCK;
  473. tzset();
  474. __time_localtime_tzi(timer, result, _time_tzinfo);
  475. TZUNLOCK;
  476. return result;
  477. }
  478. strong_alias(__localtime_r,localtime_r)
  479. #endif
  480. /**********************************************************************/
  481. #ifdef L__time_localtime_tzi
  482. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  483. struct ll_tzname_item;
  484. typedef struct ll_tzname_item {
  485. struct ll_tzname_item *next;
  486. char tzname[TZNAME_MAX+1];
  487. } ll_tzname_item_t;
  488. static ll_tzname_item_t ll_tzname[] = {
  489. { ll_tzname + 1, "UTC" }, /* Always 1st. */
  490. { NULL, "???" } /* Always 2nd. (invalid or out-of-memory) */
  491. };
  492. static const char *lookup_tzname(const char *key)
  493. {
  494. ll_tzname_item_t *p;
  495. for (p=ll_tzname ; p ; p=p->next) {
  496. if (!__strcmp(p->tzname, key)) {
  497. return p->tzname;
  498. }
  499. }
  500. /* Hmm... a new name. */
  501. if (strnlen(key, TZNAME_MAX+1) < TZNAME_MAX+1) { /* Verify legal length */
  502. if ((p = malloc(sizeof(ll_tzname_item_t))) != NULL) {
  503. /* Insert as 3rd item in the list. */
  504. p->next = ll_tzname[1].next;
  505. ll_tzname[1].next = p;
  506. __strcpy(p->tzname, key);
  507. return p->tzname;
  508. }
  509. }
  510. /* Either invalid or couldn't alloc. */
  511. return ll_tzname[1].tzname;
  512. }
  513. #endif /* __UCLIBC_HAS_TM_EXTENSIONS__ */
  514. static const unsigned char day_cor[] = { /* non-leap */
  515. 31, 31, 34, 34, 35, 35, 36, 36, 36, 37, 37, 38, 38
  516. /* 0, 0, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 7 */
  517. /* 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 */
  518. };
  519. /* Note: timezone locking is done by localtime_r. */
  520. static int tm_isdst(register const struct tm *__restrict ptm,
  521. register rule_struct *r)
  522. {
  523. long sec;
  524. int i, isdst, isleap, day, day0, monlen, mday;
  525. int oday; /* Note: oday can be uninitialized. */
  526. isdst = 0;
  527. if (r[1].tzname[0] != 0) {
  528. /* First, get the current seconds offset from the start of the year.
  529. * Fields of ptm are assumed to be in their normal ranges. */
  530. sec = ptm->tm_sec
  531. + 60 * (ptm->tm_min
  532. + 60 * (long)(ptm->tm_hour
  533. + 24 * ptm->tm_yday));
  534. /* Do some prep work. */
  535. i = (ptm->tm_year % 400) + 1900; /* Make sure we don't overflow. */
  536. isleap = __isleap(i);
  537. --i;
  538. day0 = (1
  539. + i /* Normal years increment 1 wday. */
  540. + (i/4)
  541. - (i/100)
  542. + (i/400) ) % 7;
  543. i = 0;
  544. do {
  545. day = r->day; /* Common for 'J' and # case. */
  546. if (r->rule_type == 'J') {
  547. if (!isleap || (day < (31+29))) {
  548. --day;
  549. }
  550. } else if (r->rule_type == 'M') {
  551. /* Find 0-based day number for 1st of the month. */
  552. day = 31*r->month - day_cor[r->month -1];
  553. if (isleap && (day >= 59)) {
  554. ++day;
  555. }
  556. monlen = 31 + day_cor[r->month -1] - day_cor[r->month];
  557. if (isleap && (r->month > 1)) {
  558. ++monlen;
  559. }
  560. /* Wweekday (0 is Sunday) of 1st of the month
  561. * is (day0 + day) % 7. */
  562. if ((mday = r->day - ((day0 + day) % 7)) >= 0) {
  563. mday -= 7; /* Back up into prev month since r->week>0. */
  564. }
  565. if ((mday += 7 * r->week) >= monlen) {
  566. mday -= 7;
  567. }
  568. /* So, 0-based day number is... */
  569. day += mday;
  570. }
  571. if (i != 0) {
  572. /* Adjust sec since dst->std change time is in dst. */
  573. sec += (r[-1].gmt_offset - r->gmt_offset);
  574. if (oday > day) {
  575. ++isdst; /* Year starts in dst. */
  576. }
  577. }
  578. oday = day;
  579. /* Now convert day to seconds and add offset and compare. */
  580. if (sec >= (day * 86400L) + r->dst_offset) {
  581. ++isdst;
  582. }
  583. ++r;
  584. } while (++i < 2);
  585. }
  586. return (isdst & 1);
  587. }
  588. struct tm attribute_hidden *__time_localtime_tzi(register const time_t *__restrict timer,
  589. register struct tm *__restrict result,
  590. rule_struct *tzi)
  591. {
  592. time_t x[1];
  593. long offset;
  594. int days, dst;
  595. dst = 0;
  596. do {
  597. days = -7;
  598. offset = 604800L - tzi[dst].gmt_offset;
  599. if (*timer > (LONG_MAX - 604800L)) {
  600. days = -days;
  601. offset = -offset;
  602. }
  603. *x = *timer + offset;
  604. _time_t2tm(x, days, result);
  605. result->tm_isdst = dst;
  606. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  607. result->tm_gmtoff = - tzi[dst].gmt_offset;
  608. result->tm_zone = lookup_tzname(tzi[dst].tzname);
  609. #endif /* __UCLIBC_HAS_TM_EXTENSIONS__ */
  610. } while ((++dst < 2)
  611. && ((result->tm_isdst = tm_isdst(result, tzi)) != 0));
  612. return result;
  613. }
  614. #endif
  615. /**********************************************************************/
  616. #ifdef L_mktime
  617. time_t mktime(struct tm *timeptr)
  618. {
  619. return _time_mktime(timeptr, 1);
  620. }
  621. /* Another name for `mktime'. */
  622. /* time_t timelocal(struct tm *tp) */
  623. weak_alias(mktime,timelocal);
  624. #endif
  625. /**********************************************************************/
  626. #ifdef L_timegm
  627. /* Like `mktime' but timeptr represents Universal Time, not local time. */
  628. time_t timegm(struct tm *timeptr)
  629. {
  630. rule_struct gmt_tzinfo[2];
  631. __memset(gmt_tzinfo, 0, sizeof(gmt_tzinfo));
  632. __strcpy(gmt_tzinfo[0].tzname, "GMT"); /* Match glibc behavior here. */
  633. return _time_mktime_tzi(timeptr, 1, gmt_tzinfo);
  634. }
  635. #endif
  636. /**********************************************************************/
  637. #if defined(L_strftime) || defined(L_strftime_l)
  638. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  639. size_t attribute_hidden __strftime(char *__restrict s, size_t maxsize,
  640. const char *__restrict format,
  641. const struct tm *__restrict timeptr)
  642. {
  643. return __strftime_l(s, maxsize, format, timeptr, __UCLIBC_CURLOCALE);
  644. }
  645. strong_alias(__strftime,strftime)
  646. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  647. #define NO_E_MOD 0x80
  648. #define NO_O_MOD 0x40
  649. #define ILLEGAL_SPEC 0x3f
  650. #define INT_SPEC 0x00 /* must be 0x00!! */
  651. #define STRING_SPEC 0x10 /* must be 0x10!! */
  652. #define CALC_SPEC 0x20
  653. #define STACKED_SPEC 0x30
  654. #define MASK_SPEC 0x30
  655. /* Compatibility:
  656. *
  657. * No alternate digit (%O?) handling. Always uses 0-9.
  658. * Alternate locale format (%E?) handling is broken for nontrivial ERAs.
  659. * glibc's %P is currently faked by %p. This means it doesn't do lower case.
  660. * glibc's %k, %l, and %s are handled.
  661. * glibc apparently allows (and ignores) extraneous 'E' and 'O' modifiers,
  662. * while they are flagged as illegal conversions here.
  663. */
  664. /* Warning: Assumes ASCII values! (as do lots of other things in the lib...) */
  665. static const unsigned char spec[] = {
  666. /* A */ 0x03 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  667. /* B */ 0x04 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  668. /* C */ 0x0a | INT_SPEC | NO_O_MOD,
  669. /* D */ 0x02 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  670. /* E */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  671. /* F */ 0x03 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  672. /* G */ 0x03 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  673. /* H */ 0x0b | INT_SPEC | NO_E_MOD,
  674. /* I */ 0x0c | INT_SPEC | NO_E_MOD,
  675. /* J */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  676. /* K */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  677. /* L */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  678. /* M */ 0x0d | INT_SPEC | NO_E_MOD,
  679. /* N */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  680. /* O */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  681. /* P */ 0x05 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* glibc ; use %p */
  682. /* Q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  683. /* R */ 0x04 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  684. /* S */ 0x0e | INT_SPEC | NO_E_MOD,
  685. /* T */ 0x05 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  686. /* U */ 0x04 | CALC_SPEC | NO_E_MOD,
  687. /* V */ 0x05 | CALC_SPEC | NO_E_MOD,
  688. /* W */ 0x06 | CALC_SPEC | NO_E_MOD,
  689. /* X */ 0x0a | STACKED_SPEC | NO_O_MOD,
  690. /* Y */ 0x0f | INT_SPEC | NO_O_MOD,
  691. /* Z */ 0x01 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  692. '?', /* 26 */
  693. '?', /* 27 */
  694. '?', /* 28 */
  695. '?', /* 29 */
  696. 0, /* 30 */
  697. 0, /* 31 */
  698. /* a */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  699. /* b */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  700. /* c */ 0x08 | STACKED_SPEC | NO_O_MOD,
  701. /* d */ 0x00 | INT_SPEC | NO_E_MOD,
  702. /* e */ 0x01 | INT_SPEC | NO_E_MOD,
  703. /* f */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  704. /* g */ 0x02 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  705. /* h */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* same as b */
  706. /* i */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  707. /* j */ 0x08 | INT_SPEC | NO_E_MOD | NO_O_MOD,
  708. /* k */ 0x03 | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  709. /* l */ 0x04 | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  710. /* m */ 0x05 | INT_SPEC | NO_E_MOD,
  711. /* n */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  712. /* o */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  713. /* p */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  714. /* q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  715. /* r */ 0x0b | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  716. /* s */ 0x07 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  717. /* t */ 0x01 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  718. /* u */ 0x07 | INT_SPEC | NO_E_MOD,
  719. /* v */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  720. /* w */ 0x02 | INT_SPEC | NO_E_MOD,
  721. /* x */ 0x09 | STACKED_SPEC | NO_O_MOD,
  722. /* y */ 0x09 | INT_SPEC,
  723. /* z */ 0x00 | CALC_SPEC | NO_E_MOD | NO_O_MOD,
  724. /* WARNING!!! These are dependent on the layout of struct tm!!! */
  725. #define FIELD_MAX (26+6+26)
  726. 60 /* 61? */, 59, 23, 31, 11, 0 /* 9999 */, 6, 0 /* 365 */,
  727. #define TP_OFFSETS (FIELD_MAX+8)
  728. 3, /* d */
  729. 3, /* e */
  730. 6, /* w */
  731. 2, /* k */
  732. 2, /* l */
  733. 4, /* m */
  734. 0, /* CURRENTLY UNUSED */
  735. /* NOTE: u,j,y order must be preserved as 6,7,5 seq is used in the code! */
  736. #define CALC_OFFSETS (TP_OFFSETS + 7)
  737. 6, /* u */
  738. 7, /* j */
  739. 5, /* y */
  740. 5, /* C */
  741. 2, /* H */
  742. 2, /* I */
  743. 1, /* M */
  744. 0, /* S */
  745. 5, /* Y */
  746. 6, /* a */
  747. 4, /* b, h */
  748. 2, /* p */
  749. 6, /* A */
  750. 4, /* B */
  751. 2, /* P */
  752. #define TP_CODES (TP_OFFSETS + 16 + 6)
  753. 2 | 16, /* d */
  754. 2, /* e */
  755. 0 | 16, /* w */
  756. 2, /* k */
  757. 2 | 32 | 0, /* l */
  758. 2 | 16 | 1, /* m */
  759. 0, /* CURRENTLY UNUSED */
  760. 0 | 16 | 8 , /* u */
  761. 4 | 16 | 1, /* j */
  762. 2 | 128 | 32 | 16 , /* y */
  763. 2 | 128 | 64 | 32 | 16 , /* C */
  764. 2 | 16, /* H */
  765. 2 | 32 | 16 | 0, /* I */
  766. 2 | 16, /* M */
  767. 2 | 16, /* S */
  768. 6 | 16, /* Y */
  769. 2, /* a */
  770. 2, /* b, h */
  771. 2 | 64, /* p */
  772. 2, /* A */
  773. 2, /* B */
  774. 2 | 64, /* P */
  775. #define STRINGS_NL_ITEM_START (TP_CODES + 16 + 6)
  776. _NL_ITEM_INDEX(ABDAY_1), /* a */
  777. _NL_ITEM_INDEX(ABMON_1), /* b, h */
  778. _NL_ITEM_INDEX(AM_STR), /* p */
  779. _NL_ITEM_INDEX(DAY_1), /* A */
  780. _NL_ITEM_INDEX(MON_1), /* B */
  781. _NL_ITEM_INDEX(AM_STR), /* P -- wrong! need lower case */
  782. #define STACKED_STRINGS_START (STRINGS_NL_ITEM_START+6)
  783. 6, 7, 8, 16, 24, 29, /* 6 - offsets from offset-count to strings */
  784. '\n', 0, /* 2 */
  785. '\t', 0, /* 2 */
  786. '%', 'm', '/', '%', 'd', '/', '%', 'y', 0, /* 9 - %D */
  787. '%', 'Y', '-', '%', 'm', '-', '%', 'd', 0, /* 9 - %F (glibc extension) */
  788. '%', 'H', ':', '%', 'M', 0, /* 6 - %R*/
  789. '%', 'H', ':', '%', 'M', ':', '%', 'S', 0, /* 9 - %T */
  790. #define STACKED_STRINGS_NL_ITEM_START (STACKED_STRINGS_START + 43)
  791. _NL_ITEM_INDEX(D_T_FMT), /* c */
  792. _NL_ITEM_INDEX(D_FMT), /* x */
  793. _NL_ITEM_INDEX(T_FMT), /* X */
  794. _NL_ITEM_INDEX(T_FMT_AMPM), /* r */
  795. #ifdef ENABLE_ERA_CODE
  796. _NL_ITEM_INDEX(ERA_D_T_FMT), /* Ec */
  797. _NL_ITEM_INDEX(ERA_D_FMT), /* Ex */
  798. _NL_ITEM_INDEX(ERA_T_FMT), /* EX */
  799. #endif
  800. };
  801. static int load_field(int k, const struct tm *__restrict timeptr)
  802. {
  803. int r;
  804. int r_max;
  805. r = ((int *) timeptr)[k];
  806. r_max = spec[FIELD_MAX + k];
  807. if (k == 7) {
  808. r_max = 365;
  809. } else if (k == 5) {
  810. r += 1900;
  811. r_max = 9999;
  812. }
  813. if ((((unsigned int) r) > r_max) || ((k == 3) && !r)) {
  814. r = -1;
  815. }
  816. return r;
  817. }
  818. #define MAX_PUSH 4
  819. #ifdef __UCLIBC_MJN3_ONLY__
  820. #warning TODO: Check multibyte format string validity.
  821. #endif
  822. size_t attribute_hidden __UCXL(strftime)(char *__restrict s, size_t maxsize,
  823. const char *__restrict format,
  824. const struct tm *__restrict timeptr __LOCALE_PARAM )
  825. {
  826. long tzo;
  827. register const char *p;
  828. register const char *o;
  829. #ifndef __UCLIBC_HAS_TM_EXTENSIONS__
  830. const rule_struct *rsp;
  831. #endif
  832. const char *stack[MAX_PUSH];
  833. size_t count;
  834. size_t o_count;
  835. int field_val, i, j, lvl;
  836. int x[3]; /* wday, yday, year */
  837. int isofm, days;
  838. char buf[__UIM_BUFLEN_LONG];
  839. unsigned char mod;
  840. unsigned char code;
  841. tzset(); /* We'll, let's get this out of the way. */
  842. lvl = 0;
  843. p = format;
  844. count = maxsize;
  845. LOOP:
  846. if (!count) {
  847. return 0;
  848. }
  849. if (!*p) {
  850. if (lvl == 0) {
  851. *s = 0; /* nul-terminate */
  852. return maxsize - count;
  853. }
  854. p = stack[--lvl];
  855. goto LOOP;
  856. }
  857. o_count = 1;
  858. if ((*(o = p) == '%') && (*++p != '%')) {
  859. o_count = 2;
  860. mod = ILLEGAL_SPEC;
  861. if ((*p == 'O') || (*p == 'E')) { /* modifier */
  862. mod |= ((*p == 'O') ? NO_O_MOD : NO_E_MOD);
  863. ++o_count;
  864. ++p;
  865. }
  866. if ((((unsigned char)(((*p) | 0x20) - 'a')) >= 26)
  867. || (((code = spec[(int)(*p - 'A')]) & mod) >= ILLEGAL_SPEC)
  868. ) {
  869. if (!*p) {
  870. --p;
  871. --o_count;
  872. }
  873. goto OUTPUT;
  874. }
  875. code &= ILLEGAL_SPEC; /* modifiers are preserved in mod var. */
  876. if ((code & MASK_SPEC) == STACKED_SPEC) {
  877. if (lvl == MAX_PUSH) {
  878. goto OUTPUT; /* Stack full so treat as illegal spec. */
  879. }
  880. stack[lvl++] = ++p;
  881. if ((code &= 0xf) < 8) {
  882. p = ((const char *) spec) + STACKED_STRINGS_START + code;
  883. p += *((unsigned char *)p);
  884. goto LOOP;
  885. }
  886. p = ((const char *) spec) + STACKED_STRINGS_NL_ITEM_START
  887. + (code & 7);
  888. #ifdef ENABLE_ERA_CODE
  889. if ((mod & NO_E_MOD) /* Actually, this means E modifier present. */
  890. && (*(o = __XL(nl_langinfo)(_NL_ITEM(LC_TIME,
  891. (int)(((unsigned char *)p)[4]))
  892. __LOCALE_ARG
  893. )))
  894. ) {
  895. p = o;
  896. goto LOOP;
  897. }
  898. #endif
  899. p = __XL(nl_langinfo)(_NL_ITEM(LC_TIME,
  900. (int)(*((unsigned char *)p)))
  901. __LOCALE_ARG
  902. );
  903. goto LOOP;
  904. }
  905. o = spec + 26; /* set to "????" */
  906. if ((code & MASK_SPEC) == CALC_SPEC) {
  907. if (*p == 's') {
  908. time_t t;
  909. /* Use a cast to silence the warning since *timeptr won't
  910. * be changed. */
  911. if ((t = _time_mktime((struct tm *) timeptr, 0))
  912. == ((time_t) -1)
  913. ) {
  914. o_count = 1;
  915. goto OUTPUT;
  916. }
  917. #ifdef TIME_T_IS_UNSIGNED
  918. o = _uintmaxtostr(buf + sizeof(buf) - 1,
  919. (uintmax_t) t,
  920. 10, __UIM_DECIMAL);
  921. #else
  922. o = _uintmaxtostr(buf + sizeof(buf) - 1,
  923. (uintmax_t) t,
  924. -10, __UIM_DECIMAL);
  925. #endif
  926. o_count = sizeof(buf);
  927. goto OUTPUT;
  928. } else if (((*p) | 0x20) == 'z') { /* 'z' or 'Z' */
  929. if (timeptr->tm_isdst < 0) {
  930. /* SUSv3 specifies this behavior for 'z', but we'll also
  931. * treat it as "no timezone info" for 'Z' too. */
  932. o_count = 0;
  933. goto OUTPUT;
  934. }
  935. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  936. #define RSP_TZUNLOCK ((void) 0)
  937. #define RSP_TZNAME timeptr->tm_zone
  938. #define RSP_GMT_OFFSET (-timeptr->tm_gmtoff)
  939. #else
  940. #define RSP_TZUNLOCK TZUNLOCK
  941. #define RSP_TZNAME rsp->tzname
  942. #define RSP_GMT_OFFSET rsp->gmt_offset
  943. TZLOCK;
  944. rsp = _time_tzinfo;
  945. if (timeptr->tm_isdst > 0) {
  946. ++rsp;
  947. }
  948. #endif
  949. if (*p == 'Z') {
  950. o = RSP_TZNAME;
  951. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  952. /* Sigh... blasted glibc extensions. Of course we can't
  953. * count on the pointer being valid. Best we can do is
  954. * handle NULL, which looks to be all that glibc does.
  955. * At least that catches the memset() with 0 case.
  956. * NOTE: We handle this case differently than glibc!
  957. * It uses system timezone name (based on tm_isdst) in this
  958. * case... although it always seems to use the embedded
  959. * tm_gmtoff value. What we'll do instead is treat the
  960. * timezone name as unknown/invalid and return "???". */
  961. if (!o) {
  962. o = "???";
  963. }
  964. #endif
  965. assert(o != NULL);
  966. #if 0
  967. if (!o) { /* PARANOIA */
  968. o = spec+30; /* empty string */
  969. }
  970. #endif
  971. o_count = SIZE_MAX;
  972. RSP_TZUNLOCK;
  973. goto OUTPUT;
  974. } else { /* z */
  975. *s = '+';
  976. if ((tzo = -RSP_GMT_OFFSET) < 0) {
  977. tzo = -tzo;
  978. *s = '-';
  979. }
  980. RSP_TZUNLOCK;
  981. ++s;
  982. --count;
  983. i = tzo / 60;
  984. field_val = ((i / 60) * 100) + (i % 60);
  985. i = 16 + 6; /* 0-fill, width = 4 */
  986. }
  987. } else {
  988. /* TODO: don't need year for U, W */
  989. for (i=0 ; i < 3 ; i++) {
  990. if ((x[i] = load_field(spec[CALC_OFFSETS+i],timeptr)) < 0) {
  991. goto OUTPUT;
  992. }
  993. }
  994. i = 16 + 2; /* 0-fill, width = 2 */
  995. if ((*p == 'U') || (*p == 'W')) {
  996. field_val = ((x[1] - x[0]) + 7);
  997. if (*p == 'W') {
  998. ++field_val;
  999. }
  1000. field_val /= 7;
  1001. if ((*p == 'W') && !x[0]) {
  1002. --field_val;
  1003. }
  1004. } else { /* ((*p == 'g') || (*p == 'G') || (*p == 'V')) */
  1005. ISO_LOOP:
  1006. isofm = (((x[1] - x[0]) + 11) % 7) - 3; /* [-3,3] */
  1007. if (x[1] < isofm) { /* belongs to previous year */
  1008. --x[2];
  1009. x[1] += 365 + __isleap(x[2]);
  1010. goto ISO_LOOP;
  1011. }
  1012. field_val = ((x[1] - isofm) / 7) + 1; /* week # */
  1013. days = 365 + __isleap(x[2]);
  1014. isofm = ((isofm + 7*53 + 3 - days)) %7 + days - 3; /* next year */
  1015. if (x[1] >= isofm) { /* next year */
  1016. x[1] -= days;
  1017. ++x[2];
  1018. goto ISO_LOOP;
  1019. }
  1020. if (*p != 'V') { /* need year */
  1021. field_val = x[2]; /* TODO: what if x[2] now 10000 ?? */
  1022. if (*p == 'g') {
  1023. field_val %= 100;
  1024. } else {
  1025. i = 16 + 6; /* 0-fill, width = 4 */
  1026. }
  1027. }
  1028. }
  1029. }
  1030. } else {
  1031. i = TP_OFFSETS + (code & 0x1f);
  1032. if ((field_val = load_field(spec[i],timeptr)) < 0) {
  1033. goto OUTPUT;
  1034. }
  1035. i = spec[i+(TP_CODES - TP_OFFSETS)];
  1036. j = (i & 128) ? 100: 12;
  1037. if (i & 64) {
  1038. field_val /= j;;
  1039. }
  1040. if (i & 32) {
  1041. field_val %= j;
  1042. if (((i&128) + field_val) == 0) { /* mod 12? == 0 */
  1043. field_val = j; /* set to 12 */
  1044. }
  1045. }
  1046. field_val += (i & 1);
  1047. if ((i & 8) && !field_val) {
  1048. field_val += 7;
  1049. }
  1050. }
  1051. if ((code & MASK_SPEC) == STRING_SPEC) {
  1052. o_count = SIZE_MAX;
  1053. field_val += spec[STRINGS_NL_ITEM_START + (code & 0xf)];
  1054. o = __XL(nl_langinfo)(_NL_ITEM(LC_TIME, field_val) __LOCALE_ARG );
  1055. } else {
  1056. o_count = ((i >> 1) & 3) + 1;
  1057. o = buf + o_count;
  1058. do {
  1059. *(char *)(--o) = '0' + (field_val % 10);
  1060. field_val /= 10;
  1061. } while (o > buf);
  1062. if (*buf == '0') {
  1063. *buf = ' ' + (i & 16);
  1064. }
  1065. }
  1066. }
  1067. OUTPUT:
  1068. ++p;
  1069. while (o_count && count && *o) {
  1070. *s++ = *o++;
  1071. --o_count;
  1072. --count;
  1073. }
  1074. goto LOOP;
  1075. }
  1076. __UCXL_ALIAS(strftime)
  1077. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  1078. #endif
  1079. /**********************************************************************/
  1080. #if defined(L_strptime) || defined(L_strptime_l)
  1081. #define ISDIGIT(C) __isdigit_char((C))
  1082. #ifdef __UCLIBC_DO_XLOCALE
  1083. #define ISSPACE(C) isspace_l((C), locale_arg)
  1084. #else
  1085. #define ISSPACE(C) isspace((C))
  1086. #endif
  1087. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  1088. char attribute_hidden *__strptime(const char *__restrict buf, const char *__restrict format,
  1089. struct tm *__restrict tm)
  1090. {
  1091. return __strptime_l(buf, format, tm, __UCLIBC_CURLOCALE);
  1092. }
  1093. strong_alias(__strptime,strptime)
  1094. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  1095. /* TODO:
  1096. * 1) %l and %k are space-padded, so "%l" by itself fails while " %l" succeeds.
  1097. * Both work for glibc. So, should we always strip spaces?
  1098. * 2) %Z
  1099. */
  1100. /* Notes:
  1101. * There are several differences between this strptime and glibc's strptime.
  1102. * 1) glibc strips leading space before numeric conversions.
  1103. * 2) glibc will read fields without whitespace in between. SUSv3 states
  1104. * that you must have whitespace between conversion operators. Besides,
  1105. * how do you know how long a number should be if there are leading 0s?
  1106. * 3) glibc attempts to compute some the struct tm fields based on the
  1107. * data retrieved; tm_wday in particular. I don't as I consider it
  1108. * another glibc attempt at mind-reading...
  1109. */
  1110. #define NO_E_MOD 0x80
  1111. #define NO_O_MOD 0x40
  1112. #define ILLEGAL_SPEC 0x3f
  1113. #define INT_SPEC 0x00 /* must be 0x00!! */
  1114. #define STRING_SPEC 0x10 /* must be 0x10!! */
  1115. #define CALC_SPEC 0x20
  1116. #define STACKED_SPEC 0x30
  1117. #define MASK_SPEC 0x30
  1118. /* Warning: Assumes ASCII values! (as do lots of other things in the lib...) */
  1119. static const unsigned char spec[] = {
  1120. /* A */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1121. /* B */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1122. /* C */ 0x08 | INT_SPEC | NO_O_MOD,
  1123. /* D */ 0x01 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1124. /* E */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1125. /* F */ 0x02 | STACKED_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1126. /* G */ 0x0f | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1127. /* H */ 0x06 | INT_SPEC | NO_E_MOD,
  1128. /* I */ 0x07 | INT_SPEC | NO_E_MOD,
  1129. /* J */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1130. /* K */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1131. /* L */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1132. /* M */ 0x04 | INT_SPEC | NO_E_MOD,
  1133. /* N */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1134. /* O */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1135. /* P */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1136. /* Q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1137. /* R */ 0x03 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1138. /* S */ 0x05 | INT_SPEC | NO_E_MOD,
  1139. /* T */ 0x04 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1140. /* U */ 0x0c | INT_SPEC | NO_E_MOD,
  1141. /* V */ 0x0d | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1142. /* W */ 0x0c | INT_SPEC | NO_E_MOD,
  1143. /* X */ 0x0a | STACKED_SPEC | NO_O_MOD,
  1144. /* Y */ 0x0a | INT_SPEC | NO_O_MOD,
  1145. /* Z */ 0x02 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1146. /* WARNING! This assumes orderings:
  1147. * AM,PM
  1148. * ABDAY_1-ABDAY-7,DAY_1-DAY_7
  1149. * ABMON_1-ABMON_12,MON_1-MON12
  1150. * Also, there are exactly 6 bytes between 'Z' and 'a'.
  1151. */
  1152. #define STRINGS_NL_ITEM_START (26)
  1153. _NL_ITEM_INDEX(AM_STR), /* p (P) */
  1154. _NL_ITEM_INDEX(ABMON_1), /* B, b */
  1155. _NL_ITEM_INDEX(ABDAY_1), /* A, a */
  1156. 2,
  1157. 24,
  1158. 14,
  1159. /* a */ 0x02 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1160. /* b */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1161. /* c */ 0x08 | STACKED_SPEC | NO_O_MOD,
  1162. /* d */ 0x00 | INT_SPEC | NO_E_MOD,
  1163. /* e */ 0x00 | INT_SPEC | NO_E_MOD,
  1164. /* f */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1165. /* g */ 0x0e | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1166. /* h */ 0x01 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1167. /* i */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1168. /* j */ 0x01 | INT_SPEC | NO_E_MOD | NO_O_MOD,
  1169. /* k */ 0x06 | INT_SPEC | NO_E_MOD, /* glibc */
  1170. /* l */ 0x07 | INT_SPEC | NO_E_MOD, /* glibc */
  1171. /* m */ 0x02 | INT_SPEC | NO_E_MOD,
  1172. /* n */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1173. /* o */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1174. /* p */ 0x00 | STRING_SPEC | NO_E_MOD | NO_O_MOD,
  1175. /* q */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1176. /* r */ 0x0b | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1177. /* s */ 0x00 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1178. /* t */ 0x00 | STACKED_SPEC | NO_E_MOD | NO_O_MOD,
  1179. /* u */ 0x0b | INT_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1180. /* v */ ILLEGAL_SPEC | NO_E_MOD | NO_O_MOD,
  1181. /* w */ 0x03 | INT_SPEC | NO_E_MOD,
  1182. /* x */ 0x09 | STACKED_SPEC | NO_O_MOD,
  1183. /* y */ 0x09 | INT_SPEC,
  1184. /* z */ 0x01 | CALC_SPEC | NO_E_MOD | NO_O_MOD, /* glibc */
  1185. #define INT_FIELD_START (26+6+26)
  1186. /* (field #) << 3 + lower bound (0|1) + correction 0:none, 2:-1, 4:-1900
  1187. * followed by upper bound prior to correction with 1=>366 and 2=>9999. */
  1188. /* d, e */ (3 << 3) + 1 + 0, 31,
  1189. /* j */ (7 << 3) + 1 + 2, /* 366 */ 1,
  1190. /* m */ (4 << 3) + 1 + 2, 12,
  1191. /* w */ (6 << 3) + 0 + 0, 6,
  1192. /* M */ (1 << 3) + 0 + 0, 59,
  1193. /* S */ 0 + 0 + 0, 60,
  1194. /* H (k) */ (2 << 3) + 0 + 0, 23,
  1195. /* I (l) */ (9 << 3) + 1 + 0, 12, /* goes with 8 -- am/pm */
  1196. /* C */ (10<< 3) + 0 + 0, 99,
  1197. /* y */ (11<< 3) + 0 + 0, 99,
  1198. /* Y */ (5 << 3) + 0 + 4, /* 9999 */ 2,
  1199. /* u */ (6 << 3) + 1 + 0, 7,
  1200. /* The following are processed and range-checked, but ignored otherwise. */
  1201. /* U, W */ (12<< 3) + 0 + 0, 53,
  1202. /* V */ (12<< 3) + 1 + 0, 53,
  1203. /* g */ (12<< 3) + 0 + 0, 99,
  1204. /* G */ (12<< 3) + 0 /*+ 4*/, /* 9999 */ 2, /* Note: -1 or 10000? */
  1205. #define STACKED_STRINGS_START (INT_FIELD_START+32)
  1206. 5, 6, 14, 22, 27, /* 5 - offsets from offset-count to strings */
  1207. ' ', 0, /* 2 - %n or %t */
  1208. '%', 'm', '/', '%', 'd', '/', '%', 'y', 0, /* 9 - %D */
  1209. '%', 'Y', '-', '%', 'm', '-', '%', 'd', 0, /* 9 - %F (glibc extension) */
  1210. '%', 'H', ':', '%', 'M', 0, /* 6 - %R*/
  1211. '%', 'H', ':', '%', 'M', ':', '%', 'S', 0, /* 9 - %T */
  1212. #define STACKED_STRINGS_NL_ITEM_START (STACKED_STRINGS_START + 40)
  1213. _NL_ITEM_INDEX(D_T_FMT), /* c */
  1214. _NL_ITEM_INDEX(D_FMT), /* x */
  1215. _NL_ITEM_INDEX(T_FMT), /* X */
  1216. _NL_ITEM_INDEX(T_FMT_AMPM), /* r */
  1217. #ifdef ENABLE_ERA_CODE
  1218. _NL_ITEM_INDEX(ERA_D_T_FMT), /* Ec */
  1219. _NL_ITEM_INDEX(ERA_D_FMT), /* Ex */
  1220. _NL_ITEM_INDEX(ERA_T_FMT), /* EX */
  1221. #endif
  1222. };
  1223. #define MAX_PUSH 4
  1224. char attribute_hidden *__UCXL(strptime)(const char *__restrict buf, const char *__restrict format,
  1225. struct tm *__restrict tm __LOCALE_PARAM)
  1226. {
  1227. register const char *p;
  1228. char *o;
  1229. const char *stack[MAX_PUSH];
  1230. int i, j, lvl;
  1231. int fields[13];
  1232. unsigned char mod;
  1233. unsigned char code;
  1234. i = 0;
  1235. do {
  1236. fields[i] = INT_MIN;
  1237. } while (++i < 13);
  1238. lvl = 0;
  1239. p = format;
  1240. LOOP:
  1241. if (!*p) {
  1242. if (lvl == 0) { /* Done. */
  1243. if (fields[6] == 7) { /* Cleanup for %u here since just once. */
  1244. fields[6] = 0; /* Don't use mod in case unset. */
  1245. }
  1246. i = 0;
  1247. do { /* Store the values into tm. */
  1248. if (fields[i] != INT_MIN) {
  1249. ((int *) tm)[i] = fields[i];
  1250. }
  1251. } while (++i < 8);
  1252. return (char *) buf; /* Success. */
  1253. }
  1254. p = stack[--lvl];
  1255. goto LOOP;
  1256. }
  1257. if ((*p == '%') && (*++p != '%')) {
  1258. mod = ILLEGAL_SPEC;
  1259. if ((*p == 'O') || (*p == 'E')) { /* Modifier? */
  1260. mod |= ((*p == 'O') ? NO_O_MOD : NO_E_MOD);
  1261. ++p;
  1262. }
  1263. if (!*p
  1264. || (((unsigned char)(((*p) | 0x20) - 'a')) >= 26)
  1265. || (((code = spec[(int)(*p - 'A')]) & mod) >= ILLEGAL_SPEC)
  1266. ) {
  1267. return NULL; /* Illegal spec. */
  1268. }
  1269. if ((code & MASK_SPEC) == STACKED_SPEC) {
  1270. if (lvl == MAX_PUSH) {
  1271. return NULL; /* Stack full so treat as illegal spec. */
  1272. }
  1273. stack[lvl++] = ++p;
  1274. if ((code &= 0xf) < 8) {
  1275. p = ((const char *) spec) + STACKED_STRINGS_START + code;
  1276. p += *((unsigned char *)p);
  1277. goto LOOP;
  1278. }
  1279. p = ((const char *) spec) + STACKED_STRINGS_NL_ITEM_START
  1280. + (code & 7);
  1281. #ifdef ENABLE_ERA_CODE
  1282. if ((mod & NO_E_MOD) /* Actually, this means E modifier present. */
  1283. && (*(o = __XL(nl_langinfo)(_NL_ITEM(LC_TIME,
  1284. (int)(((unsigned char *)p)[4]))
  1285. __LOCALE_ARG
  1286. )))
  1287. ) {
  1288. p = o;
  1289. goto LOOP;
  1290. }
  1291. #endif
  1292. p = __XL(nl_langinfo)(_NL_ITEM(LC_TIME,
  1293. (int)(*((unsigned char *)p)))
  1294. __LOCALE_ARG );
  1295. goto LOOP;
  1296. }
  1297. ++p;
  1298. if ((code & MASK_SPEC) == STRING_SPEC) {
  1299. code &= 0xf;
  1300. j = spec[STRINGS_NL_ITEM_START + 3 + code];
  1301. i = _NL_ITEM(LC_TIME, spec[STRINGS_NL_ITEM_START + code]);
  1302. /* Go backwards to check full names before abreviations. */
  1303. do {
  1304. --j;
  1305. o = __XL(nl_langinfo)(i+j __LOCALE_ARG);
  1306. if (!__UCXL(strncasecmp)(buf,o,__strlen(o) __LOCALE_ARG) && *o) {
  1307. do { /* Found a match. */
  1308. ++buf;
  1309. } while (*++o);
  1310. if (!code) { /* am/pm */
  1311. fields[8] = j * 12;
  1312. if (fields[9] >= 0) { /* We have a previous %I or %l. */
  1313. fields[2] = fields[9] + fields[8];
  1314. }
  1315. } else { /* day (4) or month (6) */
  1316. fields[2 + (code << 1)]
  1317. = j % (spec[STRINGS_NL_ITEM_START + 3 + code] >> 1);
  1318. }
  1319. goto LOOP;
  1320. }
  1321. } while (j);
  1322. return NULL; /* Failed to match. */
  1323. }
  1324. if ((code & MASK_SPEC) == CALC_SPEC) {
  1325. if ((code &= 0xf) < 1) { /* s or z*/
  1326. time_t t;
  1327. o = (char *) buf;
  1328. i = errno;
  1329. __set_errno(0);
  1330. if (!ISSPACE(*buf)) { /* Signal an error if whitespace. */
  1331. #ifdef TIME_T_IS_UNSIGNED
  1332. t = __XL(strtoul)(buf, &o, 10 __LOCALE_ARG);
  1333. #else
  1334. t = __XL(strtol)(buf, &o, 10 __LOCALE_ARG);
  1335. #endif
  1336. }
  1337. if ((o == buf) || errno) { /* Not a number or overflow. */
  1338. return NULL;
  1339. }
  1340. __set_errno(i); /* Restore errno. */
  1341. buf = o;
  1342. if (!code) { /* s */
  1343. __localtime_r(&t, tm); /* TODO: check for failure? */
  1344. i = 0;
  1345. do { /* Now copy values from tm to fields. */
  1346. fields[i] = ((int *) tm)[i];
  1347. } while (++i < 8);
  1348. }
  1349. }
  1350. /* TODO: glibc treats %Z as a nop. For now, do the same. */
  1351. goto LOOP;
  1352. }
  1353. assert((code & MASK_SPEC) == INT_SPEC);
  1354. {
  1355. register const unsigned char *x;
  1356. code &= 0xf;
  1357. x = spec + INT_FIELD_START + (code << 1);
  1358. if ((j = x[1]) < 3) { /* upper bound (inclusive) */
  1359. j = ((j==1) ? 366 : 9999);
  1360. }
  1361. i = -1;
  1362. while (ISDIGIT(*buf)) {
  1363. if (i < 0) {
  1364. i = 0;
  1365. }
  1366. if ((i = 10*i + (*buf - '0')) > j) { /* Overflow. */
  1367. return NULL;
  1368. }
  1369. ++buf;
  1370. }
  1371. if (i < (*x & 1)) { /* This catches no-digit case too. */
  1372. return NULL;
  1373. }
  1374. if (*x & 2) {
  1375. --i;
  1376. }
  1377. if (*x & 4) {
  1378. i -= 1900;
  1379. }
  1380. if (*x == (9 << 3) + 1 + 0) { /* %I or %l */
  1381. if (i == 12) {
  1382. i = 0;
  1383. }
  1384. if (fields[8] >= 0) { /* We have a previous %p or %P. */
  1385. fields[2] = i + fields[8];
  1386. }
  1387. }
  1388. fields[(*x) >> 3] = i;
  1389. if (((unsigned char)(*x - (10<< 3) + 0 + 0)) <= 8) { /* %C or %y */
  1390. if ((j = fields[10]) < 0) { /* No %C, so i must be %y data. */
  1391. if (i <= 68) { /* Map [0-68] to 2000+i */
  1392. i += 100;
  1393. }
  1394. } else { /* Have %C data, but what about %y? */
  1395. if ((i = fields[11]) < 0) { /* No %y data. */
  1396. i = 0; /* Treat %y val as 0 following glibc's example. */
  1397. }
  1398. i += 100*(j - 19);
  1399. }
  1400. fields[5] = i;
  1401. }
  1402. }
  1403. goto LOOP;
  1404. } else if (ISSPACE(*p)) {
  1405. ++p;
  1406. while (ISSPACE(*buf)) {
  1407. ++buf;
  1408. }
  1409. goto LOOP;
  1410. } else if (*buf++ == *p++) {
  1411. goto LOOP;
  1412. }
  1413. return NULL;
  1414. }
  1415. __UCXL_ALIAS(strptime)
  1416. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  1417. #endif
  1418. /**********************************************************************/
  1419. #ifdef L_time
  1420. #ifndef __BCC__
  1421. #error The uClibc version of time is in sysdeps/linux/common.
  1422. #endif
  1423. time_t time(register time_t *tloc)
  1424. {
  1425. struct timeval tv;
  1426. register struct timeval *p = &tv;
  1427. gettimeofday(p, NULL); /* This should never fail... */
  1428. if (tloc) {
  1429. *tloc = p->tv_sec;
  1430. }
  1431. return p->tv_sec;
  1432. }
  1433. #endif
  1434. /**********************************************************************/
  1435. #ifdef L_tzset
  1436. static const char vals[] = {
  1437. 'T', 'Z', 0, /* 3 */
  1438. 'U', 'T', 'C', 0, /* 4 */
  1439. 25, 60, 60, 1, /* 4 */
  1440. '.', 1, /* M */
  1441. 5, '.', 1,
  1442. 6, 0, 0, /* Note: overloaded for non-M non-J case... */
  1443. 0, 1, 0, /* J */
  1444. ',', 'M', '4', '.', '1', '.', '0',
  1445. ',', 'M', '1', '0', '.', '5', '.', '0', 0
  1446. };
  1447. #define TZ vals
  1448. #define UTC (vals + 3)
  1449. #define RANGE (vals + 7)
  1450. #define RULE (vals + 11 - 1)
  1451. #define DEFAULT_RULES (vals + 22)
  1452. /* Initialize to UTC. */
  1453. int daylight = 0;
  1454. long timezone = 0;
  1455. char *tzname[2] = { (char *) UTC, (char *) (UTC-1) };
  1456. #ifdef __UCLIBC_HAS_THREADS__
  1457. pthread_mutex_t _time_tzlock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  1458. #endif
  1459. rule_struct _time_tzinfo[2];
  1460. static const char *getoffset(register const char *e, long *pn)
  1461. {
  1462. register const char *s = RANGE-1;
  1463. long n;
  1464. int f;
  1465. n = 0;
  1466. f = -1;
  1467. do {
  1468. ++s;
  1469. if (__isdigit_char(*e)) {
  1470. f = *e++ - '0';
  1471. }
  1472. if (__isdigit_char(*e)) {
  1473. f = 10 * f + (*e++ - '0');
  1474. }
  1475. if (((unsigned int)f) >= *s) {
  1476. return NULL;
  1477. }
  1478. n = (*s) * n + f;
  1479. f = 0;
  1480. if (*e == ':') {
  1481. ++e;
  1482. --f;
  1483. }
  1484. } while (*s > 1);
  1485. *pn = n;
  1486. return e;
  1487. }
  1488. static const char *getnumber(register const char *e, int *pn)
  1489. {
  1490. #ifdef __BCC__
  1491. /* bcc can optimize the counter if it thinks it is a pointer... */
  1492. register const char *n = (const char *) 3;
  1493. int f;
  1494. f = 0;
  1495. while (n && __isdigit_char(*e)) {
  1496. f = 10 * f + (*e++ - '0');
  1497. --n;
  1498. }
  1499. *pn = f;
  1500. return (n == (const char *) 3) ? NULL : e;
  1501. #else /* __BCC__ */
  1502. int n, f;
  1503. n = 3;
  1504. f = 0;
  1505. while (n && __isdigit_char(*e)) {
  1506. f = 10 * f + (*e++ - '0');
  1507. --n;
  1508. }
  1509. *pn = f;
  1510. return (n == 3) ? NULL : e;
  1511. #endif /* __BCC__ */
  1512. }
  1513. #ifdef __UCLIBC_MJN3_ONLY__
  1514. #warning CONSIDER: Should we preserve errno from open/read/close errors re TZ file?
  1515. #endif
  1516. #ifdef __UCLIBC_HAS_TZ_FILE__
  1517. #ifndef __UCLIBC_HAS_TZ_FILE_READ_MANY__
  1518. static int TZ_file_read; /* Let BSS initialization set this to 0. */
  1519. #endif /* __UCLIBC_HAS_TZ_FILE_READ_MANY__ */
  1520. static char *read_TZ_file(char *buf)
  1521. {
  1522. int fd;
  1523. ssize_t r;
  1524. size_t todo;
  1525. char *p = NULL;
  1526. if ((fd = __open(__UCLIBC_TZ_FILE_PATH__, O_RDONLY)) >= 0) {
  1527. todo = TZ_BUFLEN;
  1528. p = buf;
  1529. do {
  1530. if ((r = __read(fd, p, todo)) < 0) {
  1531. goto ERROR;
  1532. }
  1533. if (r == 0) {
  1534. break;
  1535. }
  1536. p += r;
  1537. todo -= r;
  1538. } while (todo);
  1539. if ((p > buf) && (p[-1] == '\n')) { /* Must end with newline. */
  1540. p[-1] = 0;
  1541. p = buf;
  1542. #ifndef __UCLIBC_HAS_TZ_FILE_READ_MANY__
  1543. ++TZ_file_read;
  1544. #endif /* __UCLIBC_HAS_TZ_FILE_READ_MANY__ */
  1545. } else {
  1546. ERROR:
  1547. p = NULL;
  1548. }
  1549. __close(fd);
  1550. }
  1551. return p;
  1552. }
  1553. #endif /* __UCLIBC_HAS_TZ_FILE__ */
  1554. void tzset(void)
  1555. {
  1556. register const char *e;
  1557. register char *s;
  1558. long off;
  1559. short *p;
  1560. rule_struct new_rules[2];
  1561. int n, count, f;
  1562. char c;
  1563. #ifdef __UCLIBC_HAS_TZ_FILE__
  1564. char buf[TZ_BUFLEN];
  1565. #endif /* __UCLIBC_HAS_TZ_FILE__ */
  1566. #ifdef __UCLIBC_HAS_TZ_CACHING__
  1567. static char oldval[TZ_BUFLEN]; /* BSS-zero'd. */
  1568. #endif /* __UCLIBC_HAS_TZ_CACHING__ */
  1569. TZLOCK;
  1570. e = __getenv(TZ); /* TZ env var always takes precedence. */
  1571. #if defined(__UCLIBC_HAS_TZ_FILE__) && !defined(__UCLIBC_HAS_TZ_FILE_READ_MANY__)
  1572. /* Put this inside the lock to prevent the possiblity of two different
  1573. * timezones being used in a threaded app. */
  1574. if (e != NULL) {
  1575. TZ_file_read = 0; /* Reset if the TZ env var is set. */
  1576. } else if (TZ_file_read > 0) {
  1577. goto FAST_DONE;
  1578. }
  1579. #endif /* defined(__UCLIBC_HAS_TZ_FILE__) && !defined(__UCLIBC_HAS_TZ_FILE_READ_MANY__) */
  1580. /* Warning!!! Since uClibc doesn't do lib locking, the following is
  1581. * potentially unsafe in a multi-threaded program since it is remotely
  1582. * possible that another thread could call setenv() for TZ and overwrite
  1583. * the string being parsed. So, don't do that... */
  1584. if ((!e /* TZ env var not set... */
  1585. #ifdef __UCLIBC_HAS_TZ_FILE__
  1586. && !(e = read_TZ_file(buf)) /* and no file or invalid file */
  1587. #endif /* __UCLIBC_HAS_TZ_FILE__ */
  1588. ) || !*e) { /* or set to empty string. */
  1589. ILLEGAL: /* TODO: Clean up the following... */
  1590. #ifdef __UCLIBC_HAS_TZ_CACHING__
  1591. *oldval = 0; /* Set oldval to an empty string. */
  1592. #endif /* __UCLIBC_HAS_TZ_CACHING__ */
  1593. __memset(_time_tzinfo, 0, 2*sizeof(rule_struct));
  1594. __strcpy(_time_tzinfo[0].tzname, UTC);
  1595. goto DONE;
  1596. }
  1597. if (*e == ':') { /* Ignore leading ':'. */
  1598. ++e;
  1599. }
  1600. #ifdef __UCLIBC_HAS_TZ_CACHING__
  1601. if (__strcmp(e, oldval) == 0) { /* Same string as last time... */
  1602. goto FAST_DONE; /* So nothing to do. */
  1603. }
  1604. /* Make a copy of the TZ env string. It won't be nul-terminated if
  1605. * it is too long, but it that case it will be illegal and will be reset
  1606. * to the empty string anyway. */
  1607. __strncpy(oldval, e, TZ_BUFLEN);
  1608. #endif /* __UCLIBC_HAS_TZ_CACHING__ */
  1609. count = 0;
  1610. new_rules[1].tzname[0] = 0;
  1611. LOOP:
  1612. /* Get std or dst name. */
  1613. c = 0;
  1614. if (*e == '<') {
  1615. ++e;
  1616. c = '>';
  1617. }
  1618. s = new_rules[count].tzname;
  1619. n = 0;
  1620. while (*e
  1621. && isascii(*e) /* SUSv3 requires char in portable char set. */
  1622. && (isalpha(*e)
  1623. || (c && (isalnum(*e) || (*e == '+') || (*e == '-'))))
  1624. ) {
  1625. *s++ = *e++;
  1626. if (++n > TZNAME_MAX) {
  1627. goto ILLEGAL;
  1628. }
  1629. }
  1630. *s = 0;
  1631. if ((n < 3) /* Check for minimum length. */
  1632. || (c && (*e++ != c)) /* Match any quoting '<'. */
  1633. ) {
  1634. goto ILLEGAL;
  1635. }
  1636. /* Get offset */
  1637. s = (char *) e;
  1638. if ((*e != '-') && (*e != '+')) {
  1639. if (count && !__isdigit_char(*e)) {
  1640. off -= 3600; /* Default to 1 hour ahead of std. */
  1641. goto SKIP_OFFSET;
  1642. }
  1643. --e;
  1644. }
  1645. ++e;
  1646. if (!(e = getoffset(e, &off))) {
  1647. goto ILLEGAL;
  1648. }
  1649. if (*s == '-') {
  1650. off = -off; /* Save off in case needed for dst default. */
  1651. }
  1652. SKIP_OFFSET:
  1653. new_rules[count].gmt_offset = off;
  1654. if (!count) {
  1655. new_rules[1].gmt_offset = off; /* Shouldn't be needed... */
  1656. if (*e) {
  1657. ++count;
  1658. goto LOOP;
  1659. }
  1660. } else { /* OK, we have dst, so get some rules. */
  1661. count = 0;
  1662. if (!*e) { /* No rules so default to US rules. */
  1663. e = DEFAULT_RULES;
  1664. }
  1665. do {
  1666. if (*e++ != ',') {
  1667. goto ILLEGAL;
  1668. }
  1669. n = 365;
  1670. s = (char *) RULE;
  1671. if ((c = *e++) == 'M') {
  1672. n = 12;
  1673. } else if (c == 'J') {
  1674. s += 8;
  1675. } else {
  1676. --e;
  1677. c = 0;
  1678. s += 6;
  1679. }
  1680. *(p = &new_rules[count].rule_type) = c;
  1681. if (c != 'M') {
  1682. p -= 2;
  1683. }
  1684. do {
  1685. ++s;
  1686. if (!(e = getnumber(e, &f))
  1687. || (((unsigned int)(f - s[1])) > n)
  1688. || (*s && (*e++ != *s))
  1689. ) {
  1690. goto ILLEGAL;
  1691. }
  1692. *--p = f;
  1693. } while ((n = *(s += 2)) > 0);
  1694. off = 2 * 60 * 60; /* Default to 2:00:00 */
  1695. if (*e == '/') {
  1696. ++e;
  1697. if (!(e = getoffset(e, &off))) {
  1698. goto ILLEGAL;
  1699. }
  1700. }
  1701. new_rules[count].dst_offset = off;
  1702. } while (++count < 2);
  1703. if (*e) {
  1704. goto ILLEGAL;
  1705. }
  1706. }
  1707. __memcpy(_time_tzinfo, new_rules, sizeof(new_rules));
  1708. DONE:
  1709. tzname[0] = _time_tzinfo[0].tzname;
  1710. tzname[1] = _time_tzinfo[1].tzname;
  1711. daylight = !!_time_tzinfo[1].tzname[0];
  1712. timezone = _time_tzinfo[0].gmt_offset;
  1713. #if defined(__UCLIBC_HAS_TZ_FILE__) || defined(__UCLIBC_HAS_TZ_CACHING__)
  1714. FAST_DONE:
  1715. #endif
  1716. TZUNLOCK;
  1717. }
  1718. #endif
  1719. /**********************************************************************/
  1720. /* #ifdef L_utime */
  1721. /* utime is a syscall in both linux and elks. */
  1722. /* int utime(const char *path, const struct utimbuf *times) */
  1723. /* #endif */
  1724. /**********************************************************************/
  1725. /* Non-SUSv3 */
  1726. /**********************************************************************/
  1727. #ifdef L_utimes
  1728. #ifndef __BCC__
  1729. #error The uClibc version of utimes is in sysdeps/linux/common.
  1730. #endif
  1731. #include <utime.h>
  1732. #include <sys/time.h>
  1733. int utimes(const char *filename, register const struct timeval *tvp)
  1734. {
  1735. register struct utimbuf *p = NULL;
  1736. struct utimbuf utb;
  1737. if (tvp) {
  1738. p = &utb;
  1739. p->actime = tvp[0].tv_sec;
  1740. p->modtime = tvp[1].tv_sec;
  1741. }
  1742. return utime(filename, p);
  1743. }
  1744. #endif
  1745. /**********************************************************************/
  1746. #ifdef L__time_t2tm
  1747. static const uint16_t _vals[] = {
  1748. 60, 60, 24, 7 /* special */, 36524, 1461, 365, 0
  1749. };
  1750. static const unsigned char days[] = {
  1751. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, /* non-leap */
  1752. 29,
  1753. };
  1754. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  1755. static const char utc_string[] = "UTC";
  1756. #endif
  1757. /* Notes:
  1758. * If time_t is 32 bits, then no overflow is possible.
  1759. * It time_t is > 32 bits, this needs to be adjusted to deal with overflow.
  1760. */
  1761. /* Note: offset is the correction in _days_ to *timer! */
  1762. struct tm attribute_hidden *_time_t2tm(const time_t *__restrict timer,
  1763. int offset, struct tm *__restrict result)
  1764. {
  1765. register int *p;
  1766. time_t t1, t, v;
  1767. int wday; /* Note: wday can be uninitialized. */
  1768. {
  1769. register const uint16_t *vp;
  1770. t = *timer;
  1771. p = (int *) result;
  1772. p[7] = 0;
  1773. vp = _vals;
  1774. do {
  1775. if ((v = *vp) == 7) {
  1776. /* Overflow checking, assuming time_t is long int... */
  1777. #if (LONG_MAX > INT_MAX) && (LONG_MAX > 2147483647L)
  1778. #if (INT_MAX == 2147483647L) && (LONG_MAX == 9223372036854775807L)
  1779. /* Valid range for t is [-784223472856L, 784223421720L].
  1780. * Outside of this range, the tm_year field will overflow. */
  1781. if (((unsigned long)(t + offset- -784223472856L))
  1782. > (784223421720L - -784223472856L)
  1783. ) {
  1784. return NULL;
  1785. }
  1786. #else
  1787. #error overflow conditions unknown
  1788. #endif
  1789. #endif
  1790. /* We have days since the epoch, so caluclate the weekday. */
  1791. #if defined(__BCC__) && TIME_T_IS_UNSIGNED
  1792. wday = (t + 4) % (*vp); /* t is unsigned */
  1793. #else
  1794. wday = ((int)((t % (*vp)) + 11)) % ((int)(*vp)); /* help bcc */
  1795. #endif
  1796. /* Set divisor to days in 400 years. Be kind to bcc... */
  1797. v = ((time_t)(vp[1])) << 2;
  1798. ++v;
  1799. /* Change to days since 1/1/1601 so that for 32 bit time_t
  1800. * values, we'll have t >= 0. This should be changed for
  1801. * archs with larger time_t types.
  1802. * Also, correct for offset since a multiple of 7. */
  1803. /* TODO: Does this still work on archs with time_t > 32 bits? */
  1804. t += (135140L - 366) + offset; /* 146097 - (365*30 + 7) -366 */
  1805. }
  1806. #if defined(__BCC__) && TIME_T_IS_UNSIGNED
  1807. t -= ((t1 = t / v) * v);
  1808. #else
  1809. if ((t -= ((t1 = t / v) * v)) < 0) {
  1810. t += v;
  1811. --t1;
  1812. }
  1813. #endif
  1814. if ((*vp == 7) && (t == v-1)) {
  1815. --t; /* Correct for 400th year leap case */
  1816. ++p[4]; /* Stash the extra day... */
  1817. }
  1818. #if defined(__BCC__) && 0
  1819. *p = t1;
  1820. if (v <= 60) {
  1821. *p = t;
  1822. t = t1;
  1823. }
  1824. ++p;
  1825. #else
  1826. if (v <= 60) {
  1827. *p++ = t;
  1828. t = t1;
  1829. } else {
  1830. *p++ = t1;
  1831. }
  1832. #endif
  1833. } while (*++vp);
  1834. }
  1835. if (p[-1] == 4) {
  1836. --p[-1];
  1837. t = 365;
  1838. }
  1839. *p += ((int) t); /* result[7] .. tm_yday */
  1840. p -= 2; /* at result[5] */
  1841. #if (LONG_MAX > INT_MAX) && (LONG_MAX > 2147483647L)
  1842. /* Protect against overflow. TODO: Unecessary if int arith wraps? */
  1843. *p = ((((p[-2]<<2) + p[-1])*25 + p[0])<< 2) + (p[1] - 299); /* tm_year */
  1844. #else
  1845. *p = ((((p[-2]<<2) + p[-1])*25 + p[0])<< 2) + p[1] - 299; /* tm_year */
  1846. #endif
  1847. p[1] = wday; /* result[6] .. tm_wday */
  1848. {
  1849. register const unsigned char *d = days;
  1850. wday = 1900 + *p;
  1851. if (__isleap(wday)) {
  1852. d += 11;
  1853. }
  1854. wday = p[2] + 1; /* result[7] .. tm_yday */
  1855. *--p = 0; /* at result[4] .. tm_mon */
  1856. while (wday > *d) {
  1857. wday -= *d;
  1858. if (*d == 29) {
  1859. d -= 11; /* Backup to non-leap Feb. */
  1860. }
  1861. ++d;
  1862. ++*p; /* Increment tm_mon. */
  1863. }
  1864. p[-1] = wday; /* result[3] .. tm_mday */
  1865. }
  1866. /* TODO -- should this be 0? */
  1867. p[4] = 0; /* result[8] .. tm_isdst */
  1868. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  1869. result->tm_gmtoff = 0;
  1870. result->tm_zone = utc_string;
  1871. #endif /* __UCLIBC_HAS_TM_EXTENSIONS__ */
  1872. return result;
  1873. }
  1874. #endif
  1875. /**********************************************************************/
  1876. #ifdef L___time_tm
  1877. struct tm __time_tm; /* Global shared by gmtime() and localtime(). */
  1878. #endif
  1879. /**********************************************************************/
  1880. #ifdef L__time_mktime
  1881. time_t attribute_hidden _time_mktime(struct tm *timeptr, int store_on_success)
  1882. {
  1883. time_t t;
  1884. TZLOCK;
  1885. tzset();
  1886. t = _time_mktime_tzi(timeptr, store_on_success, _time_tzinfo);
  1887. TZUNLOCK;
  1888. return t;
  1889. }
  1890. #endif
  1891. /**********************************************************************/
  1892. #ifdef L__time_mktime_tzi
  1893. static const unsigned char __vals[] = {
  1894. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, /* non-leap */
  1895. 29,
  1896. };
  1897. time_t attribute_hidden _time_mktime_tzi(struct tm *timeptr, int store_on_success,
  1898. rule_struct *tzi)
  1899. {
  1900. #ifdef __BCC__
  1901. long days, secs;
  1902. #else
  1903. long long secs;
  1904. #endif
  1905. time_t t;
  1906. struct tm x;
  1907. /* 0:sec 1:min 2:hour 3:mday 4:mon 5:year 6:wday 7:yday 8:isdst */
  1908. register int *p = (int *) &x;
  1909. register const unsigned char *s;
  1910. int d, default_dst;
  1911. __memcpy(p, timeptr, sizeof(struct tm));
  1912. if (!tzi[1].tzname[0]) { /* No dst in this timezone, */
  1913. p[8] = 0; /* so set tm_isdst to 0. */
  1914. }
  1915. default_dst = 0;
  1916. if (p[8]) { /* Either dst or unknown? */
  1917. default_dst = 1; /* Assume advancing (even if unknown). */
  1918. p[8] = ((p[8] > 0) ? 1 : -1); /* Normalize so abs() <= 1. */
  1919. }
  1920. d = 400;
  1921. p[5] = (p[5] - ((p[6] = p[5]/d) * d)) + (p[7] = p[4]/12);
  1922. if ((p[4] -= 12 * p[7]) < 0) {
  1923. p[4] += 12;
  1924. --p[5];
  1925. }
  1926. s = __vals;
  1927. d = (p[5] += 1900); /* Correct year. Now between 1900 and 2300. */
  1928. if (__isleap(d)) {
  1929. s += 11;
  1930. }
  1931. p[7] = 0;
  1932. d = p[4];
  1933. while (d) {
  1934. p[7] += *s;
  1935. if (*s == 29) {
  1936. s -= 11; /* Backup to non-leap Feb. */
  1937. }
  1938. ++s;
  1939. --d;
  1940. }
  1941. #ifdef __BCC__
  1942. d = p[5] - 1;
  1943. days = -719163L + ((long)d)*365 + ((d/4) - (d/100) + (d/400) + p[3] + p[7]);
  1944. secs = p[0] + 60*( p[1] + 60*((long)(p[2])) )
  1945. + tzi[default_dst].gmt_offset;
  1946. DST_CORRECT:
  1947. if (secs < 0) {
  1948. secs += 120009600L;
  1949. days -= 1389;
  1950. }
  1951. if ( ((unsigned long)(days + secs/86400L)) > 49710L) {
  1952. t = ((time_t)(-1));
  1953. goto DONE;
  1954. }
  1955. secs += (days * 86400L);
  1956. #else
  1957. d = p[5] - 1;
  1958. d = -719163L + d*365 + (d/4) - (d/100) + (d/400);
  1959. secs = p[0]
  1960. + tzi[default_dst].gmt_offset
  1961. + 60*( p[1]
  1962. + 60*(p[2]
  1963. + 24*(((146073L * ((long long)(p[6])) + d)
  1964. + p[3]) + p[7])));
  1965. DST_CORRECT:
  1966. if (((unsigned long long)(secs - LONG_MIN))
  1967. > (((unsigned long long)LONG_MAX) - LONG_MIN)
  1968. ) {
  1969. t = ((time_t)(-1));
  1970. goto DONE;
  1971. }
  1972. #endif
  1973. d = ((struct tm *)p)->tm_isdst;
  1974. t = secs;
  1975. __time_localtime_tzi(&t, (struct tm *)p, tzi);
  1976. if (t == ((time_t)(-1))) { /* Remember, time_t can be unsigned. */
  1977. goto DONE;
  1978. }
  1979. if ((d < 0) && (((struct tm *)p)->tm_isdst != default_dst)) {
  1980. #ifdef __BCC__
  1981. secs -= (days * 86400L);
  1982. #endif
  1983. secs += (tzi[1-default_dst].gmt_offset
  1984. - tzi[default_dst].gmt_offset);
  1985. goto DST_CORRECT;
  1986. }
  1987. if (store_on_success) {
  1988. __memcpy(timeptr, p, sizeof(struct tm));
  1989. }
  1990. DONE:
  1991. return t;
  1992. }
  1993. #endif
  1994. /**********************************************************************/
  1995. #if defined(L_wcsftime) || defined(L_wcsftime_l)
  1996. #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
  1997. extern size_t __wcsftime_l (wchar_t *__restrict __s, size_t __maxsize,
  1998. __const wchar_t *__restrict __format,
  1999. __const struct tm *__restrict __timeptr,
  2000. __locale_t __loc) __THROW attribute_hidden;
  2001. size_t wcsftime(wchar_t *__restrict s, size_t maxsize,
  2002. const wchar_t *__restrict format,
  2003. const struct tm *__restrict timeptr)
  2004. {
  2005. return __wcsftime_l(s, maxsize, format, timeptr, __UCLIBC_CURLOCALE);
  2006. }
  2007. #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  2008. size_t attribute_hidden __UCXL(wcsftime)(wchar_t *__restrict s, size_t maxsize,
  2009. const wchar_t *__restrict format,
  2010. const struct tm *__restrict timeptr __LOCALE_PARAM )
  2011. {
  2012. #warning wcsftime always fails
  2013. return 0; /* always fail */
  2014. }
  2015. __UCXL_ALIAS(wcsftime)
  2016. #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
  2017. #endif
  2018. /**********************************************************************/
  2019. #ifdef L_dysize
  2020. /* Return the number of days in YEAR. */
  2021. int dysize(int year)
  2022. {
  2023. return __isleap(year) ? 366 : 365;
  2024. }
  2025. #endif
  2026. /**********************************************************************/