parse_date.re 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015-2019 Derick Rethans
  5. * Copyright (c) 2018 MongoDB, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "timelib.h"
  26. #include "timelib_private.h"
  27. #include <ctype.h>
  28. #include <math.h>
  29. #include <assert.h>
  30. #include <limits.h>
  31. #if defined(_MSC_VER)
  32. # define strtoll(s, f, b) _atoi64(s)
  33. #elif !defined(HAVE_STRTOLL)
  34. # if defined(HAVE_ATOLL)
  35. # define strtoll(s, f, b) atoll(s)
  36. # else
  37. # define strtoll(s, f, b) strtol(s, f, b)
  38. # endif
  39. #endif
  40. #define EOI 257
  41. #define TIME 258
  42. #define DATE 259
  43. #define TIMELIB_XMLRPC_SOAP 260
  44. #define TIMELIB_TIME12 261
  45. #define TIMELIB_TIME24 262
  46. #define TIMELIB_GNU_NOCOLON 263
  47. #define TIMELIB_GNU_NOCOLON_TZ 264
  48. #define TIMELIB_ISO_NOCOLON 265
  49. #define TIMELIB_AMERICAN 266
  50. #define TIMELIB_ISO_DATE 267
  51. #define TIMELIB_DATE_FULL 268
  52. #define TIMELIB_DATE_TEXT 269
  53. #define TIMELIB_DATE_NOCOLON 270
  54. #define TIMELIB_PG_YEARDAY 271
  55. #define TIMELIB_PG_TEXT 272
  56. #define TIMELIB_PG_REVERSE 273
  57. #define TIMELIB_CLF 274
  58. #define TIMELIB_DATE_NO_DAY 275
  59. #define TIMELIB_SHORTDATE_WITH_TIME 276
  60. #define TIMELIB_DATE_FULL_POINTED 277
  61. #define TIMELIB_TIME24_WITH_ZONE 278
  62. #define TIMELIB_ISO_WEEK 279
  63. #define TIMELIB_LF_DAY_OF_MONTH 280
  64. #define TIMELIB_WEEK_DAY_OF_MONTH 281
  65. #define TIMELIB_TIMEZONE 300
  66. #define TIMELIB_AGO 301
  67. #define TIMELIB_RELATIVE 310
  68. #define TIMELIB_ERROR 999
  69. /* Some compilers like AIX, defines uchar in sys/types.h */
  70. #undef uchar
  71. typedef unsigned char uchar;
  72. #define BSIZE 8192
  73. #define YYCTYPE uchar
  74. #define YYCURSOR cursor
  75. #define YYLIMIT s->lim
  76. #define YYMARKER s->ptr
  77. #define YYFILL(n) return EOI;
  78. #define RET(i) {s->cur = cursor; return i;}
  79. #define timelib_string_free timelib_free
  80. #define TIMELIB_HAVE_TIME() { if (s->time->have_time) { add_error(s, TIMELIB_ERR_DOUBLE_TIME, "Double time specification"); timelib_string_free(str); return TIMELIB_ERROR; } else { s->time->have_time = 1; s->time->h = 0; s->time->i = 0; s->time->s = 0; s->time->us = 0; } }
  81. #define TIMELIB_UNHAVE_TIME() { s->time->have_time = 0; s->time->h = 0; s->time->i = 0; s->time->s = 0; s->time->us = 0; }
  82. #define TIMELIB_HAVE_DATE() { if (s->time->have_date) { add_error(s, TIMELIB_ERR_DOUBLE_DATE, "Double date specification"); timelib_string_free(str); return TIMELIB_ERROR; } else { s->time->have_date = 1; } }
  83. #define TIMELIB_UNHAVE_DATE() { s->time->have_date = 0; s->time->d = 0; s->time->m = 0; s->time->y = 0; }
  84. #define TIMELIB_HAVE_RELATIVE() { s->time->have_relative = 1; }
  85. #define TIMELIB_HAVE_WEEKDAY_RELATIVE() { s->time->have_relative = 1; s->time->relative.have_weekday_relative = 1; }
  86. #define TIMELIB_HAVE_SPECIAL_RELATIVE() { s->time->have_relative = 1; s->time->relative.have_special_relative = 1; }
  87. #define TIMELIB_HAVE_TZ() { s->cur = cursor; if (s->time->have_zone) { s->time->have_zone > 1 ? add_error(s, TIMELIB_ERR_DOUBLE_TZ, "Double timezone specification") : add_warning(s, TIMELIB_WARN_DOUBLE_TZ, "Double timezone specification"); timelib_string_free(str); s->time->have_zone++; return TIMELIB_ERROR; } else { s->time->have_zone++; } }
  88. #define TIMELIB_INIT s->cur = cursor; str = timelib_string(s); ptr = str
  89. #define TIMELIB_DEINIT timelib_string_free(str)
  90. #define TIMELIB_ADJUST_RELATIVE_WEEKDAY() if (in->time.have_weekday_relative && (in.rel.d > 0)) { in.rel.d -= 7; }
  91. #define TIMELIB_PROCESS_YEAR(x, l) { \
  92. if (((x) == TIMELIB_UNSET) || ((l) >= 4)) { \
  93. /* (x) = 0; */ \
  94. } else if ((x) < 100) { \
  95. if ((x) < 70) { \
  96. (x) += 2000; \
  97. } else { \
  98. (x) += 1900; \
  99. } \
  100. } \
  101. }
  102. #ifdef DEBUG_PARSER
  103. #define DEBUG_OUTPUT(s) printf("%s\n", s);
  104. #define YYDEBUG(s,c) { if (s != -1) { printf("state: %d ", s); printf("[%c]\n", c); } }
  105. #else
  106. #define DEBUG_OUTPUT(s)
  107. #define YYDEBUG(s,c)
  108. #endif
  109. typedef struct _timelib_elems {
  110. unsigned int c; /* Number of elements */
  111. char **v; /* Values */
  112. } timelib_elems;
  113. typedef struct _Scanner {
  114. int fd;
  115. uchar *lim, *str, *ptr, *cur, *tok, *pos;
  116. unsigned int line, len;
  117. timelib_error_container *errors;
  118. timelib_time *time;
  119. const timelib_tzdb *tzdb;
  120. } Scanner;
  121. typedef struct _timelib_lookup_table {
  122. const char *name;
  123. int type;
  124. int value;
  125. } timelib_lookup_table;
  126. typedef struct _timelib_relunit {
  127. const char *name;
  128. int unit;
  129. int multiplier;
  130. } timelib_relunit;
  131. /* The timezone table. */
  132. static const timelib_tz_lookup_table timelib_timezone_lookup[] = {
  133. #include "timezonemap.h"
  134. { NULL, 0, 0, NULL },
  135. };
  136. static const timelib_tz_lookup_table timelib_timezone_fallbackmap[] = {
  137. #include "fallbackmap.h"
  138. { NULL, 0, 0, NULL },
  139. };
  140. static const timelib_tz_lookup_table timelib_timezone_utc[] = {
  141. { "utc", 0, 0, "UTC" },
  142. };
  143. #if defined(_POSIX_TZNAME_MAX)
  144. # define MAX_ABBR_LEN _POSIX_TZNAME_MAX
  145. #elif defined(TZNAME_MAX)
  146. # define MAX_ABBR_LEN TZNAME_MAX
  147. #else
  148. # define MAX_ABBR_LEN 6
  149. #endif
  150. static timelib_relunit const timelib_relunit_lookup[] = {
  151. { "ms", TIMELIB_MICROSEC, 1000 },
  152. { "msec", TIMELIB_MICROSEC, 1000 },
  153. { "msecs", TIMELIB_MICROSEC, 1000 },
  154. { "millisecond", TIMELIB_MICROSEC, 1000 },
  155. { "milliseconds", TIMELIB_MICROSEC, 1000 },
  156. { "µs", TIMELIB_MICROSEC, 1 },
  157. { "usec", TIMELIB_MICROSEC, 1 },
  158. { "usecs", TIMELIB_MICROSEC, 1 },
  159. { "µsec", TIMELIB_MICROSEC, 1 },
  160. { "µsecs", TIMELIB_MICROSEC, 1 },
  161. { "microsecond", TIMELIB_MICROSEC, 1 },
  162. { "microseconds", TIMELIB_MICROSEC, 1 },
  163. { "sec", TIMELIB_SECOND, 1 },
  164. { "secs", TIMELIB_SECOND, 1 },
  165. { "second", TIMELIB_SECOND, 1 },
  166. { "seconds", TIMELIB_SECOND, 1 },
  167. { "min", TIMELIB_MINUTE, 1 },
  168. { "mins", TIMELIB_MINUTE, 1 },
  169. { "minute", TIMELIB_MINUTE, 1 },
  170. { "minutes", TIMELIB_MINUTE, 1 },
  171. { "hour", TIMELIB_HOUR, 1 },
  172. { "hours", TIMELIB_HOUR, 1 },
  173. { "day", TIMELIB_DAY, 1 },
  174. { "days", TIMELIB_DAY, 1 },
  175. { "week", TIMELIB_DAY, 7 },
  176. { "weeks", TIMELIB_DAY, 7 },
  177. { "fortnight", TIMELIB_DAY, 14 },
  178. { "fortnights", TIMELIB_DAY, 14 },
  179. { "forthnight", TIMELIB_DAY, 14 },
  180. { "forthnights", TIMELIB_DAY, 14 },
  181. { "month", TIMELIB_MONTH, 1 },
  182. { "months", TIMELIB_MONTH, 1 },
  183. { "year", TIMELIB_YEAR, 1 },
  184. { "years", TIMELIB_YEAR, 1 },
  185. { "mondays", TIMELIB_WEEKDAY, 1 },
  186. { "monday", TIMELIB_WEEKDAY, 1 },
  187. { "mon", TIMELIB_WEEKDAY, 1 },
  188. { "tuesdays", TIMELIB_WEEKDAY, 2 },
  189. { "tuesday", TIMELIB_WEEKDAY, 2 },
  190. { "tue", TIMELIB_WEEKDAY, 2 },
  191. { "wednesdays", TIMELIB_WEEKDAY, 3 },
  192. { "wednesday", TIMELIB_WEEKDAY, 3 },
  193. { "wed", TIMELIB_WEEKDAY, 3 },
  194. { "thursdays", TIMELIB_WEEKDAY, 4 },
  195. { "thursday", TIMELIB_WEEKDAY, 4 },
  196. { "thu", TIMELIB_WEEKDAY, 4 },
  197. { "fridays", TIMELIB_WEEKDAY, 5 },
  198. { "friday", TIMELIB_WEEKDAY, 5 },
  199. { "fri", TIMELIB_WEEKDAY, 5 },
  200. { "saturdays", TIMELIB_WEEKDAY, 6 },
  201. { "saturday", TIMELIB_WEEKDAY, 6 },
  202. { "sat", TIMELIB_WEEKDAY, 6 },
  203. { "sundays", TIMELIB_WEEKDAY, 0 },
  204. { "sunday", TIMELIB_WEEKDAY, 0 },
  205. { "sun", TIMELIB_WEEKDAY, 0 },
  206. { "weekday", TIMELIB_SPECIAL, TIMELIB_SPECIAL_WEEKDAY },
  207. { "weekdays", TIMELIB_SPECIAL, TIMELIB_SPECIAL_WEEKDAY },
  208. { NULL, 0, 0 }
  209. };
  210. /* The relative text table. */
  211. static timelib_lookup_table const timelib_reltext_lookup[] = {
  212. { "first", 0, 1 },
  213. { "next", 0, 1 },
  214. { "second", 0, 2 },
  215. { "third", 0, 3 },
  216. { "fourth", 0, 4 },
  217. { "fifth", 0, 5 },
  218. { "sixth", 0, 6 },
  219. { "seventh", 0, 7 },
  220. { "eight", 0, 8 },
  221. { "eighth", 0, 8 },
  222. { "ninth", 0, 9 },
  223. { "tenth", 0, 10 },
  224. { "eleventh", 0, 11 },
  225. { "twelfth", 0, 12 },
  226. { "last", 0, -1 },
  227. { "previous", 0, -1 },
  228. { "this", 1, 0 },
  229. { NULL, 1, 0 }
  230. };
  231. /* The month table. */
  232. static timelib_lookup_table const timelib_month_lookup[] = {
  233. { "jan", 0, 1 },
  234. { "feb", 0, 2 },
  235. { "mar", 0, 3 },
  236. { "apr", 0, 4 },
  237. { "may", 0, 5 },
  238. { "jun", 0, 6 },
  239. { "jul", 0, 7 },
  240. { "aug", 0, 8 },
  241. { "sep", 0, 9 },
  242. { "sept", 0, 9 },
  243. { "oct", 0, 10 },
  244. { "nov", 0, 11 },
  245. { "dec", 0, 12 },
  246. { "i", 0, 1 },
  247. { "ii", 0, 2 },
  248. { "iii", 0, 3 },
  249. { "iv", 0, 4 },
  250. { "v", 0, 5 },
  251. { "vi", 0, 6 },
  252. { "vii", 0, 7 },
  253. { "viii", 0, 8 },
  254. { "ix", 0, 9 },
  255. { "x", 0, 10 },
  256. { "xi", 0, 11 },
  257. { "xii", 0, 12 },
  258. { "january", 0, 1 },
  259. { "february", 0, 2 },
  260. { "march", 0, 3 },
  261. { "april", 0, 4 },
  262. { "may", 0, 5 },
  263. { "june", 0, 6 },
  264. { "july", 0, 7 },
  265. { "august", 0, 8 },
  266. { "september", 0, 9 },
  267. { "october", 0, 10 },
  268. { "november", 0, 11 },
  269. { "december", 0, 12 },
  270. { NULL, 0, 0 }
  271. };
  272. #if 0
  273. static char* timelib_ltrim(char *s)
  274. {
  275. char *ptr = s;
  276. while (ptr[0] == ' ' || ptr[0] == '\t') {
  277. ptr++;
  278. }
  279. return ptr;
  280. }
  281. #endif
  282. #if 0
  283. uchar *fill(Scanner *s, uchar *cursor){
  284. if(!s->eof){
  285. unsigned int cnt = s->tok - s->bot;
  286. if(cnt){
  287. memcpy(s->bot, s->tok, s->lim - s->tok);
  288. s->tok = s->bot;
  289. s->ptr -= cnt;
  290. cursor -= cnt;
  291. s->pos -= cnt;
  292. s->lim -= cnt;
  293. }
  294. if((s->top - s->lim) < BSIZE){
  295. uchar *buf = (uchar*) timelib_malloc(((s->lim - s->bot) + BSIZE)*sizeof(uchar));
  296. memcpy(buf, s->tok, s->lim - s->tok);
  297. s->tok = buf;
  298. s->ptr = &buf[s->ptr - s->bot];
  299. cursor = &buf[cursor - s->bot];
  300. s->pos = &buf[s->pos - s->bot];
  301. s->lim = &buf[s->lim - s->bot];
  302. s->top = &s->lim[BSIZE];
  303. timelib_free(s->bot);
  304. s->bot = buf;
  305. }
  306. if((cnt = read(s->fd, (char*) s->lim, BSIZE)) != BSIZE){
  307. s->eof = &s->lim[cnt]; *(s->eof)++ = '\n';
  308. }
  309. s->lim += cnt;
  310. }
  311. return cursor;
  312. }
  313. #endif
  314. static timelib_error_message *alloc_error_message(timelib_error_message **messages, int *count)
  315. {
  316. /* Realloc in power of two increments */
  317. int is_pow2 = (*count & (*count - 1)) == 0;
  318. if (is_pow2) {
  319. size_t alloc_size = *count ? (*count * 2) : 1;
  320. *messages = timelib_realloc(*messages, alloc_size * sizeof(timelib_error_message));
  321. }
  322. return *messages + (*count)++;
  323. }
  324. static void add_warning(Scanner *s, int error_code, const char *error)
  325. {
  326. timelib_error_message *message = alloc_error_message(&s->errors->warning_messages, &s->errors->warning_count);
  327. message->error_code = error_code;
  328. message->position = s->tok ? s->tok - s->str : 0;
  329. message->character = s->tok ? *s->tok : 0;
  330. message->message = timelib_strdup(error);
  331. }
  332. static void add_error(Scanner *s, int error_code, const char *error)
  333. {
  334. timelib_error_message *message = alloc_error_message(&s->errors->error_messages, &s->errors->error_count);
  335. message->error_code = error_code;
  336. message->position = s->tok ? s->tok - s->str : 0;
  337. message->character = s->tok ? *s->tok : 0;
  338. message->message = timelib_strdup(error);
  339. }
  340. static void add_pbf_warning(Scanner *s, int error_code, const char *error, const char *sptr, const char *cptr)
  341. {
  342. timelib_error_message *message = alloc_error_message(&s->errors->warning_messages, &s->errors->warning_count);
  343. message->error_code = error_code;
  344. message->position = cptr - sptr;
  345. message->character = *cptr;
  346. message->message = timelib_strdup(error);
  347. }
  348. static void add_pbf_error(Scanner *s, int error_code, const char *error, const char *sptr, const char *cptr)
  349. {
  350. timelib_error_message *message = alloc_error_message(&s->errors->error_messages, &s->errors->error_count);
  351. message->error_code = error_code;
  352. message->position = cptr - sptr;
  353. message->character = *cptr;
  354. message->message = timelib_strdup(error);
  355. }
  356. static timelib_sll timelib_meridian(const char **ptr, timelib_sll h)
  357. {
  358. timelib_sll retval = 0;
  359. while (!strchr("AaPp", **ptr)) {
  360. ++*ptr;
  361. }
  362. if (**ptr == 'a' || **ptr == 'A') {
  363. if (h == 12) {
  364. retval = -12;
  365. }
  366. } else if (h != 12) {
  367. retval = 12;
  368. }
  369. ++*ptr;
  370. if (**ptr == '.') {
  371. ++*ptr;
  372. }
  373. if (**ptr == 'M' || **ptr == 'm') {
  374. ++*ptr;
  375. }
  376. if (**ptr == '.') {
  377. ++*ptr;
  378. }
  379. return retval;
  380. }
  381. static timelib_sll timelib_meridian_with_check(const char **ptr, timelib_sll h)
  382. {
  383. timelib_sll retval = 0;
  384. while (**ptr && !strchr("AaPp", **ptr)) {
  385. ++*ptr;
  386. }
  387. if(!**ptr) {
  388. return TIMELIB_UNSET;
  389. }
  390. if (**ptr == 'a' || **ptr == 'A') {
  391. if (h == 12) {
  392. retval = -12;
  393. }
  394. } else if (h != 12) {
  395. retval = 12;
  396. }
  397. ++*ptr;
  398. if (**ptr == '.') {
  399. ++*ptr;
  400. if (**ptr != 'm' && **ptr != 'M') {
  401. return TIMELIB_UNSET;
  402. }
  403. ++*ptr;
  404. if (**ptr != '.' ) {
  405. return TIMELIB_UNSET;
  406. }
  407. ++*ptr;
  408. } else if (**ptr == 'm' || **ptr == 'M') {
  409. ++*ptr;
  410. } else {
  411. return TIMELIB_UNSET;
  412. }
  413. return retval;
  414. }
  415. static char *timelib_string(Scanner *s)
  416. {
  417. char *tmp = timelib_calloc(1, s->cur - s->tok + 1);
  418. memcpy(tmp, s->tok, s->cur - s->tok);
  419. return tmp;
  420. }
  421. static timelib_sll timelib_get_nr_ex(const char **ptr, int max_length, int *scanned_length)
  422. {
  423. const char *begin, *end;
  424. char *str;
  425. timelib_sll tmp_nr = TIMELIB_UNSET;
  426. int len = 0;
  427. while ((**ptr < '0') || (**ptr > '9')) {
  428. if (**ptr == '\0') {
  429. return TIMELIB_UNSET;
  430. }
  431. ++*ptr;
  432. }
  433. begin = *ptr;
  434. while ((**ptr >= '0') && (**ptr <= '9') && len < max_length) {
  435. ++*ptr;
  436. ++len;
  437. }
  438. end = *ptr;
  439. if (scanned_length) {
  440. *scanned_length = end - begin;
  441. }
  442. str = timelib_calloc(1, end - begin + 1);
  443. memcpy(str, begin, end - begin);
  444. tmp_nr = strtoll(str, NULL, 10);
  445. timelib_free(str);
  446. return tmp_nr;
  447. }
  448. static timelib_sll timelib_get_nr(const char **ptr, int max_length)
  449. {
  450. return timelib_get_nr_ex(ptr, max_length, NULL);
  451. }
  452. static void timelib_skip_day_suffix(const char **ptr)
  453. {
  454. if (isspace(**ptr)) {
  455. return;
  456. }
  457. if (!timelib_strncasecmp(*ptr, "nd", 2) || !timelib_strncasecmp(*ptr, "rd", 2) ||!timelib_strncasecmp(*ptr, "st", 2) || !timelib_strncasecmp(*ptr, "th", 2)) {
  458. *ptr += 2;
  459. }
  460. }
  461. static timelib_sll timelib_get_frac_nr(const char **ptr)
  462. {
  463. const char *begin, *end;
  464. char *str;
  465. double tmp_nr = TIMELIB_UNSET;
  466. while ((**ptr != '.') && (**ptr != ':') && ((**ptr < '0') || (**ptr > '9'))) {
  467. if (**ptr == '\0') {
  468. return TIMELIB_UNSET;
  469. }
  470. ++*ptr;
  471. }
  472. begin = *ptr;
  473. while ((**ptr == '.') || (**ptr == ':') || ((**ptr >= '0') && (**ptr <= '9'))) {
  474. ++*ptr;
  475. }
  476. end = *ptr;
  477. str = timelib_calloc(1, end - begin);
  478. memcpy(str, begin + 1, end - begin - 1);
  479. tmp_nr = strtod(str, NULL) * pow(10, 7 - (end - begin));
  480. timelib_free(str);
  481. return tmp_nr;
  482. }
  483. static timelib_ull timelib_get_signed_nr(Scanner *s, const char **ptr, int max_length)
  484. {
  485. timelib_ull dir = 1;
  486. while (((**ptr < '0') || (**ptr > '9')) && (**ptr != '+') && (**ptr != '-')) {
  487. if (**ptr == '\0') {
  488. add_error(s, TIMELIB_ERR_UNEXPECTED_DATA, "Found unexpected data");
  489. return 0;
  490. }
  491. ++*ptr;
  492. }
  493. while (**ptr == '+' || **ptr == '-')
  494. {
  495. if (**ptr == '-') {
  496. dir *= -1;
  497. }
  498. ++*ptr;
  499. }
  500. return dir * timelib_get_nr(ptr, max_length);
  501. }
  502. static timelib_sll timelib_lookup_relative_text(const char **ptr, int *behavior)
  503. {
  504. char *word;
  505. const char *begin = *ptr, *end;
  506. timelib_sll value = 0;
  507. const timelib_lookup_table *tp;
  508. while ((**ptr >= 'A' && **ptr <= 'Z') || (**ptr >= 'a' && **ptr <= 'z')) {
  509. ++*ptr;
  510. }
  511. end = *ptr;
  512. word = timelib_calloc(1, end - begin + 1);
  513. memcpy(word, begin, end - begin);
  514. for (tp = timelib_reltext_lookup; tp->name; tp++) {
  515. if (timelib_strcasecmp(word, tp->name) == 0) {
  516. value = tp->value;
  517. *behavior = tp->type;
  518. }
  519. }
  520. timelib_free(word);
  521. return value;
  522. }
  523. static timelib_sll timelib_get_relative_text(const char **ptr, int *behavior)
  524. {
  525. while (**ptr == ' ' || **ptr == '\t' || **ptr == '-' || **ptr == '/') {
  526. ++*ptr;
  527. }
  528. return timelib_lookup_relative_text(ptr, behavior);
  529. }
  530. static timelib_long timelib_lookup_month(const char **ptr)
  531. {
  532. char *word;
  533. const char *begin = *ptr, *end;
  534. timelib_long value = 0;
  535. const timelib_lookup_table *tp;
  536. while ((**ptr >= 'A' && **ptr <= 'Z') || (**ptr >= 'a' && **ptr <= 'z')) {
  537. ++*ptr;
  538. }
  539. end = *ptr;
  540. word = timelib_calloc(1, end - begin + 1);
  541. memcpy(word, begin, end - begin);
  542. for (tp = timelib_month_lookup; tp->name; tp++) {
  543. if (timelib_strcasecmp(word, tp->name) == 0) {
  544. value = tp->value;
  545. }
  546. }
  547. timelib_free(word);
  548. return value;
  549. }
  550. static timelib_long timelib_get_month(const char **ptr)
  551. {
  552. while (**ptr == ' ' || **ptr == '\t' || **ptr == '-' || **ptr == '.' || **ptr == '/') {
  553. ++*ptr;
  554. }
  555. return timelib_lookup_month(ptr);
  556. }
  557. static void timelib_eat_spaces(const char **ptr)
  558. {
  559. while (**ptr == ' ' || **ptr == '\t') {
  560. ++*ptr;
  561. }
  562. }
  563. static void timelib_eat_until_separator(const char **ptr)
  564. {
  565. ++*ptr;
  566. while (strchr(" \t.,:;/-0123456789", **ptr) == NULL) {
  567. ++*ptr;
  568. }
  569. }
  570. static const timelib_relunit* timelib_lookup_relunit(const char **ptr)
  571. {
  572. char *word;
  573. const char *begin = *ptr, *end;
  574. const timelib_relunit *tp, *value = NULL;
  575. while (**ptr != '\0' && **ptr != ' ' && **ptr != ',' && **ptr != '\t' && **ptr != ';' && **ptr != ':' &&
  576. **ptr != '/' && **ptr != '.' && **ptr != '-' && **ptr != '(' && **ptr != ')' ) {
  577. ++*ptr;
  578. }
  579. end = *ptr;
  580. word = timelib_calloc(1, end - begin + 1);
  581. memcpy(word, begin, end - begin);
  582. for (tp = timelib_relunit_lookup; tp->name; tp++) {
  583. if (timelib_strcasecmp(word, tp->name) == 0) {
  584. value = tp;
  585. break;
  586. }
  587. }
  588. timelib_free(word);
  589. return value;
  590. }
  591. /**
  592. * The time_part parameter is a flag. It can be TIMELIB_TIME_PART_KEEP in case
  593. * the time portion should not be reset to midnight, or
  594. * TIMELIB_TIME_PART_DONT_KEEP in case it does need to be reset. This is used
  595. * for not overwriting the time portion for 'X weekday'.
  596. */
  597. static void timelib_set_relative(const char **ptr, timelib_sll amount, int behavior, Scanner *s, int time_part)
  598. {
  599. const timelib_relunit* relunit;
  600. if (!(relunit = timelib_lookup_relunit(ptr))) {
  601. return;
  602. }
  603. switch (relunit->unit) {
  604. case TIMELIB_MICROSEC: s->time->relative.us += amount * relunit->multiplier; break;
  605. case TIMELIB_SECOND: s->time->relative.s += amount * relunit->multiplier; break;
  606. case TIMELIB_MINUTE: s->time->relative.i += amount * relunit->multiplier; break;
  607. case TIMELIB_HOUR: s->time->relative.h += amount * relunit->multiplier; break;
  608. case TIMELIB_DAY: s->time->relative.d += amount * relunit->multiplier; break;
  609. case TIMELIB_MONTH: s->time->relative.m += amount * relunit->multiplier; break;
  610. case TIMELIB_YEAR: s->time->relative.y += amount * relunit->multiplier; break;
  611. case TIMELIB_WEEKDAY:
  612. TIMELIB_HAVE_WEEKDAY_RELATIVE();
  613. if (time_part != TIMELIB_TIME_PART_KEEP) {
  614. TIMELIB_UNHAVE_TIME();
  615. }
  616. s->time->relative.d += (amount > 0 ? amount - 1 : amount) * 7;
  617. s->time->relative.weekday = relunit->multiplier;
  618. s->time->relative.weekday_behavior = behavior;
  619. break;
  620. case TIMELIB_SPECIAL:
  621. TIMELIB_HAVE_SPECIAL_RELATIVE();
  622. if (time_part != TIMELIB_TIME_PART_KEEP) {
  623. TIMELIB_UNHAVE_TIME();
  624. }
  625. s->time->relative.special.type = relunit->multiplier;
  626. s->time->relative.special.amount = amount;
  627. }
  628. }
  629. static const timelib_tz_lookup_table* abbr_search(const char *word, timelib_long gmtoffset, int isdst)
  630. {
  631. int first_found = 0;
  632. const timelib_tz_lookup_table *tp, *first_found_elem = NULL;
  633. const timelib_tz_lookup_table *fmp;
  634. if (timelib_strcasecmp("utc", word) == 0 || timelib_strcasecmp("gmt", word) == 0) {
  635. return timelib_timezone_utc;
  636. }
  637. for (tp = timelib_timezone_lookup; tp->name; tp++) {
  638. if (timelib_strcasecmp(word, tp->name) == 0) {
  639. if (!first_found) {
  640. first_found = 1;
  641. first_found_elem = tp;
  642. if (gmtoffset == -1) {
  643. return tp;
  644. }
  645. }
  646. if (tp->gmtoffset == gmtoffset) {
  647. return tp;
  648. }
  649. }
  650. }
  651. if (first_found) {
  652. return first_found_elem;
  653. }
  654. /* Still didn't find anything, let's find the zone solely based on
  655. * offset/isdst then */
  656. for (fmp = timelib_timezone_fallbackmap; fmp->name; fmp++) {
  657. if (fmp->gmtoffset == gmtoffset && fmp->type == isdst) {
  658. return fmp;
  659. }
  660. }
  661. return NULL;
  662. }
  663. static timelib_long timelib_lookup_abbr(const char **ptr, int *dst, char **tz_abbr, int *found)
  664. {
  665. char *word;
  666. const char *begin = *ptr, *end;
  667. timelib_long value = 0;
  668. const timelib_tz_lookup_table *tp;
  669. while (**ptr != '\0' && **ptr != ')' && **ptr != ' ') {
  670. ++*ptr;
  671. }
  672. end = *ptr;
  673. word = timelib_calloc(1, end - begin + 1);
  674. memcpy(word, begin, end - begin);
  675. if (end - begin < MAX_ABBR_LEN && (tp = abbr_search(word, -1, 0))) {
  676. value = tp->gmtoffset;
  677. *dst = tp->type;
  678. value -= tp->type * 3600;
  679. *found = 1;
  680. } else {
  681. *found = 0;
  682. }
  683. *tz_abbr = word;
  684. return value;
  685. }
  686. #define sHOUR(a) (int)(a * 3600)
  687. #define sMIN(a) (int)(a * 60)
  688. static timelib_long timelib_parse_tz_cor(const char **ptr, int *tz_not_found)
  689. {
  690. const char *begin = *ptr, *end;
  691. timelib_long tmp;
  692. *tz_not_found = 1;
  693. while (isdigit(**ptr) || **ptr == ':') {
  694. ++*ptr;
  695. }
  696. end = *ptr;
  697. switch (end - begin) {
  698. case 1: /* H */
  699. case 2: /* HH */
  700. *tz_not_found = 0;
  701. return sHOUR(strtol(begin, NULL, 10));
  702. case 3: /* H:M */
  703. case 4: /* H:MM, HH:M, HHMM */
  704. if (begin[1] == ':') {
  705. *tz_not_found = 0;
  706. tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 2, NULL, 10));
  707. return tmp;
  708. } else if (begin[2] == ':') {
  709. *tz_not_found = 0;
  710. tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 3, NULL, 10));
  711. return tmp;
  712. } else {
  713. *tz_not_found = 0;
  714. tmp = strtol(begin, NULL, 10);
  715. return sHOUR(tmp / 100) + sMIN(tmp % 100);
  716. }
  717. case 5: /* HH:MM */
  718. if (begin[2] != ':') {
  719. break;
  720. }
  721. *tz_not_found = 0;
  722. tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 3, NULL, 10));
  723. return tmp;
  724. case 6: /* HHMMSS */
  725. *tz_not_found = 0;
  726. tmp = strtol(begin, NULL, 10);
  727. tmp = sHOUR(tmp / 10000) + sMIN((tmp / 100) % 100) + (tmp % 100);
  728. return tmp;
  729. case 8: /* HH:MM:SS */
  730. if (begin[2] != ':' || begin[5] != ':') {
  731. break;
  732. }
  733. *tz_not_found = 0;
  734. tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 3, NULL, 10)) + strtol(begin + 6, NULL, 10);
  735. return tmp;
  736. }
  737. return 0;
  738. }
  739. static timelib_long timelib_parse_tz_minutes(const char **ptr, timelib_time *t)
  740. {
  741. timelib_long retval = TIMELIB_UNSET;
  742. const char *begin = *ptr;
  743. /* First character must be +/- */
  744. if (**ptr != '+' && **ptr != '-') {
  745. return retval;
  746. }
  747. ++*ptr;
  748. while (isdigit(**ptr)) {
  749. ++*ptr;
  750. }
  751. if (*begin == '+') {
  752. t->is_localtime = 1;
  753. t->zone_type = TIMELIB_ZONETYPE_OFFSET;
  754. t->dst = 0;
  755. retval = sMIN(strtol(begin + 1, NULL, 10));
  756. } else if (*begin == '-') {
  757. t->is_localtime = 1;
  758. t->zone_type = TIMELIB_ZONETYPE_OFFSET;
  759. t->dst = 0;
  760. retval = -1 * sMIN(strtol(begin + 1, NULL, 10));
  761. }
  762. return retval;
  763. }
  764. timelib_long timelib_parse_zone(const char **ptr, int *dst, timelib_time *t, int *tz_not_found, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_wrapper)
  765. {
  766. timelib_tzinfo *res;
  767. timelib_long retval = 0;
  768. *tz_not_found = 0;
  769. while (**ptr == ' ' || **ptr == '\t' || **ptr == '(') {
  770. ++*ptr;
  771. }
  772. if ((*ptr)[0] == 'G' && (*ptr)[1] == 'M' && (*ptr)[2] == 'T' && ((*ptr)[3] == '+' || (*ptr)[3] == '-')) {
  773. *ptr += 3;
  774. }
  775. if (**ptr == '+') {
  776. ++*ptr;
  777. t->is_localtime = 1;
  778. t->zone_type = TIMELIB_ZONETYPE_OFFSET;
  779. t->dst = 0;
  780. retval = timelib_parse_tz_cor(ptr, tz_not_found);
  781. } else if (**ptr == '-') {
  782. ++*ptr;
  783. t->is_localtime = 1;
  784. t->zone_type = TIMELIB_ZONETYPE_OFFSET;
  785. t->dst = 0;
  786. retval = -1 * timelib_parse_tz_cor(ptr, tz_not_found);
  787. } else {
  788. int found = 0;
  789. timelib_long offset = 0;
  790. char *tz_abbr;
  791. t->is_localtime = 1;
  792. /* First, we lookup by abbreviation only */
  793. offset = timelib_lookup_abbr(ptr, dst, &tz_abbr, &found);
  794. if (found) {
  795. t->zone_type = TIMELIB_ZONETYPE_ABBR;
  796. t->dst = *dst;
  797. timelib_time_tz_abbr_update(t, tz_abbr);
  798. }
  799. /* Otherwise, we look if we have a TimeZone identifier */
  800. if (!found || strcmp("UTC", tz_abbr) == 0) {
  801. int dummy_error_code;
  802. if ((res = tz_wrapper(tz_abbr, tzdb, &dummy_error_code)) != NULL) {
  803. t->tz_info = res;
  804. t->zone_type = TIMELIB_ZONETYPE_ID;
  805. found++;
  806. }
  807. }
  808. timelib_free(tz_abbr);
  809. *tz_not_found = (found == 0);
  810. retval = offset;
  811. }
  812. while (**ptr == ')') {
  813. ++*ptr;
  814. }
  815. return retval;
  816. }
  817. #define timelib_split_free(arg) { \
  818. int i; \
  819. for (i = 0; i < arg.c; i++) { \
  820. timelib_free(arg.v[i]); \
  821. } \
  822. if (arg.v) { \
  823. timelib_free(arg.v); \
  824. } \
  825. }
  826. static int scan(Scanner *s, timelib_tz_get_wrapper tz_get_wrapper)
  827. {
  828. uchar *cursor = s->cur;
  829. char *str;
  830. const char *ptr = NULL;
  831. std:
  832. s->tok = cursor;
  833. s->len = 0;
  834. /*!re2c
  835. any = [\000-\377];
  836. space = [ \t]+;
  837. frac = "."[0-9]+;
  838. ago = 'ago';
  839. hour24 = [01]?[0-9] | "2"[0-4];
  840. hour24lz = [01][0-9] | "2"[0-4];
  841. hour12 = "0"?[1-9] | "1"[0-2];
  842. minute = [0-5]?[0-9];
  843. minutelz = [0-5][0-9];
  844. second = minute | "60";
  845. secondlz = minutelz | "60";
  846. meridian = ([AaPp] "."? [Mm] "."?) [\000\t ];
  847. tz = "("? [A-Za-z]{1,6} ")"? | [A-Z][a-z]+([_/-][A-Za-z]+)+;
  848. tzcorrection = "GMT"? [+-] ((hour24 (":"? minute)?) | (hour24lz minutelz secondlz) | (hour24lz ":" minutelz ":" secondlz));
  849. daysuf = "st" | "nd" | "rd" | "th";
  850. month = "0"? [0-9] | "1"[0-2];
  851. day = (([0-2]?[0-9]) | ("3"[01])) daysuf?;
  852. year = [0-9]{1,4};
  853. year2 = [0-9]{2};
  854. year4 = [0-9]{4};
  855. year4withsign = [+-]? [0-9]{4};
  856. yearx = [+-] [0-9]{5,19};
  857. dayofyear = "00"[1-9] | "0"[1-9][0-9] | [1-2][0-9][0-9] | "3"[0-5][0-9] | "36"[0-6];
  858. weekofyear = "0"[1-9] | [1-4][0-9] | "5"[0-3];
  859. monthlz = "0" [0-9] | "1" [0-2];
  860. daylz = "0" [0-9] | [1-2][0-9] | "3" [01];
  861. dayfulls = 'sundays' | 'mondays' | 'tuesdays' | 'wednesdays' | 'thursdays' | 'fridays' | 'saturdays';
  862. dayfull = 'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday';
  863. dayabbr = 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun';
  864. dayspecial = 'weekday' | 'weekdays';
  865. daytext = dayfulls | dayfull | dayabbr | dayspecial;
  866. monthfull = 'january' | 'february' | 'march' | 'april' | 'may' | 'june' | 'july' | 'august' | 'september' | 'october' | 'november' | 'december';
  867. monthabbr = 'jan' | 'feb' | 'mar' | 'apr' | 'may' | 'jun' | 'jul' | 'aug' | 'sep' | 'sept' | 'oct' | 'nov' | 'dec';
  868. monthroman = "I" | "II" | "III" | "IV" | "V" | "VI" | "VII" | "VIII" | "IX" | "X" | "XI" | "XII";
  869. monthtext = monthfull | monthabbr | monthroman;
  870. /* Time formats */
  871. timetiny12 = hour12 space? meridian;
  872. timeshort12 = hour12[:.]minutelz space? meridian;
  873. timelong12 = hour12[:.]minute[:.]secondlz space? meridian;
  874. timetiny24 = 't' hour24;
  875. timeshort24 = 't'? hour24[:.]minute;
  876. timelong24 = 't'? hour24[:.]minute[:.]second;
  877. iso8601long = 't'? hour24 [:.] minute [:.] second frac;
  878. /* iso8601shorttz = hour24 [:] minutelz space? (tzcorrection | tz); */
  879. iso8601normtz = 't'? hour24 [:.] minute [:.] secondlz space? (tzcorrection | tz);
  880. /* iso8601longtz = hour24 [:] minute [:] secondlz frac space? (tzcorrection | tz); */
  881. gnunocolon = 't'? hour24lz minutelz;
  882. /* gnunocolontz = hour24lz minutelz space? (tzcorrection | tz); */
  883. iso8601nocolon = 't'? hour24lz minutelz secondlz;
  884. /* iso8601nocolontz = hour24lz minutelz secondlz space? (tzcorrection | tz); */
  885. /* Date formats */
  886. americanshort = month "/" day;
  887. american = month "/" day "/" year;
  888. iso8601dateslash = year4 "/" monthlz "/" daylz "/"?;
  889. dateslash = year4 "/" month "/" day;
  890. iso8601date4 = year4withsign "-" monthlz "-" daylz;
  891. iso8601date2 = year2 "-" monthlz "-" daylz;
  892. iso8601datex = yearx "-" monthlz "-" daylz;
  893. gnudateshorter = year4 "-" month;
  894. gnudateshort = year "-" month "-" day;
  895. pointeddate4 = day [.\t-] month [.-] year4;
  896. pointeddate2 = day [.\t] month "." year2;
  897. datefull = day ([ \t.-])* monthtext ([ \t.-])* year;
  898. datenoday = monthtext ([ .\t-])* year4;
  899. datenodayrev = year4 ([ .\t-])* monthtext;
  900. datetextual = monthtext ([ .\t-])* day [,.stndrh\t ]+ year;
  901. datenoyear = monthtext ([ .\t-])* day ([,.stndrh\t ]+|[\000]);
  902. datenoyearrev = day ([ .\t-])* monthtext;
  903. datenocolon = year4 monthlz daylz;
  904. /* Special formats */
  905. soap = year4 "-" monthlz "-" daylz "T" hour24lz ":" minutelz ":" secondlz frac tzcorrection?;
  906. xmlrpc = year4 monthlz daylz "T" hour24 ":" minutelz ":" secondlz;
  907. xmlrpcnocolon = year4 monthlz daylz 't' hour24 minutelz secondlz;
  908. wddx = year4 "-" month "-" day "T" hour24 ":" minute ":" second;
  909. pgydotd = year4 [.-]? dayofyear;
  910. pgtextshort = monthabbr "-" daylz "-" year;
  911. pgtextreverse = year "-" monthabbr "-" daylz;
  912. mssqltime = hour12 ":" minutelz ":" secondlz [:.] [0-9]+ meridian;
  913. isoweekday = year4 "-"? "W" weekofyear "-"? [0-7];
  914. isoweek = year4 "-"? "W" weekofyear;
  915. exif = year4 ":" monthlz ":" daylz " " hour24lz ":" minutelz ":" secondlz;
  916. firstdayof = 'first day of';
  917. lastdayof = 'last day of';
  918. backof = 'back of ' hour24 (space? meridian)?;
  919. frontof = 'front of ' hour24 (space? meridian)?;
  920. /* Common Log Format: 10/Oct/2000:13:55:36 -0700 */
  921. clf = day "/" monthabbr "/" year4 ":" hour24lz ":" minutelz ":" secondlz space tzcorrection;
  922. /* Timestamp format: @1126396800 */
  923. timestamp = "@" "-"? [0-9]+;
  924. timestampms = "@" "-"? [0-9]+ "." [0-9]{0,6};
  925. /* To fix some ambiguities */
  926. dateshortwithtimeshort12 = datenoyear timeshort12;
  927. dateshortwithtimelong12 = datenoyear timelong12;
  928. dateshortwithtimeshort = datenoyear timeshort24;
  929. dateshortwithtimelong = datenoyear timelong24;
  930. dateshortwithtimelongtz = datenoyear iso8601normtz;
  931. /*
  932. * Relative regexps
  933. */
  934. reltextnumber = 'first'|'second'|'third'|'fourth'|'fifth'|'sixth'|'seventh'|'eight'|'eighth'|'ninth'|'tenth'|'eleventh'|'twelfth';
  935. reltexttext = 'next'|'last'|'previous'|'this';
  936. reltextunit = 'ms' | 'µs' | (('msec'|'millisecond'|'µsec'|'microsecond'|'usec'|'sec'|'second'|'min'|'minute'|'hour'|'day'|'fortnight'|'forthnight'|'month'|'year') 's'?) | 'weeks' | daytext;
  937. relnumber = ([+-]*[ \t]*[0-9]{1,13});
  938. relative = relnumber space? (reltextunit | 'week' );
  939. relativetext = (reltextnumber|reltexttext) space reltextunit;
  940. relativetextweek = reltexttext space 'week';
  941. weekdayof = (reltextnumber|reltexttext) space (dayfulls|dayfull|dayabbr) space 'of';
  942. */
  943. /*!re2c
  944. /* so that vim highlights correctly */
  945. 'yesterday'
  946. {
  947. DEBUG_OUTPUT("yesterday");
  948. TIMELIB_INIT;
  949. TIMELIB_HAVE_RELATIVE();
  950. TIMELIB_UNHAVE_TIME();
  951. s->time->relative.d = -1;
  952. TIMELIB_DEINIT;
  953. return TIMELIB_RELATIVE;
  954. }
  955. 'now'
  956. {
  957. DEBUG_OUTPUT("now");
  958. TIMELIB_INIT;
  959. TIMELIB_DEINIT;
  960. return TIMELIB_RELATIVE;
  961. }
  962. 'noon'
  963. {
  964. DEBUG_OUTPUT("noon");
  965. TIMELIB_INIT;
  966. TIMELIB_UNHAVE_TIME();
  967. TIMELIB_HAVE_TIME();
  968. s->time->h = 12;
  969. TIMELIB_DEINIT;
  970. return TIMELIB_RELATIVE;
  971. }
  972. 'midnight' | 'today'
  973. {
  974. DEBUG_OUTPUT("midnight | today");
  975. TIMELIB_INIT;
  976. TIMELIB_UNHAVE_TIME();
  977. TIMELIB_DEINIT;
  978. return TIMELIB_RELATIVE;
  979. }
  980. 'tomorrow'
  981. {
  982. DEBUG_OUTPUT("tomorrow");
  983. TIMELIB_INIT;
  984. TIMELIB_HAVE_RELATIVE();
  985. TIMELIB_UNHAVE_TIME();
  986. s->time->relative.d = 1;
  987. TIMELIB_DEINIT;
  988. return TIMELIB_RELATIVE;
  989. }
  990. timestamp
  991. {
  992. timelib_ull i;
  993. TIMELIB_INIT;
  994. TIMELIB_HAVE_RELATIVE();
  995. TIMELIB_UNHAVE_DATE();
  996. TIMELIB_UNHAVE_TIME();
  997. TIMELIB_HAVE_TZ();
  998. i = timelib_get_signed_nr(s, &ptr, 24);
  999. s->time->y = 1970;
  1000. s->time->m = 1;
  1001. s->time->d = 1;
  1002. s->time->h = s->time->i = s->time->s = 0;
  1003. s->time->us = 0;
  1004. s->time->relative.s += i;
  1005. s->time->is_localtime = 1;
  1006. s->time->zone_type = TIMELIB_ZONETYPE_OFFSET;
  1007. s->time->z = 0;
  1008. s->time->dst = 0;
  1009. TIMELIB_DEINIT;
  1010. return TIMELIB_RELATIVE;
  1011. }
  1012. timestampms
  1013. {
  1014. timelib_sll i;
  1015. timelib_ull us;
  1016. const char *ptr_before;
  1017. bool is_negative;
  1018. TIMELIB_INIT;
  1019. TIMELIB_HAVE_RELATIVE();
  1020. TIMELIB_UNHAVE_DATE();
  1021. TIMELIB_UNHAVE_TIME();
  1022. TIMELIB_HAVE_TZ();
  1023. is_negative = *(ptr + 1) == '-';
  1024. i = timelib_get_signed_nr(s, &ptr, 24);
  1025. ptr_before = ptr;
  1026. us = timelib_get_signed_nr(s, &ptr, 6);
  1027. us = us * pow(10, 7 - (ptr - ptr_before));
  1028. if (is_negative) {
  1029. us *= -1;
  1030. }
  1031. s->time->y = 1970;
  1032. s->time->m = 1;
  1033. s->time->d = 1;
  1034. s->time->h = s->time->i = s->time->s = 0;
  1035. s->time->us = 0;
  1036. s->time->relative.s += i;
  1037. s->time->relative.us = us;
  1038. s->time->is_localtime = 1;
  1039. s->time->zone_type = TIMELIB_ZONETYPE_OFFSET;
  1040. s->time->z = 0;
  1041. s->time->dst = 0;
  1042. TIMELIB_DEINIT;
  1043. return TIMELIB_RELATIVE;
  1044. }
  1045. firstdayof | lastdayof
  1046. {
  1047. DEBUG_OUTPUT("firstdayof | lastdayof");
  1048. TIMELIB_INIT;
  1049. TIMELIB_HAVE_RELATIVE();
  1050. /* skip "last day of" or "first day of" */
  1051. if (*ptr == 'l' || *ptr == 'L') {
  1052. s->time->relative.first_last_day_of = TIMELIB_SPECIAL_LAST_DAY_OF_MONTH;
  1053. } else {
  1054. s->time->relative.first_last_day_of = TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH;
  1055. }
  1056. TIMELIB_DEINIT;
  1057. return TIMELIB_LF_DAY_OF_MONTH;
  1058. }
  1059. backof | frontof
  1060. {
  1061. DEBUG_OUTPUT("backof | frontof");
  1062. TIMELIB_INIT;
  1063. TIMELIB_UNHAVE_TIME();
  1064. TIMELIB_HAVE_TIME();
  1065. if (*ptr == 'b') {
  1066. s->time->h = timelib_get_nr(&ptr, 2);
  1067. s->time->i = 15;
  1068. } else {
  1069. s->time->h = timelib_get_nr(&ptr, 2) - 1;
  1070. s->time->i = 45;
  1071. }
  1072. if (*ptr != '\0' ) {
  1073. timelib_eat_spaces(&ptr);
  1074. s->time->h += timelib_meridian(&ptr, s->time->h);
  1075. }
  1076. TIMELIB_DEINIT;
  1077. return TIMELIB_LF_DAY_OF_MONTH;
  1078. }
  1079. weekdayof
  1080. {
  1081. timelib_sll i;
  1082. int behavior = 0;
  1083. DEBUG_OUTPUT("weekdayof");
  1084. TIMELIB_INIT;
  1085. TIMELIB_HAVE_RELATIVE();
  1086. TIMELIB_HAVE_SPECIAL_RELATIVE();
  1087. i = timelib_get_relative_text(&ptr, &behavior);
  1088. timelib_eat_spaces(&ptr);
  1089. if (i > 0) { /* first, second... etc */
  1090. s->time->relative.special.type = TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH;
  1091. timelib_set_relative(&ptr, i, 1, s, TIMELIB_TIME_PART_DONT_KEEP);
  1092. } else { /* last */
  1093. s->time->relative.special.type = TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH;
  1094. timelib_set_relative(&ptr, i, behavior, s, TIMELIB_TIME_PART_DONT_KEEP);
  1095. }
  1096. TIMELIB_DEINIT;
  1097. return TIMELIB_WEEK_DAY_OF_MONTH;
  1098. }
  1099. timetiny12 | timeshort12 | timelong12
  1100. {
  1101. DEBUG_OUTPUT("timetiny12 | timeshort12 | timelong12");
  1102. TIMELIB_INIT;
  1103. TIMELIB_HAVE_TIME();
  1104. s->time->h = timelib_get_nr(&ptr, 2);
  1105. if (*ptr == ':' || *ptr == '.') {
  1106. s->time->i = timelib_get_nr(&ptr, 2);
  1107. if (*ptr == ':' || *ptr == '.') {
  1108. s->time->s = timelib_get_nr(&ptr, 2);
  1109. }
  1110. }
  1111. s->time->h += timelib_meridian(&ptr, s->time->h);
  1112. TIMELIB_DEINIT;
  1113. return TIMELIB_TIME12;
  1114. }
  1115. mssqltime
  1116. {
  1117. DEBUG_OUTPUT("mssqltime");
  1118. TIMELIB_INIT;
  1119. TIMELIB_HAVE_TIME();
  1120. s->time->h = timelib_get_nr(&ptr, 2);
  1121. s->time->i = timelib_get_nr(&ptr, 2);
  1122. if (*ptr == ':' || *ptr == '.') {
  1123. s->time->s = timelib_get_nr(&ptr, 2);
  1124. if (*ptr == ':' || *ptr == '.') {
  1125. s->time->us = timelib_get_frac_nr(&ptr);
  1126. }
  1127. }
  1128. timelib_eat_spaces(&ptr);
  1129. s->time->h += timelib_meridian(&ptr, s->time->h);
  1130. TIMELIB_DEINIT;
  1131. return TIMELIB_TIME24_WITH_ZONE;
  1132. }
  1133. timetiny24 | timeshort24 | timelong24 /* | iso8601short | iso8601norm */ | iso8601long /*| iso8601shorttz | iso8601normtz | iso8601longtz*/
  1134. {
  1135. int tz_not_found;
  1136. DEBUG_OUTPUT("timetiny24 | timeshort24 | timelong24 | iso8601long");
  1137. TIMELIB_INIT;
  1138. TIMELIB_HAVE_TIME();
  1139. s->time->h = timelib_get_nr(&ptr, 2);
  1140. if (*ptr == ':' || *ptr == '.') {
  1141. s->time->i = timelib_get_nr(&ptr, 2);
  1142. if (*ptr == ':' || *ptr == '.') {
  1143. s->time->s = timelib_get_nr(&ptr, 2);
  1144. if (*ptr == '.') {
  1145. s->time->us = timelib_get_frac_nr(&ptr);
  1146. }
  1147. }
  1148. }
  1149. if (*ptr != '\0') {
  1150. s->time->z = timelib_parse_zone(&ptr, &s->time->dst, s->time, &tz_not_found, s->tzdb, tz_get_wrapper);
  1151. if (tz_not_found) {
  1152. add_error(s, TIMELIB_ERR_TZID_NOT_FOUND, "The timezone could not be found in the database");
  1153. }
  1154. }
  1155. TIMELIB_DEINIT;
  1156. return TIMELIB_TIME24_WITH_ZONE;
  1157. }
  1158. gnunocolon
  1159. {
  1160. DEBUG_OUTPUT("gnunocolon");
  1161. TIMELIB_INIT;
  1162. switch (s->time->have_time) {
  1163. case 0:
  1164. s->time->h = timelib_get_nr(&ptr, 2);
  1165. s->time->i = timelib_get_nr(&ptr, 2);
  1166. s->time->s = 0;
  1167. break;
  1168. case 1:
  1169. s->time->y = timelib_get_nr(&ptr, 4);
  1170. break;
  1171. default:
  1172. TIMELIB_DEINIT;
  1173. add_error(s, TIMELIB_ERR_DOUBLE_TIME, "Double time specification");
  1174. return TIMELIB_ERROR;
  1175. }
  1176. s->time->have_time++;
  1177. TIMELIB_DEINIT;
  1178. return TIMELIB_GNU_NOCOLON;
  1179. }
  1180. /*
  1181. gnunocolontz
  1182. {
  1183. DEBUG_OUTPUT("gnunocolontz");
  1184. TIMELIB_INIT;
  1185. switch (s->time->have_time) {
  1186. case 0:
  1187. s->time->h = timelib_get_nr(&ptr, 2);
  1188. s->time->i = timelib_get_nr(&ptr, 2);
  1189. s->time->s = 0;
  1190. s->time->z = timelib_parse_zone(&ptr, &s->time->dst, s->time, s->tzdb, tz_get_wrapper);
  1191. break;
  1192. case 1:
  1193. s->time->y = timelib_get_nr(&ptr, 4);
  1194. break;
  1195. default:
  1196. TIMELIB_DEINIT;
  1197. return TIMELIB_ERROR;
  1198. }
  1199. s->time->have_time++;
  1200. TIMELIB_DEINIT;
  1201. return TIMELIB_GNU_NOCOLON_TZ;
  1202. }
  1203. */
  1204. iso8601nocolon /*| iso8601nocolontz*/
  1205. {
  1206. int tz_not_found;
  1207. DEBUG_OUTPUT("iso8601nocolon");
  1208. TIMELIB_INIT;
  1209. TIMELIB_HAVE_TIME();
  1210. s->time->h = timelib_get_nr(&ptr, 2);
  1211. s->time->i = timelib_get_nr(&ptr, 2);
  1212. s->time->s = timelib_get_nr(&ptr, 2);
  1213. if (*ptr != '\0') {
  1214. s->time->z = timelib_parse_zone(&ptr, &s->time->dst, s->time, &tz_not_found, s->tzdb, tz_get_wrapper);
  1215. if (tz_not_found) {
  1216. add_error(s, TIMELIB_ERR_TZID_NOT_FOUND, "The timezone could not be found in the database");
  1217. }
  1218. }
  1219. TIMELIB_DEINIT;
  1220. return TIMELIB_ISO_NOCOLON;
  1221. }
  1222. americanshort | american
  1223. {
  1224. int length = 0;
  1225. DEBUG_OUTPUT("americanshort | american");
  1226. TIMELIB_INIT;
  1227. TIMELIB_HAVE_DATE();
  1228. s->time->m = timelib_get_nr(&ptr, 2);
  1229. s->time->d = timelib_get_nr(&ptr, 2);
  1230. if (*ptr == '/') {
  1231. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1232. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1233. }
  1234. TIMELIB_DEINIT;
  1235. return TIMELIB_AMERICAN;
  1236. }
  1237. iso8601date4 | iso8601dateslash | dateslash
  1238. {
  1239. DEBUG_OUTPUT("iso8601date4 | iso8601date2 | iso8601dateslash | dateslash");
  1240. TIMELIB_INIT;
  1241. TIMELIB_HAVE_DATE();
  1242. s->time->y = timelib_get_signed_nr(s, &ptr, 4);
  1243. s->time->m = timelib_get_nr(&ptr, 2);
  1244. s->time->d = timelib_get_nr(&ptr, 2);
  1245. TIMELIB_DEINIT;
  1246. return TIMELIB_ISO_DATE;
  1247. }
  1248. iso8601date2
  1249. {
  1250. int length = 0;
  1251. DEBUG_OUTPUT("iso8601date2");
  1252. TIMELIB_INIT;
  1253. TIMELIB_HAVE_DATE();
  1254. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1255. s->time->m = timelib_get_nr(&ptr, 2);
  1256. s->time->d = timelib_get_nr(&ptr, 2);
  1257. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1258. TIMELIB_DEINIT;
  1259. return TIMELIB_ISO_DATE;
  1260. }
  1261. iso8601datex
  1262. {
  1263. DEBUG_OUTPUT("iso8601datex");
  1264. TIMELIB_INIT;
  1265. TIMELIB_HAVE_DATE();
  1266. s->time->y = timelib_get_signed_nr(s, &ptr, 19);
  1267. s->time->m = timelib_get_nr(&ptr, 2);
  1268. s->time->d = timelib_get_nr(&ptr, 2);
  1269. TIMELIB_DEINIT;
  1270. return TIMELIB_ISO_DATE;
  1271. }
  1272. gnudateshorter
  1273. {
  1274. int length = 0;
  1275. DEBUG_OUTPUT("gnudateshorter");
  1276. TIMELIB_INIT;
  1277. TIMELIB_HAVE_DATE();
  1278. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1279. s->time->m = timelib_get_nr(&ptr, 2);
  1280. s->time->d = 1;
  1281. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1282. TIMELIB_DEINIT;
  1283. return TIMELIB_ISO_DATE;
  1284. }
  1285. gnudateshort
  1286. {
  1287. int length = 0;
  1288. DEBUG_OUTPUT("gnudateshort");
  1289. TIMELIB_INIT;
  1290. TIMELIB_HAVE_DATE();
  1291. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1292. s->time->m = timelib_get_nr(&ptr, 2);
  1293. s->time->d = timelib_get_nr(&ptr, 2);
  1294. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1295. TIMELIB_DEINIT;
  1296. return TIMELIB_ISO_DATE;
  1297. }
  1298. datefull
  1299. {
  1300. int length = 0;
  1301. DEBUG_OUTPUT("datefull");
  1302. TIMELIB_INIT;
  1303. TIMELIB_HAVE_DATE();
  1304. s->time->d = timelib_get_nr(&ptr, 2);
  1305. timelib_skip_day_suffix(&ptr);
  1306. s->time->m = timelib_get_month(&ptr);
  1307. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1308. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1309. TIMELIB_DEINIT;
  1310. return TIMELIB_DATE_FULL;
  1311. }
  1312. pointeddate4
  1313. {
  1314. DEBUG_OUTPUT("pointed date YYYY");
  1315. TIMELIB_INIT;
  1316. TIMELIB_HAVE_DATE();
  1317. s->time->d = timelib_get_nr(&ptr, 2);
  1318. s->time->m = timelib_get_nr(&ptr, 2);
  1319. s->time->y = timelib_get_nr(&ptr, 4);
  1320. TIMELIB_DEINIT;
  1321. return TIMELIB_DATE_FULL_POINTED;
  1322. }
  1323. pointeddate2
  1324. {
  1325. int length = 0;
  1326. DEBUG_OUTPUT("pointed date YY");
  1327. TIMELIB_INIT;
  1328. TIMELIB_HAVE_DATE();
  1329. s->time->d = timelib_get_nr(&ptr, 2);
  1330. s->time->m = timelib_get_nr(&ptr, 2);
  1331. s->time->y = timelib_get_nr_ex(&ptr, 2, &length);
  1332. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1333. TIMELIB_DEINIT;
  1334. return TIMELIB_DATE_FULL_POINTED;
  1335. }
  1336. datenoday
  1337. {
  1338. int length = 0;
  1339. DEBUG_OUTPUT("datenoday");
  1340. TIMELIB_INIT;
  1341. TIMELIB_HAVE_DATE();
  1342. s->time->m = timelib_get_month(&ptr);
  1343. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1344. s->time->d = 1;
  1345. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1346. TIMELIB_DEINIT;
  1347. return TIMELIB_DATE_NO_DAY;
  1348. }
  1349. datenodayrev
  1350. {
  1351. int length = 0;
  1352. DEBUG_OUTPUT("datenodayrev");
  1353. TIMELIB_INIT;
  1354. TIMELIB_HAVE_DATE();
  1355. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1356. s->time->m = timelib_get_month(&ptr);
  1357. s->time->d = 1;
  1358. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1359. TIMELIB_DEINIT;
  1360. return TIMELIB_DATE_NO_DAY;
  1361. }
  1362. datetextual | datenoyear
  1363. {
  1364. int length = 0;
  1365. DEBUG_OUTPUT("datetextual | datenoyear");
  1366. TIMELIB_INIT;
  1367. TIMELIB_HAVE_DATE();
  1368. s->time->m = timelib_get_month(&ptr);
  1369. s->time->d = timelib_get_nr(&ptr, 2);
  1370. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1371. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1372. TIMELIB_DEINIT;
  1373. return TIMELIB_DATE_TEXT;
  1374. }
  1375. datenoyearrev
  1376. {
  1377. DEBUG_OUTPUT("datenoyearrev");
  1378. TIMELIB_INIT;
  1379. TIMELIB_HAVE_DATE();
  1380. s->time->d = timelib_get_nr(&ptr, 2);
  1381. timelib_skip_day_suffix(&ptr);
  1382. s->time->m = timelib_get_month(&ptr);
  1383. TIMELIB_DEINIT;
  1384. return TIMELIB_DATE_TEXT;
  1385. }
  1386. datenocolon
  1387. {
  1388. DEBUG_OUTPUT("datenocolon");
  1389. TIMELIB_INIT;
  1390. TIMELIB_HAVE_DATE();
  1391. s->time->y = timelib_get_nr(&ptr, 4);
  1392. s->time->m = timelib_get_nr(&ptr, 2);
  1393. s->time->d = timelib_get_nr(&ptr, 2);
  1394. TIMELIB_DEINIT;
  1395. return TIMELIB_DATE_NOCOLON;
  1396. }
  1397. xmlrpc | xmlrpcnocolon | soap | wddx | exif
  1398. {
  1399. int tz_not_found;
  1400. DEBUG_OUTPUT("xmlrpc | xmlrpcnocolon | soap | wddx | exif");
  1401. TIMELIB_INIT;
  1402. TIMELIB_HAVE_TIME();
  1403. TIMELIB_HAVE_DATE();
  1404. s->time->y = timelib_get_nr(&ptr, 4);
  1405. s->time->m = timelib_get_nr(&ptr, 2);
  1406. s->time->d = timelib_get_nr(&ptr, 2);
  1407. s->time->h = timelib_get_nr(&ptr, 2);
  1408. s->time->i = timelib_get_nr(&ptr, 2);
  1409. s->time->s = timelib_get_nr(&ptr, 2);
  1410. if (*ptr == '.') {
  1411. s->time->us = timelib_get_frac_nr(&ptr);
  1412. if (*ptr) { /* timezone is optional */
  1413. s->time->z = timelib_parse_zone(&ptr, &s->time->dst, s->time, &tz_not_found, s->tzdb, tz_get_wrapper);
  1414. if (tz_not_found) {
  1415. add_error(s, TIMELIB_ERR_TZID_NOT_FOUND, "The timezone could not be found in the database");
  1416. }
  1417. }
  1418. }
  1419. TIMELIB_DEINIT;
  1420. return TIMELIB_XMLRPC_SOAP;
  1421. }
  1422. pgydotd
  1423. {
  1424. int length = 0;
  1425. DEBUG_OUTPUT("pgydotd");
  1426. TIMELIB_INIT;
  1427. TIMELIB_HAVE_DATE();
  1428. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1429. s->time->d = timelib_get_nr(&ptr, 3);
  1430. s->time->m = 1;
  1431. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1432. TIMELIB_DEINIT;
  1433. return TIMELIB_PG_YEARDAY;
  1434. }
  1435. isoweekday
  1436. {
  1437. timelib_sll w, d;
  1438. DEBUG_OUTPUT("isoweekday");
  1439. TIMELIB_INIT;
  1440. TIMELIB_HAVE_DATE();
  1441. TIMELIB_HAVE_RELATIVE();
  1442. s->time->y = timelib_get_nr(&ptr, 4);
  1443. w = timelib_get_nr(&ptr, 2);
  1444. d = timelib_get_nr(&ptr, 1);
  1445. s->time->m = 1;
  1446. s->time->d = 1;
  1447. s->time->relative.d = timelib_daynr_from_weeknr(s->time->y, w, d);
  1448. TIMELIB_DEINIT;
  1449. return TIMELIB_ISO_WEEK;
  1450. }
  1451. isoweek
  1452. {
  1453. timelib_sll w, d;
  1454. DEBUG_OUTPUT("isoweek");
  1455. TIMELIB_INIT;
  1456. TIMELIB_HAVE_DATE();
  1457. TIMELIB_HAVE_RELATIVE();
  1458. s->time->y = timelib_get_nr(&ptr, 4);
  1459. w = timelib_get_nr(&ptr, 2);
  1460. d = 1;
  1461. s->time->m = 1;
  1462. s->time->d = 1;
  1463. s->time->relative.d = timelib_daynr_from_weeknr(s->time->y, w, d);
  1464. TIMELIB_DEINIT;
  1465. return TIMELIB_ISO_WEEK;
  1466. }
  1467. pgtextshort
  1468. {
  1469. int length = 0;
  1470. DEBUG_OUTPUT("pgtextshort");
  1471. TIMELIB_INIT;
  1472. TIMELIB_HAVE_DATE();
  1473. s->time->m = timelib_get_month(&ptr);
  1474. s->time->d = timelib_get_nr(&ptr, 2);
  1475. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1476. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1477. TIMELIB_DEINIT;
  1478. return TIMELIB_PG_TEXT;
  1479. }
  1480. pgtextreverse
  1481. {
  1482. int length = 0;
  1483. DEBUG_OUTPUT("pgtextreverse");
  1484. TIMELIB_INIT;
  1485. TIMELIB_HAVE_DATE();
  1486. s->time->y = timelib_get_nr_ex(&ptr, 4, &length);
  1487. s->time->m = timelib_get_month(&ptr);
  1488. s->time->d = timelib_get_nr(&ptr, 2);
  1489. TIMELIB_PROCESS_YEAR(s->time->y, length);
  1490. TIMELIB_DEINIT;
  1491. return TIMELIB_PG_TEXT;
  1492. }
  1493. clf
  1494. {
  1495. int tz_not_found;
  1496. DEBUG_OUTPUT("clf");
  1497. TIMELIB_INIT;
  1498. TIMELIB_HAVE_TIME();
  1499. TIMELIB_HAVE_DATE();
  1500. s->time->d = timelib_get_nr(&ptr, 2);
  1501. s->time->m = timelib_get_month(&ptr);
  1502. s->time->y = timelib_get_nr(&ptr, 4);
  1503. s->time->h = timelib_get_nr(&ptr, 2);
  1504. s->time->i = timelib_get_nr(&ptr, 2);
  1505. s->time->s = timelib_get_nr(&ptr, 2);
  1506. s->time->z = timelib_parse_zone(&ptr, &s->time->dst, s->time, &tz_not_found, s->tzdb, tz_get_wrapper);
  1507. if (tz_not_found) {
  1508. add_error(s, TIMELIB_ERR_TZID_NOT_FOUND, "The timezone could not be found in the database");
  1509. }
  1510. TIMELIB_DEINIT;
  1511. return TIMELIB_CLF;
  1512. }
  1513. year4
  1514. {
  1515. DEBUG_OUTPUT("year4");
  1516. TIMELIB_INIT;
  1517. s->time->y = timelib_get_nr(&ptr, 4);
  1518. TIMELIB_DEINIT;
  1519. return TIMELIB_CLF;
  1520. }
  1521. ago
  1522. {
  1523. DEBUG_OUTPUT("ago");
  1524. TIMELIB_INIT;
  1525. s->time->relative.y = 0 - s->time->relative.y;
  1526. s->time->relative.m = 0 - s->time->relative.m;
  1527. s->time->relative.d = 0 - s->time->relative.d;
  1528. s->time->relative.h = 0 - s->time->relative.h;
  1529. s->time->relative.i = 0 - s->time->relative.i;
  1530. s->time->relative.s = 0 - s->time->relative.s;
  1531. s->time->relative.weekday = 0 - s->time->relative.weekday;
  1532. if (s->time->relative.weekday == 0) {
  1533. s->time->relative.weekday = -7;
  1534. }
  1535. if (s->time->relative.have_special_relative && s->time->relative.special.type == TIMELIB_SPECIAL_WEEKDAY) {
  1536. s->time->relative.special.amount = 0 - s->time->relative.special.amount;
  1537. }
  1538. TIMELIB_DEINIT;
  1539. return TIMELIB_AGO;
  1540. }
  1541. daytext
  1542. {
  1543. const timelib_relunit* relunit;
  1544. DEBUG_OUTPUT("daytext");
  1545. TIMELIB_INIT;
  1546. TIMELIB_HAVE_RELATIVE();
  1547. TIMELIB_HAVE_WEEKDAY_RELATIVE();
  1548. TIMELIB_UNHAVE_TIME();
  1549. relunit = timelib_lookup_relunit(&ptr);
  1550. s->time->relative.weekday = relunit->multiplier;
  1551. if (s->time->relative.weekday_behavior != 2) {
  1552. s->time->relative.weekday_behavior = 1;
  1553. }
  1554. TIMELIB_DEINIT;
  1555. return TIMELIB_WEEKDAY;
  1556. }
  1557. relativetextweek
  1558. {
  1559. timelib_sll i;
  1560. int behavior = 0;
  1561. DEBUG_OUTPUT("relativetextweek");
  1562. TIMELIB_INIT;
  1563. TIMELIB_HAVE_RELATIVE();
  1564. while(*ptr) {
  1565. i = timelib_get_relative_text(&ptr, &behavior);
  1566. timelib_eat_spaces(&ptr);
  1567. timelib_set_relative(&ptr, i, behavior, s, TIMELIB_TIME_PART_DONT_KEEP);
  1568. s->time->relative.weekday_behavior = 2;
  1569. /* to handle the format weekday + last/this/next week */
  1570. if (s->time->relative.have_weekday_relative == 0) {
  1571. TIMELIB_HAVE_WEEKDAY_RELATIVE();
  1572. s->time->relative.weekday = 1;
  1573. }
  1574. }
  1575. TIMELIB_DEINIT;
  1576. return TIMELIB_RELATIVE;
  1577. }
  1578. relativetext
  1579. {
  1580. timelib_sll i;
  1581. int behavior = 0;
  1582. DEBUG_OUTPUT("relativetext");
  1583. TIMELIB_INIT;
  1584. TIMELIB_HAVE_RELATIVE();
  1585. while(*ptr) {
  1586. i = timelib_get_relative_text(&ptr, &behavior);
  1587. timelib_eat_spaces(&ptr);
  1588. timelib_set_relative(&ptr, i, behavior, s, TIMELIB_TIME_PART_DONT_KEEP);
  1589. }
  1590. TIMELIB_DEINIT;
  1591. return TIMELIB_RELATIVE;
  1592. }
  1593. monthfull | monthabbr
  1594. {
  1595. DEBUG_OUTPUT("monthtext");
  1596. TIMELIB_INIT;
  1597. TIMELIB_HAVE_DATE();
  1598. s->time->m = timelib_lookup_month(&ptr);
  1599. TIMELIB_DEINIT;
  1600. return TIMELIB_DATE_TEXT;
  1601. }
  1602. tzcorrection | tz
  1603. {
  1604. int tz_not_found;
  1605. DEBUG_OUTPUT("tzcorrection | tz");
  1606. TIMELIB_INIT;
  1607. TIMELIB_HAVE_TZ();
  1608. s->time->z = timelib_parse_zone(&ptr, &s->time->dst, s->time, &tz_not_found, s->tzdb, tz_get_wrapper);
  1609. if (tz_not_found) {
  1610. add_error(s, TIMELIB_ERR_TZID_NOT_FOUND, "The timezone could not be found in the database");
  1611. }
  1612. TIMELIB_DEINIT;
  1613. return TIMELIB_TIMEZONE;
  1614. }
  1615. dateshortwithtimeshort12 | dateshortwithtimelong12
  1616. {
  1617. DEBUG_OUTPUT("dateshortwithtimeshort12 | dateshortwithtimelong12");
  1618. TIMELIB_INIT;
  1619. TIMELIB_HAVE_DATE();
  1620. s->time->m = timelib_get_month(&ptr);
  1621. s->time->d = timelib_get_nr(&ptr, 2);
  1622. TIMELIB_HAVE_TIME();
  1623. s->time->h = timelib_get_nr(&ptr, 2);
  1624. s->time->i = timelib_get_nr(&ptr, 2);
  1625. if (*ptr == ':' || *ptr == '.') {
  1626. s->time->s = timelib_get_nr(&ptr, 2);
  1627. if (*ptr == '.') {
  1628. s->time->us = timelib_get_frac_nr(&ptr);
  1629. }
  1630. }
  1631. s->time->h += timelib_meridian(&ptr, s->time->h);
  1632. TIMELIB_DEINIT;
  1633. return TIMELIB_SHORTDATE_WITH_TIME;
  1634. }
  1635. dateshortwithtimeshort | dateshortwithtimelong | dateshortwithtimelongtz
  1636. {
  1637. int tz_not_found;
  1638. DEBUG_OUTPUT("dateshortwithtimeshort | dateshortwithtimelong | dateshortwithtimelongtz");
  1639. TIMELIB_INIT;
  1640. TIMELIB_HAVE_DATE();
  1641. s->time->m = timelib_get_month(&ptr);
  1642. s->time->d = timelib_get_nr(&ptr, 2);
  1643. TIMELIB_HAVE_TIME();
  1644. s->time->h = timelib_get_nr(&ptr, 2);
  1645. s->time->i = timelib_get_nr(&ptr, 2);
  1646. if (*ptr == ':') {
  1647. s->time->s = timelib_get_nr(&ptr, 2);
  1648. if (*ptr == '.') {
  1649. s->time->us = timelib_get_frac_nr(&ptr);
  1650. }
  1651. }
  1652. if (*ptr != '\0') {
  1653. s->time->z = timelib_parse_zone(&ptr, &s->time->dst, s->time, &tz_not_found, s->tzdb, tz_get_wrapper);
  1654. if (tz_not_found) {
  1655. add_error(s, TIMELIB_ERR_TZID_NOT_FOUND, "The timezone could not be found in the database");
  1656. }
  1657. }
  1658. TIMELIB_DEINIT;
  1659. return TIMELIB_SHORTDATE_WITH_TIME;
  1660. }
  1661. relative
  1662. {
  1663. timelib_ull i;
  1664. DEBUG_OUTPUT("relative");
  1665. TIMELIB_INIT;
  1666. TIMELIB_HAVE_RELATIVE();
  1667. while(*ptr) {
  1668. i = timelib_get_signed_nr(s, &ptr, 24);
  1669. timelib_eat_spaces(&ptr);
  1670. timelib_set_relative(&ptr, i, 1, s, TIMELIB_TIME_PART_KEEP);
  1671. }
  1672. TIMELIB_DEINIT;
  1673. return TIMELIB_RELATIVE;
  1674. }
  1675. [ .,\t]
  1676. {
  1677. goto std;
  1678. }
  1679. "\000"|"\n"
  1680. {
  1681. s->pos = cursor; s->line++;
  1682. goto std;
  1683. }
  1684. any
  1685. {
  1686. add_error(s, TIMELIB_ERR_UNEXPECTED_CHARACTER, "Unexpected character");
  1687. goto std;
  1688. }
  1689. */
  1690. }
  1691. /*!max:re2c */
  1692. timelib_time *timelib_strtotime(const char *s, size_t len, timelib_error_container **errors, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_get_wrapper)
  1693. {
  1694. Scanner in;
  1695. int t;
  1696. const char *e = s + len - 1;
  1697. memset(&in, 0, sizeof(in));
  1698. in.errors = timelib_malloc(sizeof(timelib_error_container));
  1699. in.errors->warning_count = 0;
  1700. in.errors->warning_messages = NULL;
  1701. in.errors->error_count = 0;
  1702. in.errors->error_messages = NULL;
  1703. if (len > 0) {
  1704. while (isspace(*s) && s < e) {
  1705. s++;
  1706. }
  1707. while (isspace(*e) && e > s) {
  1708. e--;
  1709. }
  1710. }
  1711. if (e - s < 0) {
  1712. in.time = timelib_time_ctor();
  1713. add_error(&in, TIMELIB_ERR_EMPTY_STRING, "Empty string");
  1714. if (errors) {
  1715. *errors = in.errors;
  1716. } else {
  1717. timelib_error_container_dtor(in.errors);
  1718. }
  1719. in.time->y = in.time->d = in.time->m = in.time->h = in.time->i = in.time->s = in.time->us = in.time->dst = in.time->z = TIMELIB_UNSET;
  1720. in.time->is_localtime = in.time->zone_type = 0;
  1721. return in.time;
  1722. }
  1723. e++;
  1724. in.str = timelib_malloc((e - s) + YYMAXFILL);
  1725. memset(in.str, 0, (e - s) + YYMAXFILL);
  1726. memcpy(in.str, s, (e - s));
  1727. in.lim = in.str + (e - s) + YYMAXFILL;
  1728. in.cur = in.str;
  1729. in.time = timelib_time_ctor();
  1730. in.time->y = TIMELIB_UNSET;
  1731. in.time->d = TIMELIB_UNSET;
  1732. in.time->m = TIMELIB_UNSET;
  1733. in.time->h = TIMELIB_UNSET;
  1734. in.time->i = TIMELIB_UNSET;
  1735. in.time->s = TIMELIB_UNSET;
  1736. in.time->us = TIMELIB_UNSET;
  1737. in.time->z = TIMELIB_UNSET;
  1738. in.time->dst = TIMELIB_UNSET;
  1739. in.tzdb = tzdb;
  1740. in.time->is_localtime = 0;
  1741. in.time->zone_type = 0;
  1742. in.time->relative.days = TIMELIB_UNSET;
  1743. do {
  1744. t = scan(&in, tz_get_wrapper);
  1745. #ifdef DEBUG_PARSER
  1746. printf("%d\n", t);
  1747. #endif
  1748. } while(t != EOI);
  1749. /* do funky checking whether the parsed time was valid time */
  1750. if (in.time->have_time && !timelib_valid_time( in.time->h, in.time->i, in.time->s)) {
  1751. add_warning(&in, TIMELIB_WARN_INVALID_TIME, "The parsed time was invalid");
  1752. }
  1753. /* do funky checking whether the parsed date was valid date */
  1754. if (in.time->have_date && !timelib_valid_date( in.time->y, in.time->m, in.time->d)) {
  1755. add_warning(&in, TIMELIB_WARN_INVALID_DATE, "The parsed date was invalid");
  1756. }
  1757. timelib_free(in.str);
  1758. if (errors) {
  1759. *errors = in.errors;
  1760. } else {
  1761. timelib_error_container_dtor(in.errors);
  1762. }
  1763. return in.time;
  1764. }
  1765. #define TIMELIB_CHECK_NUMBER \
  1766. if (strchr("0123456789", *ptr) == NULL) \
  1767. { \
  1768. add_pbf_error(s, TIMELIB_ERR_UNEXPECTED_DATA, "Unexpected data found.", string, begin); \
  1769. }
  1770. #define TIMELIB_CHECK_SIGNED_NUMBER \
  1771. if (strchr("-0123456789", *ptr) == NULL) \
  1772. { \
  1773. add_pbf_error(s, TIMELIB_ERR_UNEXPECTED_DATA, "Unexpected data found.", string, begin); \
  1774. }
  1775. static void timelib_time_reset_fields(timelib_time *time)
  1776. {
  1777. assert(time != NULL);
  1778. time->y = 1970;
  1779. time->m = 1;
  1780. time->d = 1;
  1781. time->h = time->i = time->s = 0;
  1782. time->us = 0;
  1783. time->tz_info = NULL;
  1784. }
  1785. static void timelib_time_reset_unset_fields(timelib_time *time)
  1786. {
  1787. assert(time != NULL);
  1788. if (time->y == TIMELIB_UNSET ) time->y = 1970;
  1789. if (time->m == TIMELIB_UNSET ) time->m = 1;
  1790. if (time->d == TIMELIB_UNSET ) time->d = 1;
  1791. if (time->h == TIMELIB_UNSET ) time->h = 0;
  1792. if (time->i == TIMELIB_UNSET ) time->i = 0;
  1793. if (time->s == TIMELIB_UNSET ) time->s = 0;
  1794. if (time->us == TIMELIB_UNSET ) time->us = 0;
  1795. }
  1796. static const timelib_format_specifier default_format_map[] = {
  1797. {'+', TIMELIB_FORMAT_ALLOW_EXTRA_CHARACTERS},
  1798. {'#', TIMELIB_FORMAT_ANY_SEPARATOR},
  1799. {'j', TIMELIB_FORMAT_DAY_TWO_DIGIT},
  1800. {'d', TIMELIB_FORMAT_DAY_TWO_DIGIT_PADDED},
  1801. {'z', TIMELIB_FORMAT_DAY_OF_YEAR},
  1802. {'S', TIMELIB_FORMAT_DAY_SUFFIX},
  1803. {'U', TIMELIB_FORMAT_EPOCH_SECONDS},
  1804. {'\\', TIMELIB_FORMAT_ESCAPE},
  1805. {'h', TIMELIB_FORMAT_HOUR_TWO_DIGIT_12_MAX},
  1806. {'g', TIMELIB_FORMAT_HOUR_TWO_DIGIT_12_MAX_PADDED},
  1807. {'H', TIMELIB_FORMAT_HOUR_TWO_DIGIT_24_MAX},
  1808. {'G', TIMELIB_FORMAT_HOUR_TWO_DIGIT_24_MAX_PADDED},
  1809. {'a', TIMELIB_FORMAT_MERIDIAN},
  1810. {'A', TIMELIB_FORMAT_MERIDIAN},
  1811. {'u', TIMELIB_FORMAT_MICROSECOND_SIX_DIGIT},
  1812. {'v', TIMELIB_FORMAT_MILLISECOND_THREE_DIGIT},
  1813. {'i', TIMELIB_FORMAT_MINUTE_TWO_DIGIT},
  1814. {'n', TIMELIB_FORMAT_MONTH_TWO_DIGIT},
  1815. {'m', TIMELIB_FORMAT_MONTH_TWO_DIGIT_PADDED},
  1816. {'?', TIMELIB_FORMAT_RANDOM_CHAR},
  1817. {'!', TIMELIB_FORMAT_RESET_ALL},
  1818. {'|', TIMELIB_FORMAT_RESET_ALL_WHEN_NOT_SET},
  1819. {'s', TIMELIB_FORMAT_SECOND_TWO_DIGIT},
  1820. {';', TIMELIB_FORMAT_SEPARATOR},
  1821. {':', TIMELIB_FORMAT_SEPARATOR},
  1822. {'/', TIMELIB_FORMAT_SEPARATOR},
  1823. {'.', TIMELIB_FORMAT_SEPARATOR},
  1824. {',', TIMELIB_FORMAT_SEPARATOR},
  1825. {'-', TIMELIB_FORMAT_SEPARATOR},
  1826. {'(', TIMELIB_FORMAT_SEPARATOR},
  1827. {')', TIMELIB_FORMAT_SEPARATOR},
  1828. {'*', TIMELIB_FORMAT_SKIP_TO_SEPARATOR},
  1829. {'D', TIMELIB_FORMAT_TEXTUAL_DAY_3_LETTER},
  1830. {'l', TIMELIB_FORMAT_TEXTUAL_DAY_FULL},
  1831. {'M', TIMELIB_FORMAT_TEXTUAL_MONTH_3_LETTER},
  1832. {'F', TIMELIB_FORMAT_TEXTUAL_MONTH_FULL},
  1833. {'e', TIMELIB_FORMAT_TIMEZONE_OFFSET},
  1834. {'P', TIMELIB_FORMAT_TIMEZONE_OFFSET},
  1835. {'p', TIMELIB_FORMAT_TIMEZONE_OFFSET},
  1836. {'T', TIMELIB_FORMAT_TIMEZONE_OFFSET},
  1837. {'O', TIMELIB_FORMAT_TIMEZONE_OFFSET},
  1838. {' ', TIMELIB_FORMAT_WHITESPACE},
  1839. {'y', TIMELIB_FORMAT_YEAR_TWO_DIGIT},
  1840. {'Y', TIMELIB_FORMAT_YEAR_FOUR_DIGIT},
  1841. {'\0', TIMELIB_FORMAT_END}
  1842. };
  1843. static const timelib_format_config default_format_config = {
  1844. default_format_map,
  1845. // No prefix required by default.
  1846. '\0'
  1847. };
  1848. static timelib_format_specifier_code timelib_lookup_format(char input, const timelib_format_specifier* format_map)
  1849. {
  1850. while (format_map && format_map->specifier != '\0') {
  1851. if (format_map->specifier == input) {
  1852. return format_map->code;
  1853. }
  1854. format_map++;
  1855. }
  1856. return TIMELIB_FORMAT_LITERAL;
  1857. }
  1858. timelib_time *timelib_parse_from_format(const char *format, const char *string, size_t len, timelib_error_container **errors, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_get_wrapper)
  1859. {
  1860. return timelib_parse_from_format_with_map(format, string, len, errors, tzdb, tz_get_wrapper, &default_format_config);
  1861. }
  1862. timelib_time *timelib_parse_from_format_with_map(const char *format, const char *string, size_t len, timelib_error_container **errors, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_get_wrapper, const timelib_format_config* format_config)
  1863. {
  1864. const char *fptr = format;
  1865. const char *ptr = string;
  1866. const char *begin;
  1867. timelib_sll tmp;
  1868. Scanner in;
  1869. Scanner *s = &in;
  1870. bool allow_extra = false;
  1871. bool prefix_found = false;
  1872. int iso_year = TIMELIB_UNSET;
  1873. int iso_week_of_year = TIMELIB_UNSET;
  1874. int iso_day_of_week = TIMELIB_UNSET;
  1875. char prefix_char = format_config->prefix_char;
  1876. const timelib_format_specifier *format_map = format_config->format_map;
  1877. memset(&in, 0, sizeof(in));
  1878. in.errors = timelib_malloc(sizeof(timelib_error_container));
  1879. in.errors->warning_count = 0;
  1880. in.errors->warning_messages = NULL;
  1881. in.errors->error_count = 0;
  1882. in.errors->error_messages = NULL;
  1883. in.time = timelib_time_ctor();
  1884. in.time->y = TIMELIB_UNSET;
  1885. in.time->d = TIMELIB_UNSET;
  1886. in.time->m = TIMELIB_UNSET;
  1887. in.time->h = TIMELIB_UNSET;
  1888. in.time->i = TIMELIB_UNSET;
  1889. in.time->s = TIMELIB_UNSET;
  1890. in.time->us = TIMELIB_UNSET;
  1891. in.time->z = TIMELIB_UNSET;
  1892. in.time->dst = TIMELIB_UNSET;
  1893. in.tzdb = tzdb;
  1894. in.time->is_localtime = 0;
  1895. in.time->zone_type = 0;
  1896. /* Loop over the format string */
  1897. while (*fptr && *ptr) {
  1898. begin = ptr;
  1899. if (prefix_char) {
  1900. /* There are 2 cases where the input string and format string
  1901. * should match the next literal:
  1902. *
  1903. * 1. No prefix has been specified yet in the format, so expect 1:1
  1904. * match.
  1905. * 2. Sequential prefix characters indicating that the second
  1906. * prefix is escaped. (e.g. "%%" is expecting literal "%")
  1907. */
  1908. if ((!prefix_found && *fptr != prefix_char) ||
  1909. (prefix_found && *fptr == prefix_char)) {
  1910. if (*fptr != *ptr) {
  1911. add_pbf_error(s, TIMELIB_ERR_FORMAT_LITERAL_MISMATCH, "Format literal not found", string, begin);
  1912. }
  1913. ptr++;
  1914. fptr++;
  1915. prefix_found = false;
  1916. continue;
  1917. }
  1918. if (*fptr == prefix_char) {
  1919. fptr++;
  1920. prefix_found = true;
  1921. continue;
  1922. }
  1923. /* Fall through case is that the prefix has been found and the next
  1924. * character is the format specifier. */
  1925. prefix_found = false;
  1926. }
  1927. switch (timelib_lookup_format(*fptr, format_map)) {
  1928. case TIMELIB_FORMAT_TEXTUAL_DAY_3_LETTER: /* three letter day */
  1929. case TIMELIB_FORMAT_TEXTUAL_DAY_FULL: /* full day */
  1930. {
  1931. const timelib_relunit* tmprel = 0;
  1932. tmprel = timelib_lookup_relunit(&ptr);
  1933. if (!tmprel) {
  1934. add_pbf_error(s, TIMELIB_ERR_NO_TEXTUAL_DAY, "A textual day could not be found", string, begin);
  1935. break;
  1936. } else {
  1937. in.time->have_relative = 1;
  1938. in.time->relative.have_weekday_relative = 1;
  1939. in.time->relative.weekday = tmprel->multiplier;
  1940. in.time->relative.weekday_behavior = 1;
  1941. }
  1942. }
  1943. break;
  1944. case TIMELIB_FORMAT_DAY_TWO_DIGIT: /* two digit day, without leading zero */
  1945. case TIMELIB_FORMAT_DAY_TWO_DIGIT_PADDED: /* two digit day, with leading zero */
  1946. TIMELIB_CHECK_NUMBER;
  1947. if ((s->time->d = timelib_get_nr(&ptr, 2)) == TIMELIB_UNSET) {
  1948. add_pbf_error(s, TIMELIB_ERR_NO_TWO_DIGIT_DAY, "A two digit day could not be found", string, begin);
  1949. break;
  1950. }
  1951. s->time->have_date = 1;
  1952. break;
  1953. case TIMELIB_FORMAT_DAY_SUFFIX: /* day suffix, ignored, nor checked */
  1954. timelib_skip_day_suffix(&ptr);
  1955. break;
  1956. case TIMELIB_FORMAT_DAY_OF_YEAR: /* day of year - resets month (0 based) - also initializes everything else to !TIMELIB_UNSET */
  1957. TIMELIB_CHECK_NUMBER;
  1958. if (s->time->y == TIMELIB_UNSET) {
  1959. add_pbf_error(s, TIMELIB_ERR_MERIDIAN_BEFORE_HOUR, "A 'day of year' can only come after a year has been found", string, begin);
  1960. }
  1961. if ((tmp = timelib_get_nr(&ptr, 3)) == TIMELIB_UNSET) {
  1962. add_pbf_error(s, TIMELIB_ERR_NO_THREE_DIGIT_DAY_OF_YEAR, "A three digit day-of-year could not be found", string, begin);
  1963. break;
  1964. }
  1965. if (s->time->y != TIMELIB_UNSET) {
  1966. s->time->have_date = 1;
  1967. s->time->m = 1;
  1968. s->time->d = tmp + 1;
  1969. timelib_do_normalize(s->time);
  1970. }
  1971. break;
  1972. case TIMELIB_FORMAT_MONTH_TWO_DIGIT: /* two digit month, without leading zero */
  1973. case TIMELIB_FORMAT_MONTH_TWO_DIGIT_PADDED: /* two digit month, with leading zero */
  1974. TIMELIB_CHECK_NUMBER;
  1975. if ((s->time->m = timelib_get_nr(&ptr, 2)) == TIMELIB_UNSET) {
  1976. add_pbf_error(s, TIMELIB_ERR_NO_TWO_DIGIT_MONTH, "A two digit month could not be found", string, begin);
  1977. break;
  1978. }
  1979. s->time->have_date = 1;
  1980. break;
  1981. case TIMELIB_FORMAT_TEXTUAL_MONTH_3_LETTER: /* three letter month */
  1982. case TIMELIB_FORMAT_TEXTUAL_MONTH_FULL: /* full month */
  1983. tmp = timelib_lookup_month(&ptr);
  1984. if (!tmp) {
  1985. add_pbf_error(s, TIMELIB_ERR_NO_TEXTUAL_MONTH, "A textual month could not be found", string, begin);
  1986. break;
  1987. }
  1988. s->time->have_date = 1;
  1989. s->time->m = tmp;
  1990. break;
  1991. case TIMELIB_FORMAT_YEAR_TWO_DIGIT: /* two digit year */
  1992. {
  1993. int length = 0;
  1994. TIMELIB_CHECK_NUMBER;
  1995. if ((s->time->y = timelib_get_nr_ex(&ptr, 2, &length)) == TIMELIB_UNSET) {
  1996. add_pbf_error(s, TIMELIB_ERR_NO_TWO_DIGIT_YEAR, "A two digit year could not be found", string, begin);
  1997. break;
  1998. }
  1999. s->time->have_date = 1;
  2000. TIMELIB_PROCESS_YEAR(s->time->y, length);
  2001. }
  2002. break;
  2003. case TIMELIB_FORMAT_YEAR_FOUR_DIGIT: /* four digit year */
  2004. TIMELIB_CHECK_NUMBER;
  2005. if ((s->time->y = timelib_get_nr(&ptr, 4)) == TIMELIB_UNSET) {
  2006. add_pbf_error(s, TIMELIB_ERR_NO_FOUR_DIGIT_YEAR, "A four digit year could not be found", string, begin);
  2007. break;
  2008. }
  2009. s->time->have_date = 1;
  2010. break;
  2011. case TIMELIB_FORMAT_HOUR_TWO_DIGIT_12_MAX: /* two digit hour, without leading zero */
  2012. case TIMELIB_FORMAT_HOUR_TWO_DIGIT_12_MAX_PADDED: /* two digit hour, with leading zero */
  2013. TIMELIB_CHECK_NUMBER;
  2014. if ((s->time->h = timelib_get_nr(&ptr, 2)) == TIMELIB_UNSET) {
  2015. add_pbf_error(s, TIMELIB_ERR_NO_TWO_DIGIT_HOUR, "A two digit hour could not be found", string, begin);
  2016. break;
  2017. }
  2018. if (s->time->h > 12) {
  2019. add_pbf_error(s, TIMELIB_ERR_HOUR_LARGER_THAN_12, "Hour cannot be higher than 12", string, begin);
  2020. break;
  2021. }
  2022. s->time->have_time = 1;
  2023. break;
  2024. case TIMELIB_FORMAT_HOUR_TWO_DIGIT_24_MAX_PADDED: /* two digit hour, with leading zero */
  2025. case TIMELIB_FORMAT_HOUR_TWO_DIGIT_24_MAX: /* two digit hour, without leading zero */
  2026. TIMELIB_CHECK_NUMBER;
  2027. if ((s->time->h = timelib_get_nr(&ptr, 2)) == TIMELIB_UNSET) {
  2028. add_pbf_error(s, TIMELIB_ERR_NO_TWO_DIGIT_HOUR, "A two digit hour could not be found", string, begin);
  2029. break;
  2030. }
  2031. s->time->have_time = 1;
  2032. break;
  2033. case TIMELIB_FORMAT_MERIDIAN: /* am/pm/a.m./p.m. AM/PM/A.M./P.M. */
  2034. if (s->time->h == TIMELIB_UNSET) {
  2035. add_pbf_error(s, TIMELIB_ERR_MERIDIAN_BEFORE_HOUR, "Meridian can only come after an hour has been found", string, begin);
  2036. }
  2037. if ((tmp = timelib_meridian_with_check(&ptr, s->time->h)) == TIMELIB_UNSET) {
  2038. add_pbf_error(s, TIMELIB_ERR_NO_MERIDIAN, "A meridian could not be found", string, begin);
  2039. break;
  2040. }
  2041. s->time->have_time = 1;
  2042. if (s->time->h != TIMELIB_UNSET) {
  2043. s->time->h += tmp;
  2044. }
  2045. break;
  2046. case TIMELIB_FORMAT_MINUTE_TWO_DIGIT: /* two digit minute, with leading zero */
  2047. {
  2048. int length;
  2049. timelib_sll min;
  2050. TIMELIB_CHECK_NUMBER;
  2051. min = timelib_get_nr_ex(&ptr, 2, &length);
  2052. if (min == TIMELIB_UNSET || length != 2) {
  2053. add_pbf_error(s, TIMELIB_ERR_NO_TWO_DIGIT_MINUTE, "A two digit minute could not be found", string, begin);
  2054. break;
  2055. }
  2056. s->time->have_time = 1;
  2057. s->time->i = min;
  2058. }
  2059. break;
  2060. case TIMELIB_FORMAT_SECOND_TWO_DIGIT: /* two digit second, with leading zero */
  2061. {
  2062. int length;
  2063. timelib_sll sec;
  2064. TIMELIB_CHECK_NUMBER;
  2065. sec = timelib_get_nr_ex(&ptr, 2, &length);
  2066. if (sec == TIMELIB_UNSET || length != 2) {
  2067. add_pbf_error(s, TIMELIB_ERR_NO_TWO_DIGIT_SECOND, "A two digit second could not be found", string, begin);
  2068. break;
  2069. }
  2070. s->time->have_time = 1;
  2071. s->time->s = sec;
  2072. }
  2073. break;
  2074. case TIMELIB_FORMAT_MICROSECOND_SIX_DIGIT: /* up to six digit microsecond */
  2075. {
  2076. double f;
  2077. const char *tptr;
  2078. TIMELIB_CHECK_NUMBER;
  2079. tptr = ptr;
  2080. if ((f = timelib_get_nr(&ptr, 6)) == TIMELIB_UNSET || (ptr - tptr < 1)) {
  2081. add_pbf_error(s, TIMELIB_ERR_NO_SIX_DIGIT_MICROSECOND, "A six digit microsecond could not be found", string, begin);
  2082. break;
  2083. }
  2084. s->time->us = (f * pow(10, 6 - (ptr - tptr)));
  2085. }
  2086. break;
  2087. case TIMELIB_FORMAT_MILLISECOND_THREE_DIGIT: /* up to three digit millisecond */
  2088. {
  2089. double f;
  2090. const char *tptr;
  2091. TIMELIB_CHECK_NUMBER;
  2092. tptr = ptr;
  2093. if ((f = timelib_get_nr(&ptr, 3)) == TIMELIB_UNSET || (ptr - tptr < 1)) {
  2094. add_pbf_error(s, TIMELIB_ERR_NO_THREE_DIGIT_MILLISECOND, "A three digit millisecond could not be found", string, begin);
  2095. break;
  2096. }
  2097. s->time->us = (f * pow(10, 3 - (ptr - tptr)) * 1000);
  2098. }
  2099. break;
  2100. case TIMELIB_FORMAT_WHITESPACE: /* any sort of whitespace (' ' and \t) */
  2101. timelib_eat_spaces(&ptr);
  2102. break;
  2103. case TIMELIB_FORMAT_EPOCH_SECONDS: /* epoch seconds */
  2104. TIMELIB_CHECK_SIGNED_NUMBER;
  2105. tmp = timelib_get_signed_nr(s, &ptr, 24);
  2106. s->time->have_zone = 1;
  2107. s->time->sse = tmp;
  2108. s->time->is_localtime = 1;
  2109. s->time->zone_type = TIMELIB_ZONETYPE_OFFSET;
  2110. s->time->z = 0;
  2111. s->time->dst = 0;
  2112. timelib_update_from_sse(s->time);
  2113. break;
  2114. case TIMELIB_FORMAT_ANY_SEPARATOR: /* separation symbol */
  2115. if (timelib_lookup_format(*ptr, format_map) != TIMELIB_FORMAT_SEPARATOR) {
  2116. add_pbf_error(s, TIMELIB_ERR_NO_SEP_SYMBOL, "The separation symbol ([;:/.,-]) could not be found", string, begin);
  2117. break;
  2118. }
  2119. ++ptr;
  2120. break;
  2121. case TIMELIB_FORMAT_SEPARATOR:
  2122. if (*ptr != *fptr) {
  2123. add_pbf_error(s, TIMELIB_ERR_NO_SEP_SYMBOL, "The separation symbol could not be found", string, begin);
  2124. break;
  2125. }
  2126. ++ptr;
  2127. break;
  2128. case TIMELIB_FORMAT_RESET_ALL: /* reset all fields to default */
  2129. timelib_time_reset_fields(s->time);
  2130. break; /* break intentionally not missing */
  2131. case TIMELIB_FORMAT_RESET_ALL_WHEN_NOT_SET: /* reset all fields to default when not set */
  2132. timelib_time_reset_unset_fields(s->time);
  2133. break; /* break intentionally not missing */
  2134. case TIMELIB_FORMAT_RANDOM_CHAR: /* random char */
  2135. ++ptr;
  2136. break;
  2137. case TIMELIB_FORMAT_ESCAPE: /* escaped char */
  2138. if (!fptr[1]) {
  2139. add_pbf_error(s, TIMELIB_ERR_EXPECTED_ESCAPE_CHAR, "Escaped character expected", string, begin);
  2140. break;
  2141. }
  2142. fptr++;
  2143. if (*ptr != *fptr) {
  2144. add_pbf_error(s, TIMELIB_ERR_NO_ESCAPED_CHAR, "The escaped character could not be found", string, begin);
  2145. break;
  2146. }
  2147. ++ptr;
  2148. break;
  2149. case TIMELIB_FORMAT_SKIP_TO_SEPARATOR: /* random chars until a separator or number ([ \t.,:;/-0123456789]) */
  2150. timelib_eat_until_separator(&ptr);
  2151. break;
  2152. case TIMELIB_FORMAT_ALLOW_EXTRA_CHARACTERS: /* allow extra chars in the format */
  2153. allow_extra = true;
  2154. break;
  2155. case TIMELIB_FORMAT_YEAR_ISO:
  2156. if ((iso_year = timelib_get_nr(&ptr, 4)) == TIMELIB_UNSET) {
  2157. add_pbf_error(s, TIMELIB_ERR_NO_FOUR_DIGIT_YEAR_ISO, "A four digit ISO year could not be found", string, begin);
  2158. break;
  2159. }
  2160. s->time->have_date = 1;
  2161. break;
  2162. case TIMELIB_FORMAT_WEEK_OF_YEAR_ISO:
  2163. if ((iso_week_of_year = timelib_get_nr(&ptr, 2)) == TIMELIB_UNSET) {
  2164. add_pbf_error(s, TIMELIB_ERR_NO_TWO_DIGIT_WEEK, "A two digit ISO week could not be found", string, begin);
  2165. break;
  2166. }
  2167. /* Range is 1 - 53 for ISO week of year */
  2168. if (iso_week_of_year < 1 || iso_week_of_year > 53) {
  2169. add_pbf_error(s, TIMELIB_ERR_INVALID_WEEK, "ISO Week must be between 1 and 53", string, begin);
  2170. break;
  2171. }
  2172. s->time->have_date = 1;
  2173. break;
  2174. case TIMELIB_FORMAT_DAY_OF_WEEK_ISO:
  2175. if ((iso_day_of_week = timelib_get_nr(&ptr, 1)) == TIMELIB_UNSET) {
  2176. add_pbf_error(s, TIMELIB_ERR_NO_DAY_OF_WEEK, "A single digit day of week could not be found", string, begin);
  2177. break;
  2178. }
  2179. if (iso_day_of_week < 1 || iso_day_of_week > 7) {
  2180. add_pbf_error(s, TIMELIB_ERR_INVALID_DAY_OF_WEEK, "Day of week must be between 1 and 7", string, begin);
  2181. break;
  2182. }
  2183. s->time->have_date = 1;
  2184. break;
  2185. case TIMELIB_FORMAT_TIMEZONE_OFFSET: /* timezone */
  2186. {
  2187. int tz_not_found;
  2188. s->time->z = timelib_parse_zone(&ptr, &s->time->dst, s->time, &tz_not_found, s->tzdb, tz_get_wrapper);
  2189. if (tz_not_found) {
  2190. add_pbf_error(s, TIMELIB_ERR_TZID_NOT_FOUND, "The timezone could not be found in the database", string, begin);
  2191. break;
  2192. }
  2193. s->time->have_zone = 1;
  2194. }
  2195. break;
  2196. case TIMELIB_FORMAT_TIMEZONE_OFFSET_MINUTES: /* timezone format +/-mmm */
  2197. s->time->z = timelib_parse_tz_minutes(&ptr, s->time);
  2198. if (s->time->z == TIMELIB_UNSET) {
  2199. add_pbf_error(s, TIMELIB_ERR_INVALID_TZ_OFFSET, "Invalid timezone offset in minutes", string, begin);
  2200. break;
  2201. }
  2202. s->time->have_zone = 1;
  2203. break;
  2204. case TIMELIB_FORMAT_LITERAL:
  2205. default:
  2206. if (*fptr != *ptr) {
  2207. add_pbf_error(s, TIMELIB_ERR_WRONG_FORMAT_SEP, "The format separator does not match", string, begin);
  2208. }
  2209. ptr++;
  2210. }
  2211. fptr++;
  2212. }
  2213. if (*ptr) {
  2214. if (allow_extra) {
  2215. add_pbf_warning(s, TIMELIB_WARN_TRAILING_DATA, "Trailing data", string, ptr);
  2216. } else {
  2217. add_pbf_error(s, TIMELIB_ERR_TRAILING_DATA, "Trailing data", string, ptr);
  2218. }
  2219. }
  2220. if (*fptr) {
  2221. /* Trailing reset specifiers are valid. */
  2222. int done = 0;
  2223. while (*fptr && !done) {
  2224. switch (timelib_lookup_format(*fptr, format_map)) {
  2225. case TIMELIB_FORMAT_RESET_ALL: /* reset all fields to default */
  2226. timelib_time_reset_fields(s->time);
  2227. break;
  2228. case TIMELIB_FORMAT_RESET_ALL_WHEN_NOT_SET: /* reset all fields to default when not set */
  2229. timelib_time_reset_unset_fields(s->time);
  2230. break;
  2231. case TIMELIB_FORMAT_ALLOW_EXTRA_CHARACTERS:
  2232. break;
  2233. default:
  2234. add_pbf_error(s, TIMELIB_ERR_DATA_MISSING, "Not enough data available to satisfy format", string, ptr);
  2235. done = 1;
  2236. }
  2237. fptr++;
  2238. }
  2239. }
  2240. /* clean up a bit */
  2241. if (s->time->h != TIMELIB_UNSET || s->time->i != TIMELIB_UNSET || s->time->s != TIMELIB_UNSET || s->time->us != TIMELIB_UNSET) {
  2242. if (s->time->h == TIMELIB_UNSET ) {
  2243. s->time->h = 0;
  2244. }
  2245. if (s->time->i == TIMELIB_UNSET ) {
  2246. s->time->i = 0;
  2247. }
  2248. if (s->time->s == TIMELIB_UNSET ) {
  2249. s->time->s = 0;
  2250. }
  2251. if (s->time->us == TIMELIB_UNSET ) {
  2252. s->time->us = 0;
  2253. }
  2254. }
  2255. /* Check for mixing of ISO dates with natural dates. */
  2256. if (s->time->y != TIMELIB_UNSET && (iso_week_of_year != TIMELIB_UNSET || iso_year != TIMELIB_UNSET || iso_day_of_week != TIMELIB_UNSET)) {
  2257. add_pbf_error(s, TIMELIB_ERR_MIX_ISO_WITH_NATURAL, "Mixing of ISO dates with natural dates is not allowed", string, ptr);
  2258. }
  2259. if (iso_year != TIMELIB_UNSET && (s->time->y != TIMELIB_UNSET || s->time->m != TIMELIB_UNSET || s->time->d != TIMELIB_UNSET)) {
  2260. add_pbf_error(s, TIMELIB_ERR_MIX_ISO_WITH_NATURAL, "Mixing of ISO dates with natural dates is not allowed", string, ptr);
  2261. }
  2262. /* Convert ISO values */
  2263. if (iso_year != TIMELIB_UNSET) {
  2264. /* Default week of year and day of week to 1. */
  2265. if (iso_week_of_year == TIMELIB_UNSET) {
  2266. iso_week_of_year = 1;
  2267. }
  2268. if (iso_day_of_week == TIMELIB_UNSET) {
  2269. iso_day_of_week = 1;
  2270. }
  2271. timelib_date_from_isodate(iso_year, iso_week_of_year, iso_day_of_week, &s->time->y, &s->time->m, &s->time->d);
  2272. } else if (iso_week_of_year != TIMELIB_UNSET || iso_day_of_week != TIMELIB_UNSET) {
  2273. add_pbf_warning(s, TIMELIB_WARN_INVALID_DATE, "The parsed date was invalid", string, ptr);
  2274. }
  2275. /* do funky checking whether the parsed time was valid time */
  2276. if (s->time->h != TIMELIB_UNSET && s->time->i != TIMELIB_UNSET &&
  2277. s->time->s != TIMELIB_UNSET &&
  2278. !timelib_valid_time( s->time->h, s->time->i, s->time->s)) {
  2279. add_pbf_warning(s, TIMELIB_WARN_INVALID_TIME, "The parsed time was invalid", string, ptr);
  2280. }
  2281. /* do funky checking whether the parsed date was valid date */
  2282. if (s->time->y != TIMELIB_UNSET && s->time->m != TIMELIB_UNSET &&
  2283. s->time->d != TIMELIB_UNSET &&
  2284. !timelib_valid_date( s->time->y, s->time->m, s->time->d)) {
  2285. add_pbf_warning(s, TIMELIB_WARN_INVALID_DATE, "The parsed date was invalid", string, ptr);
  2286. }
  2287. if (errors) {
  2288. *errors = in.errors;
  2289. } else {
  2290. timelib_error_container_dtor(in.errors);
  2291. }
  2292. return in.time;
  2293. }
  2294. void timelib_fill_holes(timelib_time *parsed, timelib_time *now, int options)
  2295. {
  2296. if (!(options & TIMELIB_OVERRIDE_TIME) && parsed->have_date && !parsed->have_time) {
  2297. parsed->h = 0;
  2298. parsed->i = 0;
  2299. parsed->s = 0;
  2300. parsed->us = 0;
  2301. }
  2302. if (
  2303. parsed->y != TIMELIB_UNSET || parsed->m != TIMELIB_UNSET || parsed->d != TIMELIB_UNSET ||
  2304. parsed->h != TIMELIB_UNSET || parsed->i != TIMELIB_UNSET || parsed->s != TIMELIB_UNSET
  2305. ) {
  2306. if (parsed->us == TIMELIB_UNSET) parsed->us = 0;
  2307. } else {
  2308. if (parsed->us == TIMELIB_UNSET) parsed->us = now->us != TIMELIB_UNSET ? now->us : 0;
  2309. }
  2310. if (parsed->y == TIMELIB_UNSET) parsed->y = now->y != TIMELIB_UNSET ? now->y : 0;
  2311. if (parsed->m == TIMELIB_UNSET) parsed->m = now->m != TIMELIB_UNSET ? now->m : 0;
  2312. if (parsed->d == TIMELIB_UNSET) parsed->d = now->d != TIMELIB_UNSET ? now->d : 0;
  2313. if (parsed->h == TIMELIB_UNSET) parsed->h = now->h != TIMELIB_UNSET ? now->h : 0;
  2314. if (parsed->i == TIMELIB_UNSET) parsed->i = now->i != TIMELIB_UNSET ? now->i : 0;
  2315. if (parsed->s == TIMELIB_UNSET) parsed->s = now->s != TIMELIB_UNSET ? now->s : 0;
  2316. if (!parsed->tz_info) {
  2317. parsed->tz_info = now->tz_info ? (!(options & TIMELIB_NO_CLONE) ? timelib_tzinfo_clone(now->tz_info) : now->tz_info) : NULL;
  2318. if (parsed->z == TIMELIB_UNSET) parsed->z = now->z != TIMELIB_UNSET ? now->z : 0;
  2319. if (parsed->dst == TIMELIB_UNSET) parsed->dst = now->dst != TIMELIB_UNSET ? now->dst : 0;
  2320. if (!parsed->tz_abbr) {
  2321. parsed->tz_abbr = now->tz_abbr ? timelib_strdup(now->tz_abbr) : NULL;
  2322. }
  2323. }
  2324. if (parsed->zone_type == 0 && now->zone_type != 0) {
  2325. parsed->zone_type = now->zone_type;
  2326. /* parsed->tz_abbr = now->tz_abbr ? timelib_strdup(now->tz_abbr) : NULL;
  2327. parsed->tz_info = now->tz_info ? timelib_tzinfo_clone(now->tz_info) : NULL;
  2328. */ parsed->is_localtime = 1;
  2329. }
  2330. /* timelib_dump_date(parsed, 2);
  2331. timelib_dump_date(now, 2);
  2332. */
  2333. }
  2334. const char *timelib_timezone_id_from_abbr(const char *abbr, timelib_long gmtoffset, int isdst)
  2335. {
  2336. const timelib_tz_lookup_table *tp;
  2337. tp = abbr_search(abbr, gmtoffset, isdst);
  2338. if (tp) {
  2339. return (tp->full_tz_name);
  2340. } else {
  2341. return NULL;
  2342. }
  2343. }
  2344. const timelib_tz_lookup_table *timelib_timezone_abbreviations_list(void)
  2345. {
  2346. return timelib_timezone_lookup;
  2347. }
  2348. #ifdef DEBUG_PARSER_STUB
  2349. int main(void)
  2350. {
  2351. timelib_time time = timelib_strtotime("May 12");
  2352. printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
  2353. time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
  2354. if (time.have_relative) {
  2355. printf ("%3dY %3dM %3dD / %3dH %3dM %3dS",
  2356. time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
  2357. }
  2358. if (time.have_weekday_relative) {
  2359. printf (" / %d", time.relative.weekday);
  2360. }
  2361. if (time.have_weeknr_day) {
  2362. printf(" / %dW%d", time.relative.weeknr_day.weeknr, time.relative.weeknr_day.dayofweek);
  2363. }
  2364. return 0;
  2365. }
  2366. #endif
  2367. /*
  2368. * vim: syntax=c
  2369. */