parser.rl6 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cctype>
  5. #include <ctime>
  6. #include <numeric>
  7. #include <util/datetime/parser.h>
  8. #include <util/generic/ymath.h>
  9. %%{
  10. machine DateTimeParserCommon;
  11. sp = ' ';
  12. action clear_int {
  13. I = 0;
  14. Dc = 0;
  15. }
  16. action update_int {
  17. I = I * 10 + (fc - '0');
  18. ++Dc;
  19. }
  20. int = (digit+)
  21. >clear_int
  22. $update_int;
  23. int1 = digit
  24. >clear_int
  25. $update_int;
  26. int2 = (digit digit)
  27. >clear_int
  28. $update_int;
  29. int3 = (digit digit digit)
  30. >clear_int
  31. $update_int;
  32. int4 = (digit digit digit digit)
  33. >clear_int
  34. $update_int;
  35. int12 = (digit digit?)
  36. >clear_int
  37. $update_int;
  38. int24 = ( digit digit ( digit digit )? )
  39. >clear_int
  40. $update_int;
  41. # According to both RFC2822 and RFC2616 dates MUST be case-sensitive,
  42. # but Andrey fomichev@ wants relaxed parser
  43. month3 =
  44. 'Jan'i %{ DateTimeFields.Month = 1; }
  45. | 'Feb'i %{ DateTimeFields.Month = 2; }
  46. | 'Mar'i %{ DateTimeFields.Month = 3; }
  47. | 'Apr'i %{ DateTimeFields.Month = 4; }
  48. | 'May'i %{ DateTimeFields.Month = 5; }
  49. | 'Jun'i %{ DateTimeFields.Month = 6; }
  50. | 'Jul'i %{ DateTimeFields.Month = 7; }
  51. | 'Aug'i %{ DateTimeFields.Month = 8; }
  52. | 'Sep'i %{ DateTimeFields.Month = 9; }
  53. | 'Oct'i %{ DateTimeFields.Month = 10; }
  54. | 'Nov'i %{ DateTimeFields.Month = 11; }
  55. | 'Dec'i %{ DateTimeFields.Month = 12; };
  56. wkday = 'Mon'i | 'Tue'i | 'Wed'i | 'Thu'i | 'Fri'i | 'Sat'i | 'Sun'i;
  57. weekday = 'Monday'i | 'Tuesday'i | 'Wednesday'i | 'Thursday'i
  58. | 'Friday'i | 'Saturday'i | 'Sunday'i;
  59. action set_second { DateTimeFields.Second = I; }
  60. action set_minute { DateTimeFields.Minute = I; }
  61. action set_hour { DateTimeFields.Hour = I; }
  62. action set_day { DateTimeFields.Day = I; }
  63. action set_month { DateTimeFields.Month = I; }
  64. action set_year { DateTimeFields.SetLooseYear(I); }
  65. action set_zone_utc { DateTimeFields.ZoneOffsetMinutes = 0; }
  66. }%%
  67. %%{
  68. machine RFC822DateParser;
  69. ################# RFC 2822 3.3 Full Date ###################
  70. include DateTimeParserCommon;
  71. ws1 = (space+);
  72. ws0 = (space*);
  73. dow_spec = ( wkday ',' )?;
  74. day = int12 %set_day;
  75. year = int24 %set_year;
  76. # actually it must be from 0 to 23
  77. hour = int2 %set_hour;
  78. # actually it must be from 0 to 59
  79. min = int2 %set_minute;
  80. # actually it must be from 0 to 59
  81. sec = int2 %set_second;
  82. sec_spec = ( ':' . sec )?;
  83. # so called "military zone offset". I hardly believe someone uses it now, but we MUST respect RFc822
  84. action set_mil_offset {
  85. char c = (char)toupper(fc);
  86. if (c == 'Z')
  87. DateTimeFields.ZoneOffsetMinutes = 0;
  88. else {
  89. if (c <= 'M') {
  90. // ['A'..'M'] \ 'J'
  91. if (c < 'J')
  92. DateTimeFields.ZoneOffsetMinutes = (i32)TDuration::Hours(c - 'A' + 1).Minutes();
  93. else
  94. DateTimeFields.ZoneOffsetMinutes = (i32)TDuration::Hours(c - 'A').Minutes();
  95. } else {
  96. // ['N'..'Y']
  97. DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(c - 'N' + 1).Minutes();
  98. }
  99. }
  100. }
  101. action set_digit_offset {
  102. DateTimeFields.ZoneOffsetMinutes = Sign * (i32)(TDuration::Hours(I / 100) + TDuration::Minutes(I % 100)).Minutes();
  103. }
  104. mil_zone = /[A-IK-Za-ik-z]/ $set_mil_offset;
  105. # actions % were replaced with @ (when the script was migrated to ragel 5.24)
  106. # because ragel 5.24 does not call to the % action if it be called at the very end of string.
  107. # it is a bug in ragel 5 because ragel 6.2 works correctly with % at the end of string.
  108. # see http://www.complang.org/ragel/ChangeLog.
  109. zone = 'UT' @{ DateTimeFields.ZoneOffsetMinutes = 0; }
  110. | 'GMT' @{ DateTimeFields.ZoneOffsetMinutes = 0; }
  111. | 'EST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(5).Minutes();}
  112. | 'EDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(4).Minutes(); }
  113. | 'CST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(6).Minutes();}
  114. | 'CDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(5).Minutes(); }
  115. | 'MST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(7).Minutes();}
  116. | 'MDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(6).Minutes(); }
  117. | 'PST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(8).Minutes();}
  118. | 'PDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(7).Minutes(); };
  119. digit_offset = ('+' | '-') > { Sign = fc == '+' ? 1 : -1; } . int4 @set_digit_offset;
  120. offset = ( zone | mil_zone | digit_offset );
  121. rfc822datetime = ws0 . dow_spec . ws0 . day . ws1 . month3 . ws1 . year . ws1 . hour . ':' . min . sec_spec . ws1 . offset . ws0;
  122. main := rfc822datetime;
  123. write data noerror;
  124. }%%
  125. TRfc822DateTimeParserDeprecated::TRfc822DateTimeParserDeprecated() {
  126. %% write init;
  127. }
  128. bool TRfc822DateTimeParserDeprecated::ParsePart(const char* input, size_t len) {
  129. const char* p = input;
  130. const char* pe = input + len;
  131. %% write exec;
  132. return cs != %%{ write error; }%%;
  133. }
  134. TRfc822DateTimeParser::TRfc822DateTimeParser() {
  135. %% write init;
  136. }
  137. bool TRfc822DateTimeParser::ParsePart(const char* input, size_t len) {
  138. const char* p = input;
  139. const char* pe = input + len;
  140. %% write exec;
  141. return cs != %%{ write error; }%%;
  142. }
  143. %%{
  144. machine ISO8601DateTimeParser;
  145. include DateTimeParserCommon;
  146. year = int4 @set_year;
  147. month = int2 @set_month;
  148. day = int2 @set_day;
  149. hour = int2 @set_hour;
  150. minute = int2 @set_minute;
  151. second = int2 @set_second;
  152. secondFrac = digit {1,6} >clear_int $update_int @{
  153. ui32 us = I;
  154. for (int k = Dc; k < 6; ++k) {
  155. us *= 10;
  156. }
  157. DateTimeFields.MicroSecond = us;
  158. };
  159. secondFracTail = (digit*);
  160. zoneZ = [Zz] @set_zone_utc;
  161. zoneOffset = space? . ('+' | '-') >{ Sign = fc == '+' ? 1 : -1; } . int2 @{ DateTimeFields.ZoneOffsetMinutes = Sign * (i32)TDuration::Hours(I).Minutes(); } . (':')? . (int2 @{ DateTimeFields.ZoneOffsetMinutes += I * Sign; })?;
  162. zone = zoneZ | zoneOffset;
  163. iso8601date = (year . '-' . month . '-' . day) | (year . month . day);
  164. iso8601time = (hour . ':' . minute . (':' . second ('.' secondFrac secondFracTail)?)?) | (hour . minute . second?);
  165. iso8601datetime = iso8601date . ([Tt ] . iso8601time . zone?)?;
  166. main := iso8601datetime;
  167. write data noerror;
  168. }%%
  169. TIso8601DateTimeParserDeprecated::TIso8601DateTimeParserDeprecated() {
  170. %% write init;
  171. }
  172. bool TIso8601DateTimeParserDeprecated::ParsePart(const char* input, size_t len) {
  173. const char* p = input;
  174. const char* pe = input + len;
  175. %% write exec;
  176. return cs != %%{ write error; }%%;
  177. }
  178. TIso8601DateTimeParser::TIso8601DateTimeParser() {
  179. %% write init;
  180. }
  181. bool TIso8601DateTimeParser::ParsePart(const char* input, size_t len) {
  182. const char* p = input;
  183. const char* pe = input + len;
  184. %% write exec;
  185. return cs != %%{ write error; }%%;
  186. }
  187. %%{
  188. machine HttpDateTimeParser;
  189. include DateTimeParserCommon;
  190. ################# RFC 2616 3.3.1 Full Date #################
  191. time = int2 %set_hour ':' int2 %set_minute ':' int2 %set_second;
  192. date1 = int2 %set_day ' ' month3 ' ' int4 %set_year;
  193. date2 = int2 %set_day '-' month3 '-' int2 %set_year;
  194. date3 = month3 sp (int2 | sp int1) %set_day;
  195. rfc1123_date = wkday ',' sp date1 sp time sp 'GMT'i;
  196. rfc850_date = weekday ',' sp date2 sp time sp 'GMT'i;
  197. asctime_date = wkday sp date3 sp time sp int4 @set_year;
  198. http_date = (rfc1123_date | rfc850_date | asctime_date) @set_zone_utc;
  199. }%%
  200. %%{
  201. machine HttpDateTimeParserStandalone;
  202. include HttpDateTimeParser;
  203. main := http_date;
  204. write data noerror;
  205. }%%
  206. THttpDateTimeParserDeprecated::THttpDateTimeParserDeprecated() {
  207. %% write init;
  208. }
  209. bool THttpDateTimeParserDeprecated::ParsePart(const char* input, size_t len) {
  210. const char* p = input;
  211. const char* pe = input + len;
  212. %% write exec;
  213. return cs != %%{ write error; }%%;
  214. }
  215. THttpDateTimeParser::THttpDateTimeParser() {
  216. %% write init;
  217. }
  218. bool THttpDateTimeParser::ParsePart(const char* input, size_t len) {
  219. const char* p = input;
  220. const char* pe = input + len;
  221. %% write exec;
  222. return cs != %%{ write error; }%%;
  223. }
  224. %%{
  225. machine X509ValidityDateTimeParser;
  226. include DateTimeParserCommon;
  227. ################# X.509 certificate validity time (see rfc5280 4.1.2.5.*) #################
  228. year = int2 @{ DateTimeFields.Year = (I < 50 ? I + 2000 : I + 1900); };
  229. month = int2 @set_month;
  230. day = int2 @set_day;
  231. hour = int2 @set_hour;
  232. minute = int2 @set_minute;
  233. second = int2 @set_second;
  234. zone = 'Z' @set_zone_utc;
  235. main := year . month . day . hour . minute . second . zone;
  236. write data noerror;
  237. }%%
  238. TX509ValidityDateTimeParserDeprecated::TX509ValidityDateTimeParserDeprecated() {
  239. %% write init;
  240. }
  241. bool TX509ValidityDateTimeParserDeprecated::ParsePart(const char *input, size_t len) {
  242. const char *p = input;
  243. const char *pe = input + len;
  244. %% write exec;
  245. return cs != %%{ write error; }%%;
  246. }
  247. TX509ValidityDateTimeParser::TX509ValidityDateTimeParser() {
  248. %% write init;
  249. }
  250. bool TX509ValidityDateTimeParser::ParsePart(const char *input, size_t len) {
  251. const char *p = input;
  252. const char *pe = input + len;
  253. %% write exec;
  254. return cs != %%{ write error; }%%;
  255. }
  256. %%{
  257. machine X509Validity4yDateTimeParser;
  258. include DateTimeParserCommon;
  259. year = int4 @{ DateTimeFields.Year = I; };
  260. month = int2 @set_month;
  261. day = int2 @set_day;
  262. hour = int2 @set_hour;
  263. minute = int2 @set_minute;
  264. second = int2 @set_second;
  265. zone = 'Z' @set_zone_utc;
  266. main := year . month . day . hour . minute . second . zone;
  267. write data noerror;
  268. }%%
  269. TX509Validity4yDateTimeParserDeprecated::TX509Validity4yDateTimeParserDeprecated() {
  270. %% write init;
  271. }
  272. bool TX509Validity4yDateTimeParserDeprecated::ParsePart(const char *input, size_t len) {
  273. const char *p = input;
  274. const char *pe = input + len;
  275. %% write exec;
  276. return cs != %%{ write error; }%%;
  277. }
  278. TX509Validity4yDateTimeParser::TX509Validity4yDateTimeParser() {
  279. %% write init;
  280. }
  281. bool TX509Validity4yDateTimeParser::ParsePart(const char *input, size_t len) {
  282. const char *p = input;
  283. const char *pe = input + len;
  284. %% write exec;
  285. return cs != %%{ write error; }%%;
  286. }
  287. TInstant TIso8601DateTimeParserDeprecated::GetResult(TInstant defaultValue) const {
  288. Y_UNUSED(ISO8601DateTimeParser_en_main);
  289. return TDateTimeParserBaseDeprecated::GetResult(ISO8601DateTimeParser_first_final, defaultValue);
  290. }
  291. TInstant TRfc822DateTimeParserDeprecated::GetResult(TInstant defaultValue) const {
  292. Y_UNUSED(RFC822DateParser_en_main);
  293. return TDateTimeParserBaseDeprecated::GetResult(RFC822DateParser_first_final, defaultValue);
  294. }
  295. TInstant THttpDateTimeParserDeprecated::GetResult(TInstant defaultValue) const {
  296. Y_UNUSED(HttpDateTimeParserStandalone_en_main);
  297. return TDateTimeParserBaseDeprecated::GetResult(HttpDateTimeParserStandalone_first_final, defaultValue);
  298. }
  299. TInstant TX509ValidityDateTimeParserDeprecated::GetResult(TInstant defaultValue) const {
  300. Y_UNUSED(X509ValidityDateTimeParser_en_main);
  301. return TDateTimeParserBaseDeprecated::GetResult(X509ValidityDateTimeParser_first_final, defaultValue);
  302. }
  303. TInstant TX509Validity4yDateTimeParserDeprecated::GetResult(TInstant defaultValue) const {
  304. Y_UNUSED(X509Validity4yDateTimeParser_en_main);
  305. return TDateTimeParserBaseDeprecated::GetResult(X509Validity4yDateTimeParser_first_final, defaultValue);
  306. }
  307. TInstant TIso8601DateTimeParser::GetResult(TInstant defaultValue) const {
  308. Y_UNUSED(ISO8601DateTimeParser_en_main);
  309. return TDateTimeParserBase::GetResult(ISO8601DateTimeParser_first_final, defaultValue);
  310. }
  311. TInstant TRfc822DateTimeParser::GetResult(TInstant defaultValue) const {
  312. Y_UNUSED(RFC822DateParser_en_main);
  313. return TDateTimeParserBase::GetResult(RFC822DateParser_first_final, defaultValue);
  314. }
  315. TInstant THttpDateTimeParser::GetResult(TInstant defaultValue) const {
  316. Y_UNUSED(HttpDateTimeParserStandalone_en_main);
  317. return TDateTimeParserBase::GetResult(HttpDateTimeParserStandalone_first_final, defaultValue);
  318. }
  319. TInstant TX509ValidityDateTimeParser::GetResult(TInstant defaultValue) const {
  320. Y_UNUSED(X509ValidityDateTimeParser_en_main);
  321. return TDateTimeParserBase::GetResult(X509ValidityDateTimeParser_first_final, defaultValue);
  322. }
  323. TInstant TX509Validity4yDateTimeParser::GetResult(TInstant defaultValue) const {
  324. Y_UNUSED(X509Validity4yDateTimeParser_en_main);
  325. return TDateTimeParserBase::GetResult(X509Validity4yDateTimeParser_first_final, defaultValue);
  326. }
  327. template<class TParser, class TResult>
  328. static inline TResult Parse(const char* input, size_t len, TResult defaultValue) {
  329. TParser parser;
  330. if (!parser.ParsePart(input, len))
  331. return defaultValue;
  332. return parser.GetResult(defaultValue);
  333. }
  334. template<class TParser, class TResult, bool ThrowExceptionOnFailure = true>
  335. static inline TResult ParseUnsafe(const char* input, size_t len) {
  336. TResult r = Parse<TParser, TResult>(input, len, TResult::Max());
  337. if (ThrowExceptionOnFailure && r == TResult::Max())
  338. ythrow TDateTimeParseException() << "error in datetime parsing. Input data: " << TStringBuf(input, len);
  339. return r;
  340. }
  341. TInstant TInstant::ParseIso8601Deprecated(const TStringBuf input) {
  342. return ParseUnsafe<TIso8601DateTimeParserDeprecated, TInstant>(input.data(), input.size());
  343. }
  344. TInstant TInstant::ParseRfc822Deprecated(const TStringBuf input) {
  345. return ParseUnsafe<TRfc822DateTimeParserDeprecated, TInstant>(input.data(), input.size());
  346. }
  347. TInstant TInstant::ParseHttpDeprecated(const TStringBuf input) {
  348. return ParseUnsafe<THttpDateTimeParserDeprecated, TInstant>(input.data(), input.size());
  349. }
  350. TInstant TInstant::ParseX509ValidityDeprecated(const TStringBuf input) {
  351. switch (input.size()) {
  352. case 13:
  353. return ParseUnsafe<TX509ValidityDateTimeParserDeprecated, TInstant>(input.data(), 13);
  354. case 15:
  355. return ParseUnsafe<TX509Validity4yDateTimeParserDeprecated, TInstant>(input.data(), 15);
  356. default:
  357. ythrow TDateTimeParseException();
  358. }
  359. }
  360. bool TInstant::TryParseIso8601Deprecated(const TStringBuf input, TInstant& instant) {
  361. const auto parsed = ParseUnsafe<TIso8601DateTimeParserDeprecated, TInstant, false>(input.data(), input.size());
  362. if (TInstant::Max() == parsed) {
  363. return false;
  364. }
  365. instant = parsed;
  366. return true;
  367. }
  368. bool TInstant::TryParseRfc822Deprecated(const TStringBuf input, TInstant& instant) {
  369. const auto parsed = ParseUnsafe<TRfc822DateTimeParserDeprecated, TInstant, false>(input.data(), input.size());
  370. if (TInstant::Max() == parsed) {
  371. return false;
  372. }
  373. instant = parsed;
  374. return true;
  375. }
  376. bool TInstant::TryParseHttpDeprecated(const TStringBuf input, TInstant& instant) {
  377. const auto parsed = ParseUnsafe<THttpDateTimeParserDeprecated, TInstant, false>(input.data(), input.size());
  378. if (TInstant::Max() == parsed) {
  379. return false;
  380. }
  381. instant = parsed;
  382. return true;
  383. }
  384. bool TInstant::TryParseX509Deprecated(const TStringBuf input, TInstant& instant) {
  385. TInstant parsed;
  386. switch (input.size()) {
  387. case 13:
  388. parsed = ParseUnsafe<TX509ValidityDateTimeParserDeprecated, TInstant, false>(input.data(), 13);
  389. break;
  390. case 15:
  391. parsed = ParseUnsafe<TX509Validity4yDateTimeParserDeprecated, TInstant, false>(input.data(), 15);
  392. break;
  393. default:
  394. return false;
  395. }
  396. if (TInstant::Max() == parsed) {
  397. return false;
  398. }
  399. instant = parsed;
  400. return true;
  401. }
  402. TInstant TInstant::ParseIso8601(const TStringBuf input) {
  403. return ParseUnsafe<TIso8601DateTimeParser, TInstant>(input.data(), input.size());
  404. }
  405. TInstant TInstant::ParseRfc822(const TStringBuf input) {
  406. return ParseUnsafe<TRfc822DateTimeParser, TInstant>(input.data(), input.size());
  407. }
  408. TInstant TInstant::ParseHttp(const TStringBuf input) {
  409. return ParseUnsafe<THttpDateTimeParser, TInstant>(input.data(), input.size());
  410. }
  411. TInstant TInstant::ParseX509Validity(const TStringBuf input) {
  412. switch (input.size()) {
  413. case 13:
  414. return ParseUnsafe<TX509ValidityDateTimeParser, TInstant>(input.data(), 13);
  415. case 15:
  416. return ParseUnsafe<TX509Validity4yDateTimeParser, TInstant>(input.data(), 15);
  417. default:
  418. ythrow TDateTimeParseException();
  419. }
  420. }
  421. bool TInstant::TryParseIso8601(const TStringBuf input, TInstant& instant) {
  422. const auto parsed = ParseUnsafe<TIso8601DateTimeParser, TInstant, false>(input.data(), input.size());
  423. if (TInstant::Max() == parsed) {
  424. return false;
  425. }
  426. instant = parsed;
  427. return true;
  428. }
  429. bool TInstant::TryParseRfc822(const TStringBuf input, TInstant& instant) {
  430. const auto parsed = ParseUnsafe<TRfc822DateTimeParser, TInstant, false>(input.data(), input.size());
  431. if (TInstant::Max() == parsed) {
  432. return false;
  433. }
  434. instant = parsed;
  435. return true;
  436. }
  437. bool TInstant::TryParseHttp(const TStringBuf input, TInstant& instant) {
  438. const auto parsed = ParseUnsafe<THttpDateTimeParser, TInstant, false>(input.data(), input.size());
  439. if (TInstant::Max() == parsed) {
  440. return false;
  441. }
  442. instant = parsed;
  443. return true;
  444. }
  445. bool TInstant::TryParseX509(const TStringBuf input, TInstant& instant) {
  446. TInstant parsed;
  447. switch (input.size()) {
  448. case 13:
  449. parsed = ParseUnsafe<TX509ValidityDateTimeParser, TInstant, false>(input.data(), 13);
  450. break;
  451. case 15:
  452. parsed = ParseUnsafe<TX509Validity4yDateTimeParser, TInstant, false>(input.data(), 15);
  453. break;
  454. default:
  455. return false;
  456. }
  457. if (TInstant::Max() == parsed) {
  458. return false;
  459. }
  460. instant = parsed;
  461. return true;
  462. }
  463. bool ParseRFC822DateTimeDeprecated(const char* input, time_t& utcTime) {
  464. return ParseRFC822DateTimeDeprecated(input, strlen(input), utcTime);
  465. }
  466. bool ParseISO8601DateTimeDeprecated(const char* input, time_t& utcTime) {
  467. return ParseISO8601DateTimeDeprecated(input, strlen(input), utcTime);
  468. }
  469. bool ParseHTTPDateTimeDeprecated(const char* input, time_t& utcTime) {
  470. return ParseHTTPDateTimeDeprecated(input, strlen(input), utcTime);
  471. }
  472. bool ParseX509ValidityDateTimeDeprecated(const char* input, time_t& utcTime) {
  473. return ParseX509ValidityDateTimeDeprecated(input, strlen(input), utcTime);
  474. }
  475. bool ParseRFC822DateTimeDeprecated(const char* input, size_t inputLen, time_t& utcTime) {
  476. try {
  477. utcTime = ParseUnsafe<TRfc822DateTimeParserDeprecated, TInstant>(input, inputLen).TimeT();
  478. return true;
  479. } catch (const TDateTimeParseException&) {
  480. return false;
  481. }
  482. }
  483. bool ParseISO8601DateTimeDeprecated(const char* input, size_t inputLen, time_t& utcTime) {
  484. try {
  485. utcTime = ParseUnsafe<TIso8601DateTimeParserDeprecated, TInstant>(input, inputLen).TimeT();
  486. return true;
  487. } catch (const TDateTimeParseException&) {
  488. return false;
  489. }
  490. }
  491. bool ParseHTTPDateTimeDeprecated(const char* input, size_t inputLen, time_t& utcTime) {
  492. try {
  493. utcTime = ParseUnsafe<THttpDateTimeParserDeprecated, TInstant>(input, inputLen).TimeT();
  494. return true;
  495. } catch (const TDateTimeParseException&) {
  496. return false;
  497. }
  498. }
  499. bool ParseX509ValidityDateTimeDeprecated(const char* input, size_t inputLen, time_t& utcTime) {
  500. TInstant r;
  501. switch (inputLen) {
  502. case 13:
  503. r = Parse<TX509ValidityDateTimeParserDeprecated, TInstant>(input, 13, TInstant::Max());
  504. break;
  505. case 15:
  506. r = Parse<TX509Validity4yDateTimeParserDeprecated, TInstant>(input, 15, TInstant::Max());
  507. break;
  508. default:
  509. return false;
  510. }
  511. if (r == TInstant::Max())
  512. return false;
  513. utcTime = r.TimeT();
  514. return true;
  515. }
  516. bool ParseRFC822DateTime(const char* input, time_t& utcTime) {
  517. return ParseRFC822DateTime(input, strlen(input), utcTime);
  518. }
  519. bool ParseISO8601DateTime(const char* input, time_t& utcTime) {
  520. return ParseISO8601DateTime(input, strlen(input), utcTime);
  521. }
  522. bool ParseHTTPDateTime(const char* input, time_t& utcTime) {
  523. return ParseHTTPDateTime(input, strlen(input), utcTime);
  524. }
  525. bool ParseX509ValidityDateTime(const char* input, time_t& utcTime) {
  526. return ParseX509ValidityDateTime(input, strlen(input), utcTime);
  527. }
  528. bool ParseRFC822DateTime(const char* input, size_t inputLen, time_t& utcTime) {
  529. try {
  530. utcTime = ParseUnsafe<TRfc822DateTimeParser, TInstant>(input, inputLen).TimeT();
  531. return true;
  532. } catch (const TDateTimeParseException&) {
  533. return false;
  534. }
  535. }
  536. bool ParseISO8601DateTime(const char* input, size_t inputLen, time_t& utcTime) {
  537. try {
  538. utcTime = ParseUnsafe<TIso8601DateTimeParser, TInstant>(input, inputLen).TimeT();
  539. return true;
  540. } catch (const TDateTimeParseException&) {
  541. return false;
  542. }
  543. }
  544. bool ParseHTTPDateTime(const char* input, size_t inputLen, time_t& utcTime) {
  545. try {
  546. utcTime = ParseUnsafe<THttpDateTimeParser, TInstant>(input, inputLen).TimeT();
  547. return true;
  548. } catch (const TDateTimeParseException&) {
  549. return false;
  550. }
  551. }
  552. bool ParseX509ValidityDateTime(const char* input, size_t inputLen, time_t& utcTime) {
  553. TInstant r;
  554. switch (inputLen) {
  555. case 13:
  556. r = Parse<TX509ValidityDateTimeParser, TInstant>(input, 13, TInstant::Max());
  557. break;
  558. case 15:
  559. r = Parse<TX509Validity4yDateTimeParser, TInstant>(input, 15, TInstant::Max());
  560. break;
  561. default:
  562. return false;
  563. }
  564. if (r == TInstant::Max())
  565. return false;
  566. utcTime = r.TimeT();
  567. return true;
  568. }
  569. %%{
  570. machine TDurationParser;
  571. include DateTimeParserCommon;
  572. multiplier
  573. = '' # >{ MultiplierPower = 6; } # work around Ragel bugs
  574. | 'w' @{ MultiplierPower = 6; Multiplier = 604800; }
  575. | 'd' @{ MultiplierPower = 6; Multiplier = 86400; }
  576. | 'h' @{ MultiplierPower = 6; Multiplier = 3600; }
  577. | 'm' @{ MultiplierPower = 6; Multiplier = 60; }
  578. | 's' @{ MultiplierPower = 6; Multiplier = 1; }
  579. | 'ms' @{ MultiplierPower = 3; Multiplier = 1; }
  580. | 'us' @{ MultiplierPower = 0; Multiplier = 1; }
  581. | 'ns' @{ MultiplierPower = -3; Multiplier = 1; }
  582. ;
  583. integer = int @{ IntegerPart = I; };
  584. fraction = '.' digit {1,6} >clear_int $update_int @{ FractionPart = I; FractionDigits = Dc; } digit*;
  585. duration = integer fraction? multiplier;
  586. main := duration;
  587. write data noerror;
  588. }%%
  589. TDurationParser::TDurationParser()
  590. : cs(0)
  591. , I(0)
  592. , Dc(0)
  593. , MultiplierPower(6)
  594. , Multiplier(1)
  595. , IntegerPart(0)
  596. , FractionPart(0)
  597. , FractionDigits(0)
  598. {
  599. Y_UNUSED(TDurationParser_en_main);
  600. %% write init;
  601. }
  602. bool TDurationParser::ParsePart(const char* input, size_t len) {
  603. const char* p = input;
  604. const char* pe = input + len;
  605. %% write exec;
  606. return cs != %%{ write error; }%%;
  607. }
  608. static inline ui64 DecPower(ui64 part, i32 power) {
  609. if (power >= 0)
  610. return part * Power(10, power);
  611. return part / Power(10, -power);
  612. }
  613. TDuration TDurationParser::GetResult(TDuration defaultValue) const {
  614. if (cs < TDurationParser_first_final)
  615. return defaultValue;
  616. ui64 us = 0;
  617. us += Multiplier * DecPower(IntegerPart, MultiplierPower);
  618. us += Multiplier * DecPower(FractionPart, MultiplierPower - FractionDigits);
  619. return TDuration::MicroSeconds(us);
  620. }
  621. bool TDuration::TryParse(const TStringBuf input, TDuration& result) {
  622. TDuration r = ::Parse<TDurationParser, TDuration>(input.data(), input.size(), TDuration::Max());
  623. if (r == TDuration::Max())
  624. return false;
  625. result = r;
  626. return true;
  627. }
  628. TDuration TDuration::Parse(const TStringBuf input) {
  629. return ParseUnsafe<TDurationParser, TDuration>(input.data(), input.size());
  630. }