time.c 63 KB

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