time.c 61 KB

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