parser_ut.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. #include "parser.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. static const time_t SECONDS_PER_HOUR = 3600;
  4. static const time_t SECONDS_PER_MINUTE = 60;
  5. Y_UNIT_TEST_SUITE(TDateTimeParseTest) {
  6. Y_UNIT_TEST(TestRfc822Correct) {
  7. bool r = false;
  8. time_t t = 0;
  9. // predefined time zones
  10. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 UT", t);
  11. UNIT_ASSERT(r);
  12. UNIT_ASSERT_EQUAL(t, (time_t)1109964885);
  13. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 GMT", t);
  14. UNIT_ASSERT(r);
  15. UNIT_ASSERT_EQUAL(t, (time_t)1109964885);
  16. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 EST", t);
  17. UNIT_ASSERT(r);
  18. UNIT_ASSERT_VALUES_EQUAL(t, (time_t)1109964885 + 5 * SECONDS_PER_HOUR);
  19. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 EDT", t);
  20. UNIT_ASSERT(r);
  21. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 4 * SECONDS_PER_HOUR);
  22. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 CST", t);
  23. UNIT_ASSERT(r);
  24. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 6 * SECONDS_PER_HOUR);
  25. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 CDT", t);
  26. UNIT_ASSERT(r);
  27. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 5 * SECONDS_PER_HOUR);
  28. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 MST", t);
  29. UNIT_ASSERT(r);
  30. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 7 * SECONDS_PER_HOUR);
  31. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 MDT", t);
  32. UNIT_ASSERT(r);
  33. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 6 * SECONDS_PER_HOUR);
  34. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 PST", t);
  35. UNIT_ASSERT(r);
  36. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 8 * SECONDS_PER_HOUR);
  37. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 PDT", t);
  38. UNIT_ASSERT(r);
  39. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 7 * SECONDS_PER_HOUR);
  40. // optinal century
  41. r = ParseRFC822DateTime("Fri, 4 Mar 05 19:34:45 UT", t);
  42. UNIT_ASSERT(r);
  43. UNIT_ASSERT_EQUAL(t, (time_t)1109964885);
  44. // + optional day of week
  45. r = ParseRFC822DateTime("4 Mar 05 19:34:45 UT", t);
  46. UNIT_ASSERT(r);
  47. UNIT_ASSERT_EQUAL(t, (time_t)1109964885);
  48. // + optional seconds
  49. r = ParseRFC822DateTime("4 Mar 05 19:34 UT", t);
  50. UNIT_ASSERT(r);
  51. UNIT_ASSERT_EQUAL(t, (time_t)1109964840);
  52. // local differential hour+min
  53. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 +0300", t);
  54. UNIT_ASSERT(r);
  55. UNIT_ASSERT_EQUAL(t, (time_t)1109954085);
  56. r = ParseRFC822DateTime("Fri, 4 Mar 05 19:34:45 +0300", t);
  57. UNIT_ASSERT(r);
  58. UNIT_ASSERT_EQUAL(t, (time_t)1109954085);
  59. r = ParseRFC822DateTime("21 Apr 1999 23:40:00 +0400", t);
  60. UNIT_ASSERT(r);
  61. UNIT_ASSERT_EQUAL(t, (time_t)924723600);
  62. r = ParseRFC822DateTime("21 Apr 99 23:40 +0400", t);
  63. UNIT_ASSERT(r);
  64. UNIT_ASSERT_EQUAL(t, (time_t)924723600);
  65. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 -0300", t);
  66. UNIT_ASSERT(r);
  67. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 3 * SECONDS_PER_HOUR);
  68. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34 -0300", t);
  69. UNIT_ASSERT(r);
  70. UNIT_ASSERT_EQUAL(t, (time_t)1109964840 + 3 * SECONDS_PER_HOUR);
  71. r = ParseRFC822DateTime("Fri, 4 Mar 05 19:34:45 -0330", t);
  72. UNIT_ASSERT(r);
  73. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 3 * SECONDS_PER_HOUR + 30 * SECONDS_PER_MINUTE);
  74. r = ParseRFC822DateTime("Fri, 4 Mar 05 19:34 -0330", t);
  75. UNIT_ASSERT(r);
  76. UNIT_ASSERT_EQUAL(t, (time_t)1109964840 + 3 * SECONDS_PER_HOUR + 30 * SECONDS_PER_MINUTE);
  77. r = ParseRFC822DateTime("4 Mar 2005 19:34:45 -1030", t);
  78. UNIT_ASSERT(r);
  79. UNIT_ASSERT_EQUAL(t, (time_t)1109964885 + 10 * SECONDS_PER_HOUR + 30 * SECONDS_PER_MINUTE);
  80. r = ParseRFC822DateTime("4 Mar 2005 19:34 -1030", t);
  81. UNIT_ASSERT(r);
  82. UNIT_ASSERT_EQUAL(t, (time_t)1109964840 + 10 * SECONDS_PER_HOUR + 30 * SECONDS_PER_MINUTE);
  83. // white spaces
  84. r = ParseRFC822DateTime("Fri,4 Mar 2005 19:34:45 UT", t); // no space after comma
  85. UNIT_ASSERT(r);
  86. UNIT_ASSERT_EQUAL(t, (time_t)1109964885);
  87. r = ParseRFC822DateTime(" Fri, 4 Mar 2005 19:34:45 UT ", t); // several spaces, leading and trailing
  88. UNIT_ASSERT(r);
  89. UNIT_ASSERT_EQUAL(t, (time_t)1109964885);
  90. r = ParseRFC822DateTime(" \t Fri, \t 4 \t Mar \t 2005 \t 19:34:45 \t UT \t ", t); // spaces with tabs
  91. UNIT_ASSERT(r);
  92. UNIT_ASSERT_EQUAL(t, (time_t)1109964885);
  93. r = ParseRFC822DateTime("Thu, 01 Jan 1970 03:00:00 +0300", t); // spaces with tabs
  94. UNIT_ASSERT(r);
  95. UNIT_ASSERT_EQUAL(t, (time_t)0);
  96. r = ParseRFC822DateTime("Sat, 14 Feb 2009 02:31:30 +0300", t); // spaces with tabs
  97. UNIT_ASSERT(r);
  98. UNIT_ASSERT_EQUAL(t, (time_t)1234567890);
  99. }
  100. time_t GetOffset(char militaryZone) {
  101. char ch = (char)toupper(militaryZone);
  102. if (ch == 'Z') {
  103. return 0;
  104. } else if (ch >= 'A' && ch < 'J') {
  105. return (ch - 'A' + 1) * SECONDS_PER_HOUR;
  106. } else if (ch > 'J' && ch <= 'M') {
  107. return (ch - 'A') * SECONDS_PER_HOUR;
  108. } else if (ch >= 'N' && ch <= 'Y') {
  109. return -(ch - 'N' + 1) * SECONDS_PER_HOUR;
  110. } else {
  111. ythrow yexception() << "Invalid military zone.";
  112. }
  113. }
  114. void DoTestMilitaryZones(char firstChar, char lastChar) {
  115. const time_t utcTime = 1109964885; // Fri, 4 Mar 2005 19:34:45 UT
  116. char text[] = "Fri, 4 Mar 2005 19:34:45 A";
  117. const size_t zoneCharIndex = strlen(text) - 1;
  118. for (char militaryZone = firstChar; militaryZone <= lastChar; ++militaryZone) {
  119. time_t t = 0;
  120. const time_t offset = GetOffset(militaryZone);
  121. // the last character is replaced with next zone symbol
  122. text[zoneCharIndex] = militaryZone;
  123. UNIT_ASSERT(ParseRFC822DateTime(text, t));
  124. UNIT_ASSERT_EQUAL(t, utcTime - offset);
  125. }
  126. }
  127. Y_UNIT_TEST(TestRfc822MilitaryZones) {
  128. DoTestMilitaryZones('A', 'I');
  129. DoTestMilitaryZones('K', 'Z');
  130. DoTestMilitaryZones('a', 'i');
  131. DoTestMilitaryZones('k', 'z');
  132. }
  133. Y_UNIT_TEST(TestRfc822IncorrectDates) {
  134. bool r = true;
  135. time_t t = 0;
  136. t = 12345;
  137. r = ParseRFC822DateTime("", t);
  138. UNIT_ASSERT(!r);
  139. UNIT_ASSERT_EQUAL(t, (time_t)12345);
  140. t = 223344;
  141. r = ParseRFC822DateTime("Fri, some junk", t);
  142. UNIT_ASSERT(!r);
  143. UNIT_ASSERT_EQUAL(t, (time_t)223344);
  144. t = 54321;
  145. r = ParseRFC822DateTime("Fri, 4 Mar 2005 19:34:45 UTC", t);
  146. UNIT_ASSERT(!r);
  147. UNIT_ASSERT_EQUAL(t, (time_t)54321);
  148. // TODO: check semantic validity of parsed date (30 Feb, 88:90 etc.).
  149. // The following tests MUST fail (they don't now)
  150. // r = ParseRFC822DateTime("45 Mar 2005 19:34:45 UT", t);
  151. // UNIT_ASSERT_EQUAL(r, false);
  152. // r = ParseRFC822DateTime("29 Feb 2005 19:34:45 +0300", t);
  153. // UNIT_ASSERT_EQUAL(r, false);
  154. // r = ParseRFC822DateTime("31 Apr 2004 19:34:45 +0300", t);
  155. // UNIT_ASSERT_EQUAL(r, false);
  156. r = ParseRFC822DateTime("17 Nov 2008 19:34:45", t); // no specified time zone
  157. UNIT_ASSERT(!r);
  158. r = ParseRFC822DateTime("17 Nov 200 19:34:45 UT", t);
  159. UNIT_ASSERT(!r);
  160. r = ParseRFC822DateTime("17 Nov 8 19:34:45 UT", t);
  161. UNIT_ASSERT(!r);
  162. r = ParseRFC822DateTime("17 Nov 20008 19:34:45 UT", t);
  163. UNIT_ASSERT(!r);
  164. r = ParseRFC822DateTime("17 Nov 2008 1:34:45 UT", t);
  165. UNIT_ASSERT(!r);
  166. r = ParseRFC822DateTime("17 Nov 2008 123:34:45 UT", t);
  167. UNIT_ASSERT(!r);
  168. r = ParseRFC822DateTime("17 Nov 2008 19:1:45 UT", t);
  169. UNIT_ASSERT(!r);
  170. r = ParseRFC822DateTime("17 Nov 2008 19:123:45 UT", t);
  171. UNIT_ASSERT(!r);
  172. r = ParseRFC822DateTime("17 Nov 2008 19:34:1 UT", t);
  173. UNIT_ASSERT(!r);
  174. r = ParseRFC822DateTime("17 Nov 2008 19:34:123 UT", t);
  175. UNIT_ASSERT(!r);
  176. r = ParseRFC822DateTime("17 Nov 2008 19:34:12.12 UT", t); // fractions of second are now allowed
  177. UNIT_ASSERT(!r);
  178. r = ParseRFC822DateTime("Mon , 17 Nov 2005 19:34:45 UT", t); // space after day before the comma
  179. UNIT_ASSERT(!r);
  180. r = ParseRFC822DateTime("Mon, 17 Nov 2005 19 :34:45 UT", t);
  181. UNIT_ASSERT(!r);
  182. r = ParseRFC822DateTime("Mon, 17 Nov 2005 19: 34:45 UT", t);
  183. UNIT_ASSERT(!r);
  184. r = ParseRFC822DateTime("Mon, 17 Nov 2005 19:34 :45 UT", t);
  185. UNIT_ASSERT(!r);
  186. r = ParseRFC822DateTime("Mon, 17 Nov 2005 19:34: 45 UT", t);
  187. UNIT_ASSERT(!r);
  188. r = ParseRFC822DateTime("Monday, 17 Nov 2005 19:34:45 UT", t);
  189. UNIT_ASSERT(!r);
  190. r = ParseRFC822DateTime("Mon, 17 November 2008 19:34:45 UT", t);
  191. UNIT_ASSERT(!r);
  192. r = ParseRFC822DateTime("Mon, 17 Nov 2008 19:34:45 +3", t);
  193. UNIT_ASSERT(!r);
  194. r = ParseRFC822DateTime("Mon, 17 Nov 2008 19:34:45 +03", t);
  195. UNIT_ASSERT(!r);
  196. r = ParseRFC822DateTime("Mon, 17 Nov 2008 19:34:45 +030", t);
  197. UNIT_ASSERT(!r);
  198. r = ParseRFC822DateTime("Mon, 17 Nov 2008 19:34:45 +03030", t);
  199. UNIT_ASSERT(!r);
  200. r = ParseRFC822DateTime("Mon, 17 Nov 2008 19:34:45 -3", t);
  201. UNIT_ASSERT(!r);
  202. r = ParseRFC822DateTime("Mon, 17 Nov 2008 19:34:45 -03", t);
  203. UNIT_ASSERT(!r);
  204. r = ParseRFC822DateTime("Mon, 17 Nov 2008 19:34:45 -030", t);
  205. UNIT_ASSERT(!r);
  206. r = ParseRFC822DateTime("Mon, 17 Nov 2008 19:34:45 -03030", t);
  207. UNIT_ASSERT(!r);
  208. }
  209. Y_UNIT_TEST(TestRfc822Partial) {
  210. TRfc822DateTimeParser p;
  211. const char* part1 = "Fri, 4 Mar 05 1";
  212. const char* part2 = "9:34:45 +0300";
  213. UNIT_ASSERT(p.ParsePart(part1, strlen(part1)));
  214. UNIT_ASSERT(p.ParsePart(part2, strlen(part2)));
  215. UNIT_ASSERT_VALUES_EQUAL(TInstant::Seconds(1109954085), p.GetResult(TInstant::Max()));
  216. p = TRfc822DateTimeParser();
  217. const char* part3 = "Fri, 4 Mar 05 19:34:46 +0300";
  218. UNIT_ASSERT(p.ParsePart(part3, strlen(part3)));
  219. UNIT_ASSERT_VALUES_EQUAL(TInstant::Seconds(1109954086), p.GetResult(TInstant::Zero()));
  220. }
  221. Y_UNIT_TEST(TestIso8601Partial) {
  222. TIso8601DateTimeParser p;
  223. const char* part1 = "1990-03-15T15:1";
  224. const char* part2 = "6:17+0732";
  225. UNIT_ASSERT(p.ParsePart(part1, strlen(part1)));
  226. UNIT_ASSERT(p.ParsePart(part2, strlen(part2)));
  227. UNIT_ASSERT_VALUES_EQUAL(TInstant::Seconds(637487057), p.GetResult(TInstant::Max()));
  228. p = TIso8601DateTimeParser();
  229. const char* part3 = "1990-03-15T15:16:18+0732";
  230. UNIT_ASSERT(p.ParsePart(part3, strlen(part3)));
  231. UNIT_ASSERT_VALUES_EQUAL(TInstant::Seconds(637487058), p.GetResult(TInstant::Zero()));
  232. }
  233. Y_UNIT_TEST(TestIso8601BeforeEpoch) {
  234. TIso8601DateTimeParser p;
  235. static constexpr TStringBuf timestamp = "0001-01-01T00:00:00Z";
  236. UNIT_ASSERT(p.ParsePart(timestamp.begin(), timestamp.size()));
  237. UNIT_ASSERT_VALUES_EQUAL(p.GetDateTimeFields().Year, 1);
  238. }
  239. Y_UNIT_TEST(TestIso8601Correct) {
  240. bool ret;
  241. time_t t;
  242. // ISO 8601 actually does not allow time without time zone
  243. ret = ParseISO8601DateTime("1990-03-15", t);
  244. UNIT_ASSERT(ret);
  245. UNIT_ASSERT_VALUES_EQUAL(t, 637459200);
  246. // some normal dates
  247. ret = ParseISO8601DateTime("1990-03-15T15:16:17Z", t);
  248. UNIT_ASSERT(ret);
  249. UNIT_ASSERT_VALUES_EQUAL(t, 637514177);
  250. ret = ParseISO8601DateTime("1990-03-15t15:16:17z", t); // lower-case must be allowed too
  251. UNIT_ASSERT(ret);
  252. UNIT_ASSERT_VALUES_EQUAL(t, 637514177);
  253. ret = ParseISO8601DateTime("1990-03-15 15:16:17Z", t); // space as separator should be allowed
  254. UNIT_ASSERT(ret);
  255. UNIT_ASSERT_VALUES_EQUAL(t, 637514177);
  256. ret = ParseISO8601DateTime("1990-03-15T15:16:17.18Z", t);
  257. UNIT_ASSERT(ret);
  258. UNIT_ASSERT_VALUES_EQUAL(t, 637514177);
  259. ret = ParseISO8601DateTime("1990-03-15T15:16:17.18+07:32", t);
  260. UNIT_ASSERT(ret);
  261. UNIT_ASSERT_VALUES_EQUAL(t, 637487057);
  262. ret = ParseISO8601DateTime("1990-03-15T15:16:17.18+0732", t);
  263. UNIT_ASSERT(ret);
  264. UNIT_ASSERT_VALUES_EQUAL(t, 637487057);
  265. ret = ParseISO8601DateTime("1970-01-01T00:00:00Z", t);
  266. UNIT_ASSERT(ret);
  267. UNIT_ASSERT_VALUES_EQUAL(t, 0);
  268. ret = ParseISO8601DateTime("1970-01-01T00:01:02Z", t);
  269. UNIT_ASSERT(ret);
  270. UNIT_ASSERT_VALUES_EQUAL(t, 62);
  271. #if 0
  272. // these tests are disabled, because time zones are handled differently
  273. // in old util/ parser and agalakhov@ parser
  274. ret = ParseISO8601DateTime("1970-01-01", t);
  275. UNIT_ASSERT(ret);
  276. UNIT_ASSERT_VALUES_EQUAL(t, -4 * 3600);
  277. ret = ParseISO8601DateTime("1970-01-02", t);
  278. UNIT_ASSERT(ret);
  279. UNIT_ASSERT_VALUES_EQUAL(t, 86400 - 3 * 3600);
  280. #endif
  281. // this is wrong because of timezone
  282. ret = ParseISO8601DateTime("2009-02-14T03:31:30", t);
  283. UNIT_ASSERT(ret);
  284. UNIT_ASSERT_VALUES_EQUAL(t, 1234582290);
  285. ret = ParseISO8601DateTime("2009-02-14t03:31:30", t);
  286. UNIT_ASSERT(ret);
  287. UNIT_ASSERT_VALUES_EQUAL(t, 1234582290);
  288. ret = ParseISO8601DateTime("2009-02-14T02:31:30+0300", t);
  289. UNIT_ASSERT(ret);
  290. UNIT_ASSERT_VALUES_EQUAL(t, 1234567890);
  291. ret = ParseISO8601DateTime("2009-02-14T02:31:30+03:00", t);
  292. UNIT_ASSERT(ret);
  293. UNIT_ASSERT_VALUES_EQUAL(t, 1234567890);
  294. ret = ParseISO8601DateTime("2009-02-14 02:31:30+03:00", t);
  295. UNIT_ASSERT(ret);
  296. UNIT_ASSERT_VALUES_EQUAL(t, 1234567890);
  297. ret = ParseISO8601DateTime("2010-03-28T04:27:00.000-07:00", t);
  298. UNIT_ASSERT(ret);
  299. UNIT_ASSERT_VALUES_EQUAL(t, 1269775620);
  300. }
  301. Y_UNIT_TEST(TestIso8601TimeZone) {
  302. time_t t1, t2, t3, t4;
  303. UNIT_ASSERT(ParseISO8601DateTime("2010-03-28T04:27:00.000+07:00", t1));
  304. UNIT_ASSERT(ParseISO8601DateTime("2010-03-27T21:27:00.000Z", t2));
  305. UNIT_ASSERT(ParseISO8601DateTime("2010-03-27T22:27:00.000+0100", t3));
  306. UNIT_ASSERT(ParseISO8601DateTime("2010-03-27T20:27:00.000-01:00", t4));
  307. UNIT_ASSERT_VALUES_EQUAL(t1, t2);
  308. UNIT_ASSERT_VALUES_EQUAL(t2, t3);
  309. UNIT_ASSERT_VALUES_EQUAL(t3, t4);
  310. }
  311. Y_UNIT_TEST(TestIso8601Incorrect) {
  312. bool ret;
  313. time_t t;
  314. t = 12345;
  315. ret = ParseISO8601DateTime("", t);
  316. UNIT_ASSERT(!ret);
  317. UNIT_ASSERT_EQUAL(t, (time_t)12345);
  318. // some bad dates
  319. t = 54321;
  320. ret = ParseISO8601DateTime("a990-01-15", t);
  321. UNIT_ASSERT(!ret);
  322. UNIT_ASSERT_EQUAL(t, (time_t)54321);
  323. ret = ParseISO8601DateTime("1970-01-01T03:00:00+04:00", t); // this is 1969 GMT
  324. UNIT_ASSERT(!ret);
  325. ret = ParseISO8601DateTime("1987-13-16", t);
  326. UNIT_ASSERT(!ret);
  327. ret = ParseISO8601DateTime("1987-02-29", t);
  328. UNIT_ASSERT(!ret);
  329. ret = ParseISO8601DateTime("1990-03-151Y15:16:17.18", t);
  330. UNIT_ASSERT(!ret);
  331. ret = ParseISO8601DateTime("1990-03-151T15:16:17:43.18", t);
  332. UNIT_ASSERT(!ret);
  333. ret = ParseISO8601DateTime("1990-03-151T15:16:17.18Z+21:32", t);
  334. UNIT_ASSERT(!ret);
  335. }
  336. Y_UNIT_TEST(TestIso8601Fractions) {
  337. UNIT_ASSERT_VALUES_EQUAL(
  338. TInstant::ParseIso8601("2009-09-19 03:37:08.1+04:00"),
  339. TInstant::Seconds(1253317028) + TDuration::MilliSeconds(100));
  340. UNIT_ASSERT_VALUES_EQUAL(
  341. TInstant::ParseIso8601("2009-09-19 03:37:03.926+04:00"),
  342. TInstant::Seconds(1253317023) + TDuration::MilliSeconds(926));
  343. UNIT_ASSERT_VALUES_EQUAL(
  344. TInstant::ParseIso8601("2009-09-19 03:37:03.92622+04:00"),
  345. TInstant::Seconds(1253317023) + TDuration::MicroSeconds(926220));
  346. UNIT_ASSERT_VALUES_EQUAL(
  347. TInstant::ParseIso8601("2009-09-19 03:37:03.012331+04:00"),
  348. TInstant::Seconds(1253317023) + TDuration::MicroSeconds(12331));
  349. }
  350. Y_UNIT_TEST(TestIso8601FractionsBelowMicro) {
  351. UNIT_ASSERT_VALUES_EQUAL(
  352. TInstant::ParseIso8601("1970-01-01 00:00:00.0000000+00:00"),
  353. TInstant::Seconds(0));
  354. UNIT_ASSERT_VALUES_EQUAL(
  355. TInstant::ParseIso8601("1970-01-01 00:00:00.0000009+00:00"),
  356. TInstant::Seconds(0));
  357. UNIT_ASSERT_VALUES_EQUAL(
  358. TInstant::ParseIso8601("1970-01-01 00:00:00.000000789+00:00"),
  359. TInstant::Seconds(0));
  360. UNIT_ASSERT_VALUES_EQUAL(
  361. TInstant::ParseIso8601("1970-01-01 00:00:00.1234560+00:00"),
  362. TInstant::Seconds(0) + TDuration::MicroSeconds(123456));
  363. UNIT_ASSERT_VALUES_EQUAL(
  364. TInstant::ParseIso8601("1970-01-01 00:00:00.1234569+00:00"),
  365. TInstant::Seconds(0) + TDuration::MicroSeconds(123456));
  366. UNIT_ASSERT_VALUES_EQUAL(
  367. TInstant::ParseIso8601("1970-01-01 00:00:00.123456789+00:00"),
  368. TInstant::Seconds(0) + TDuration::MicroSeconds(123456));
  369. UNIT_ASSERT_VALUES_EQUAL(
  370. TInstant::ParseIso8601("1970-01-01 00:00:00.9999990+00:00"),
  371. TInstant::Seconds(0) + TDuration::MicroSeconds(999999));
  372. UNIT_ASSERT_VALUES_EQUAL(
  373. TInstant::ParseIso8601("1970-01-01 00:00:00.9999999+00:00"),
  374. TInstant::Seconds(0) + TDuration::MicroSeconds(999999));
  375. UNIT_ASSERT_VALUES_EQUAL(
  376. TInstant::ParseIso8601("1970-01-01 00:00:00.999999789+00:00"),
  377. TInstant::Seconds(0) + TDuration::MicroSeconds(999999));
  378. }
  379. Y_UNIT_TEST(TestIso8601BigDate) {
  380. TVector<std::pair<TString, int>> dates{
  381. {"2019-01-01", 17897},
  382. {"2037-01-01", 24472},
  383. {"2050-01-01", 29220},
  384. {"2099-01-01", 47117},
  385. {"2099-12-31", 47481},
  386. {"2100-01-01", 47482},
  387. {"2100-02-28", 47540},
  388. {"2100-03-01", 47541},
  389. };
  390. for (const auto& [date, days] : dates) {
  391. TInstant instant = TInstant::ParseIso8601(date + "T00:00:00Z");
  392. UNIT_ASSERT_VALUES_EQUAL(instant.Days(), days);
  393. }
  394. }
  395. Y_UNIT_TEST(TestHttpDate) {
  396. UNIT_ASSERT_VALUES_EQUAL(
  397. TInstant::ParseHttp("Sun, 06 Nov 1994 08:49:37 GMT"),
  398. TInstant::ParseIso8601("1994-11-06T08:49:37Z"));
  399. UNIT_ASSERT_VALUES_EQUAL(
  400. TInstant::ParseHttp("Sunday, 06-Nov-94 08:49:37 GMT"),
  401. TInstant::ParseIso8601("1994-11-06T08:49:37Z"));
  402. UNIT_ASSERT_VALUES_EQUAL(
  403. TInstant::ParseHttp("Sun Nov 6 08:49:37 1994"),
  404. TInstant::ParseIso8601("1994-11-06T08:49:37Z"));
  405. UNIT_ASSERT_VALUES_EQUAL(
  406. TInstant::ParseHttp("Mon, 19 Jan 2037 08:49:37 GMT"),
  407. TInstant::ParseIso8601("2037-01-19T08:49:37Z"));
  408. }
  409. Y_UNIT_TEST(TestHttpDateIncorrect) {
  410. bool ret;
  411. time_t t = 0;
  412. ret = ParseHTTPDateTime("1990-03-15T15:16:17Z", t);
  413. UNIT_ASSERT(!ret);
  414. }
  415. Y_UNIT_TEST(TestX509ValidityTime) {
  416. UNIT_ASSERT_VALUES_EQUAL(
  417. TInstant::ParseX509Validity("20091014165533Z"),
  418. TInstant::ParseRfc822("Wed, 14 Oct 2009 16:55:33 GMT"));
  419. UNIT_ASSERT_VALUES_EQUAL(
  420. TInstant::ParseX509Validity("990104074212Z"),
  421. TInstant::ParseRfc822("4 Jan 1999 07:42:12 GMT"));
  422. UNIT_ASSERT_VALUES_EQUAL(
  423. TInstant::ParseX509Validity("191231235959Z"),
  424. TInstant::ParseRfc822("31 Dec 2019 23:59:59 GMT"));
  425. }
  426. Y_UNIT_TEST(TestX509ValidityTimeIncorrect) {
  427. bool ret;
  428. time_t t = 0;
  429. ret = ParseX509ValidityDateTime("500101000000Z", t);
  430. UNIT_ASSERT(!ret);
  431. ret = ParseX509ValidityDateTime("091014165533+0300", t);
  432. UNIT_ASSERT(!ret);
  433. }
  434. Y_UNIT_TEST(TestTInstantTryParse) {
  435. {
  436. const TStringBuf s = "2009-09-19 03:37:08.1+04:00";
  437. const auto i = TInstant::ParseIso8601(s);
  438. TInstant iTry;
  439. UNIT_ASSERT(TInstant::TryParseIso8601(s, iTry));
  440. UNIT_ASSERT_VALUES_EQUAL(i, iTry);
  441. }
  442. {
  443. const TStringBuf s = "2009-09aslkdjfkljasdjfl4:00";
  444. TInstant iTry;
  445. UNIT_ASSERT_EXCEPTION(TInstant::ParseIso8601(s), TDateTimeParseException);
  446. UNIT_ASSERT(!TInstant::TryParseIso8601(s, iTry));
  447. }
  448. {
  449. const TStringBuf s = "Wed, 14 Oct 2009 16:55:33 GMT";
  450. const auto i = TInstant::ParseRfc822(s);
  451. TInstant iTry;
  452. UNIT_ASSERT(TInstant::TryParseRfc822(s, iTry));
  453. UNIT_ASSERT_VALUES_EQUAL(i, iTry);
  454. }
  455. {
  456. const TStringBuf s = "Wed, alsdjflkasjdfl:55:33 GMT";
  457. TInstant iTry;
  458. UNIT_ASSERT_EXCEPTION(TInstant::ParseRfc822(s), TDateTimeParseException);
  459. UNIT_ASSERT(!TInstant::TryParseRfc822(s, iTry));
  460. }
  461. {
  462. const TStringBuf s = "20091014165533Z";
  463. const auto i = TInstant::ParseX509Validity(s);
  464. TInstant iTry;
  465. UNIT_ASSERT(TInstant::TryParseX509(s, iTry));
  466. UNIT_ASSERT_VALUES_EQUAL(i, iTry);
  467. }
  468. {
  469. const TStringBuf s = "200asdfasdf533Z";
  470. TInstant iTry;
  471. UNIT_ASSERT_EXCEPTION(TInstant::ParseX509Validity(s), TDateTimeParseException);
  472. UNIT_ASSERT(!TInstant::TryParseX509(s, iTry));
  473. }
  474. {
  475. const TStringBuf s = "990104074212Z";
  476. const auto i = TInstant::ParseX509Validity(s);
  477. TInstant iTry;
  478. UNIT_ASSERT(TInstant::TryParseX509(s, iTry));
  479. UNIT_ASSERT_VALUES_EQUAL(i, iTry);
  480. }
  481. {
  482. const TStringBuf s = "9901asdf4212Z";
  483. TInstant iTry;
  484. UNIT_ASSERT_EXCEPTION(TInstant::ParseX509Validity(s), TDateTimeParseException);
  485. UNIT_ASSERT(!TInstant::TryParseX509(s, iTry));
  486. }
  487. }
  488. } // Y_UNIT_TEST_SUITE(TDateTimeParseTest)
  489. Y_UNIT_TEST_SUITE(TDurationParseTest) {
  490. Y_UNIT_TEST(TestParse) {
  491. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(60 * 60 * 24 * 7), TDuration::Parse("1w"));
  492. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(60), TDuration::Parse("1m"));
  493. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(90), TDuration::Parse("1.5m"));
  494. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(102), TDuration::Parse("1.7m"));
  495. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(119400), TDuration::Parse("1.99m"));
  496. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(119940), TDuration::Parse("1.999m"));
  497. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(119994), TDuration::Parse("1.9999m"));
  498. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(60), TDuration::Parse("1h"));
  499. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(90), TDuration::Parse("1.5h"));
  500. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(102), TDuration::Parse("1.7h"));
  501. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(7164), TDuration::Parse("1.99h"));
  502. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(7196400), TDuration::Parse("1.999h"));
  503. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(7199640), TDuration::Parse("1.9999h"));
  504. UNIT_ASSERT_EQUAL(TDuration::Minutes(15), TDuration::Parse("15m"));
  505. UNIT_ASSERT_EQUAL(TDuration::Hours(10), TDuration::Parse("10h"));
  506. UNIT_ASSERT_EQUAL(TDuration::Days(365), TDuration::Parse("365d"));
  507. UNIT_ASSERT_EQUAL(TDuration::Hours(36), TDuration::Parse("1.5d"));
  508. UNIT_ASSERT_VALUES_EQUAL(TDuration::Hours(24), TDuration::Parse("1d"));
  509. UNIT_ASSERT_VALUES_EQUAL(TDuration::Hours(36), TDuration::Parse("1.5d"));
  510. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(2448), TDuration::Parse("1.7d"));
  511. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(171936), TDuration::Parse("1.99d"));
  512. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(172713600), TDuration::Parse("1.999d"));
  513. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(172791360), TDuration::Parse("1.9999d"));
  514. #if 0 // not implemented
  515. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(90), TDuration::Parse("1m30s"));
  516. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(90), TDuration::Parse("1h30m"));
  517. UNIT_ASSERT_VALUES_EQUAL(TDuration::Hours(36), TDuration::Parse("1d12h"));
  518. #endif
  519. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(10), TDuration::Parse("10s"));
  520. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(10), TDuration::Parse("10.000s"));
  521. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(4), TDuration::Parse("0.000004s"));
  522. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(3456), TDuration::Parse("3.456s"));
  523. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(3450), TDuration::Parse("3.450s"));
  524. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(3450), TDuration::Parse("3.45000000s"));
  525. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(3450), TDuration::Parse("3.45s"));
  526. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(1), TDuration::Parse("1ms"));
  527. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(1100), TDuration::Parse("1.1ms"));
  528. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(112), TDuration::Parse("112"));
  529. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(14456), TDuration::Parse("14456us"));
  530. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(1), TDuration::Parse("1000ns"));
  531. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(1), TDuration::Parse("0.000001s"));
  532. UNIT_ASSERT_EQUAL(TDuration(), TDuration::Parse("10ns")); // TDuration has 1us precision.
  533. }
  534. } // Y_UNIT_TEST_SUITE(TDurationParseTest)