parser_deprecated_ut.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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(TDateTimeParseTestDeprecated) {
  6. Y_UNIT_TEST(TestRfc822CorrectDeprecated) {
  7. bool r = false;
  8. time_t t = 0;
  9. // predefined time zones
  10. r = ParseRFC822DateTimeDeprecated("Fri, 4 Mar 2005 19:34:45 UT", t);
  11. UNIT_ASSERT(r);
  12. UNIT_ASSERT_EQUAL(t, (time_t)1109964885);
  13. r = ParseRFC822DateTimeDeprecated("Fri, 4 Mar 2005 19:34:45 GMT", t);
  14. UNIT_ASSERT(r);
  15. UNIT_ASSERT_EQUAL(t, (time_t)1109964885);
  16. r = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("Fri, 4 Mar 2005 19:34:45 +0300", t);
  54. UNIT_ASSERT(r);
  55. UNIT_ASSERT_EQUAL(t, (time_t)1109954085);
  56. r = ParseRFC822DateTimeDeprecated("Fri, 4 Mar 05 19:34:45 +0300", t);
  57. UNIT_ASSERT(r);
  58. UNIT_ASSERT_EQUAL(t, (time_t)1109954085);
  59. r = ParseRFC822DateTimeDeprecated("21 Apr 1999 23:40:00 +0400", t);
  60. UNIT_ASSERT(r);
  61. UNIT_ASSERT_EQUAL(t, (time_t)924723600);
  62. r = ParseRFC822DateTimeDeprecated("21 Apr 99 23:40 +0400", t);
  63. UNIT_ASSERT(r);
  64. UNIT_ASSERT_EQUAL(t, (time_t)924723600);
  65. r = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated(" 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 = ParseRFC822DateTimeDeprecated(" \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 = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("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(ParseRFC822DateTimeDeprecated(text, t));
  124. UNIT_ASSERT_EQUAL(t, utcTime - offset);
  125. }
  126. }
  127. Y_UNIT_TEST(TestRfc822MilitaryZonesDeprecated) {
  128. DoTestMilitaryZones('A', 'I');
  129. DoTestMilitaryZones('K', 'Z');
  130. DoTestMilitaryZones('a', 'i');
  131. DoTestMilitaryZones('k', 'z');
  132. }
  133. Y_UNIT_TEST(TestRfc822IncorrectDatesDeprecated) {
  134. bool r = true;
  135. time_t t = 0;
  136. t = 12345;
  137. r = ParseRFC822DateTimeDeprecated("", t);
  138. UNIT_ASSERT(!r);
  139. UNIT_ASSERT_EQUAL(t, (time_t)12345);
  140. t = 223344;
  141. r = ParseRFC822DateTimeDeprecated("Fri, some junk", t);
  142. UNIT_ASSERT(!r);
  143. UNIT_ASSERT_EQUAL(t, (time_t)223344);
  144. t = 54321;
  145. r = ParseRFC822DateTimeDeprecated("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 = ParseRFC822DateTimeDeprecated("45 Mar 2005 19:34:45 UT", t);
  151. // UNIT_ASSERT_EQUAL(r, false);
  152. // r = ParseRFC822DateTimeDeprecated("29 Feb 2005 19:34:45 +0300", t);
  153. // UNIT_ASSERT_EQUAL(r, false);
  154. // r = ParseRFC822DateTimeDeprecated("31 Apr 2004 19:34:45 +0300", t);
  155. // UNIT_ASSERT_EQUAL(r, false);
  156. r = ParseRFC822DateTimeDeprecated("17 Nov 2008 19:34:45", t); // no specified time zone
  157. UNIT_ASSERT(!r);
  158. r = ParseRFC822DateTimeDeprecated("17 Nov 200 19:34:45 UT", t);
  159. UNIT_ASSERT(!r);
  160. r = ParseRFC822DateTimeDeprecated("17 Nov 8 19:34:45 UT", t);
  161. UNIT_ASSERT(!r);
  162. r = ParseRFC822DateTimeDeprecated("17 Nov 20008 19:34:45 UT", t);
  163. UNIT_ASSERT(!r);
  164. r = ParseRFC822DateTimeDeprecated("17 Nov 2008 1:34:45 UT", t);
  165. UNIT_ASSERT(!r);
  166. r = ParseRFC822DateTimeDeprecated("17 Nov 2008 123:34:45 UT", t);
  167. UNIT_ASSERT(!r);
  168. r = ParseRFC822DateTimeDeprecated("17 Nov 2008 19:1:45 UT", t);
  169. UNIT_ASSERT(!r);
  170. r = ParseRFC822DateTimeDeprecated("17 Nov 2008 19:123:45 UT", t);
  171. UNIT_ASSERT(!r);
  172. r = ParseRFC822DateTimeDeprecated("17 Nov 2008 19:34:1 UT", t);
  173. UNIT_ASSERT(!r);
  174. r = ParseRFC822DateTimeDeprecated("17 Nov 2008 19:34:123 UT", t);
  175. UNIT_ASSERT(!r);
  176. r = ParseRFC822DateTimeDeprecated("17 Nov 2008 19:34:12.12 UT", t); // fractions of second are now allowed
  177. UNIT_ASSERT(!r);
  178. r = ParseRFC822DateTimeDeprecated("Mon , 17 Nov 2005 19:34:45 UT", t); // space after day before the comma
  179. UNIT_ASSERT(!r);
  180. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2005 19 :34:45 UT", t);
  181. UNIT_ASSERT(!r);
  182. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2005 19: 34:45 UT", t);
  183. UNIT_ASSERT(!r);
  184. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2005 19:34 :45 UT", t);
  185. UNIT_ASSERT(!r);
  186. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2005 19:34: 45 UT", t);
  187. UNIT_ASSERT(!r);
  188. r = ParseRFC822DateTimeDeprecated("Monday, 17 Nov 2005 19:34:45 UT", t);
  189. UNIT_ASSERT(!r);
  190. r = ParseRFC822DateTimeDeprecated("Mon, 17 November 2008 19:34:45 UT", t);
  191. UNIT_ASSERT(!r);
  192. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2008 19:34:45 +3", t);
  193. UNIT_ASSERT(!r);
  194. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2008 19:34:45 +03", t);
  195. UNIT_ASSERT(!r);
  196. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2008 19:34:45 +030", t);
  197. UNIT_ASSERT(!r);
  198. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2008 19:34:45 +03030", t);
  199. UNIT_ASSERT(!r);
  200. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2008 19:34:45 -3", t);
  201. UNIT_ASSERT(!r);
  202. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2008 19:34:45 -03", t);
  203. UNIT_ASSERT(!r);
  204. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2008 19:34:45 -030", t);
  205. UNIT_ASSERT(!r);
  206. r = ParseRFC822DateTimeDeprecated("Mon, 17 Nov 2008 19:34:45 -03030", t);
  207. UNIT_ASSERT(!r);
  208. }
  209. Y_UNIT_TEST(TestRfc822PartialDeprecated) {
  210. TRfc822DateTimeParserDeprecated 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 = TRfc822DateTimeParserDeprecated();
  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(TestIso8601PartialDeprecated) {
  222. TIso8601DateTimeParserDeprecated 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 = TIso8601DateTimeParserDeprecated();
  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(TestIso8601CorrectDeprecatedDeprecated) {
  234. bool ret;
  235. time_t t;
  236. // ISO 8601 actually does not allow time without time zone
  237. ret = ParseISO8601DateTimeDeprecated("1990-03-15", t);
  238. UNIT_ASSERT(ret);
  239. UNIT_ASSERT_VALUES_EQUAL(t, 637484400);
  240. // some normal dates
  241. ret = ParseISO8601DateTimeDeprecated("1990-03-15T15:16:17Z", t);
  242. UNIT_ASSERT(ret);
  243. UNIT_ASSERT_VALUES_EQUAL(t, 637514177);
  244. ret = ParseISO8601DateTimeDeprecated("1990-03-15t15:16:17z", t); // lower-case must be allowed too
  245. UNIT_ASSERT(ret);
  246. UNIT_ASSERT_VALUES_EQUAL(t, 637514177);
  247. ret = ParseISO8601DateTimeDeprecated("1990-03-15 15:16:17Z", t); // space as separator should be allowed
  248. UNIT_ASSERT(ret);
  249. UNIT_ASSERT_VALUES_EQUAL(t, 637514177);
  250. ret = ParseISO8601DateTimeDeprecated("1990-03-15T15:16:17.18Z", t);
  251. UNIT_ASSERT(ret);
  252. UNIT_ASSERT_VALUES_EQUAL(t, 637514177);
  253. ret = ParseISO8601DateTimeDeprecated("1990-03-15T15:16:17.18+07:32", t);
  254. UNIT_ASSERT(ret);
  255. UNIT_ASSERT_VALUES_EQUAL(t, 637487057);
  256. ret = ParseISO8601DateTimeDeprecated("1990-03-15T15:16:17.18+0732", t);
  257. UNIT_ASSERT(ret);
  258. UNIT_ASSERT_VALUES_EQUAL(t, 637487057);
  259. ret = ParseISO8601DateTimeDeprecated("1970-01-01T00:00:00Z", t);
  260. UNIT_ASSERT(ret);
  261. UNIT_ASSERT_VALUES_EQUAL(t, 0);
  262. ret = ParseISO8601DateTimeDeprecated("1970-01-01T00:01:02Z", t);
  263. UNIT_ASSERT(ret);
  264. UNIT_ASSERT_VALUES_EQUAL(t, 62);
  265. #if 0
  266. // these tests are disabled, because time zones are handled differently
  267. // in old util/ parser and agalakhov@ parser
  268. ret = ParseISO8601DateTimeDeprecated("1970-01-01", t);
  269. UNIT_ASSERT(ret);
  270. UNIT_ASSERT_VALUES_EQUAL(t, -4 * 3600);
  271. ret = ParseISO8601DateTimeDeprecated("1970-01-02", t);
  272. UNIT_ASSERT(ret);
  273. UNIT_ASSERT_VALUES_EQUAL(t, 86400 - 3 * 3600);
  274. #endif
  275. // this is wrong because of timezone
  276. ret = ParseISO8601DateTimeDeprecated("2009-02-14T03:31:30", t);
  277. UNIT_ASSERT(ret);
  278. UNIT_ASSERT_VALUES_EQUAL(t, 1234567890);
  279. ret = ParseISO8601DateTimeDeprecated("2009-02-14t03:31:30", t);
  280. UNIT_ASSERT(ret);
  281. UNIT_ASSERT_VALUES_EQUAL(t, 1234567890);
  282. ret = ParseISO8601DateTimeDeprecated("2009-02-14T02:31:30+0300", t);
  283. UNIT_ASSERT(ret);
  284. UNIT_ASSERT_VALUES_EQUAL(t, 1234567890);
  285. ret = ParseISO8601DateTimeDeprecated("2009-02-14T02:31:30+03:00", t);
  286. UNIT_ASSERT(ret);
  287. UNIT_ASSERT_VALUES_EQUAL(t, 1234567890);
  288. ret = ParseISO8601DateTimeDeprecated("2009-02-14 02:31:30+03:00", t);
  289. UNIT_ASSERT(ret);
  290. UNIT_ASSERT_VALUES_EQUAL(t, 1234567890);
  291. ret = ParseISO8601DateTimeDeprecated("2010-03-28T04:27:00.000-07:00", t);
  292. UNIT_ASSERT(ret);
  293. UNIT_ASSERT_VALUES_EQUAL(t, 1269775620);
  294. }
  295. Y_UNIT_TEST(TestIso8601TimeZoneDeprecated) {
  296. time_t t1, t2, t3, t4;
  297. UNIT_ASSERT(ParseISO8601DateTimeDeprecated("2010-03-28T04:27:00.000+07:00", t1));
  298. UNIT_ASSERT(ParseISO8601DateTimeDeprecated("2010-03-27T21:27:00.000Z", t2));
  299. UNIT_ASSERT(ParseISO8601DateTimeDeprecated("2010-03-27T22:27:00.000+0100", t3));
  300. UNIT_ASSERT(ParseISO8601DateTimeDeprecated("2010-03-27T20:27:00.000-01:00", t4));
  301. UNIT_ASSERT_VALUES_EQUAL(t1, t2);
  302. UNIT_ASSERT_VALUES_EQUAL(t2, t3);
  303. UNIT_ASSERT_VALUES_EQUAL(t3, t4);
  304. }
  305. Y_UNIT_TEST(TestIso8601IncorrectDeprecated) {
  306. bool ret;
  307. time_t t;
  308. t = 12345;
  309. ret = ParseISO8601DateTimeDeprecated("", t);
  310. UNIT_ASSERT(!ret);
  311. UNIT_ASSERT_EQUAL(t, (time_t)12345);
  312. // some bad dates
  313. t = 54321;
  314. ret = ParseISO8601DateTimeDeprecated("a990-01-15", t);
  315. UNIT_ASSERT(!ret);
  316. UNIT_ASSERT_EQUAL(t, (time_t)54321);
  317. ret = ParseISO8601DateTimeDeprecated("1970-01-01T03:00:00+04:00", t); // this is 1969 GMT
  318. UNIT_ASSERT(!ret);
  319. ret = ParseISO8601DateTimeDeprecated("1987-13-16", t);
  320. UNIT_ASSERT(!ret);
  321. ret = ParseISO8601DateTimeDeprecated("1987-02-29", t);
  322. UNIT_ASSERT(!ret);
  323. ret = ParseISO8601DateTimeDeprecated("1990-03-151Y15:16:17.18", t);
  324. UNIT_ASSERT(!ret);
  325. ret = ParseISO8601DateTimeDeprecated("1990-03-151T15:16:17:43.18", t);
  326. UNIT_ASSERT(!ret);
  327. ret = ParseISO8601DateTimeDeprecated("1990-03-151T15:16:17.18Z+21:32", t);
  328. UNIT_ASSERT(!ret);
  329. }
  330. Y_UNIT_TEST(TestIso8601FractionsDeprecated) {
  331. UNIT_ASSERT_VALUES_EQUAL(
  332. TInstant::ParseIso8601Deprecated("2009-09-19 03:37:08.1+04:00"),
  333. TInstant::Seconds(1253317028) + TDuration::MilliSeconds(100));
  334. UNIT_ASSERT_VALUES_EQUAL(
  335. TInstant::ParseIso8601Deprecated("2009-09-19 03:37:03.926+04:00"),
  336. TInstant::Seconds(1253317023) + TDuration::MilliSeconds(926));
  337. UNIT_ASSERT_VALUES_EQUAL(
  338. TInstant::ParseIso8601Deprecated("2009-09-19 03:37:03.92622+04:00"),
  339. TInstant::Seconds(1253317023) + TDuration::MicroSeconds(926220));
  340. UNIT_ASSERT_VALUES_EQUAL(
  341. TInstant::ParseIso8601Deprecated("2009-09-19 03:37:03.012331+04:00"),
  342. TInstant::Seconds(1253317023) + TDuration::MicroSeconds(12331));
  343. }
  344. Y_UNIT_TEST(TestHttpDateDeprecated) {
  345. UNIT_ASSERT_VALUES_EQUAL(
  346. TInstant::ParseHttpDeprecated("Sun, 06 Nov 1994 08:49:37 GMT"),
  347. TInstant::ParseIso8601Deprecated("1994-11-06T08:49:37Z"));
  348. UNIT_ASSERT_VALUES_EQUAL(
  349. TInstant::ParseHttpDeprecated("Sunday, 06-Nov-94 08:49:37 GMT"),
  350. TInstant::ParseIso8601Deprecated("1994-11-06T08:49:37Z"));
  351. UNIT_ASSERT_VALUES_EQUAL(
  352. TInstant::ParseHttpDeprecated("Sun Nov 6 08:49:37 1994"),
  353. TInstant::ParseIso8601Deprecated("1994-11-06T08:49:37Z"));
  354. UNIT_ASSERT_VALUES_EQUAL(
  355. TInstant::ParseHttpDeprecated("Mon, 19 Jan 2037 08:49:37 GMT"),
  356. TInstant::ParseIso8601Deprecated("2037-01-19T08:49:37Z"));
  357. }
  358. Y_UNIT_TEST(TestHttpDateIncorrectDeprecated) {
  359. bool ret;
  360. time_t t = 0;
  361. ret = ParseHTTPDateTimeDeprecated("1990-03-15T15:16:17Z", t);
  362. UNIT_ASSERT(!ret);
  363. }
  364. Y_UNIT_TEST(TestX509ValidityTimeDeprecated) {
  365. UNIT_ASSERT_VALUES_EQUAL(
  366. TInstant::ParseX509ValidityDeprecated("20091014165533Z"),
  367. TInstant::ParseRfc822Deprecated("Wed, 14 Oct 2009 16:55:33 GMT"));
  368. UNIT_ASSERT_VALUES_EQUAL(
  369. TInstant::ParseX509ValidityDeprecated("990104074212Z"),
  370. TInstant::ParseRfc822Deprecated("4 Jan 1999 07:42:12 GMT"));
  371. UNIT_ASSERT_VALUES_EQUAL(
  372. TInstant::ParseX509ValidityDeprecated("191231235959Z"),
  373. TInstant::ParseRfc822Deprecated("31 Dec 2019 23:59:59 GMT"));
  374. }
  375. Y_UNIT_TEST(TestX509ValidityTimeIncorrectDeprecated) {
  376. bool ret;
  377. time_t t = 0;
  378. ret = ParseX509ValidityDateTimeDeprecated("500101000000Z", t);
  379. UNIT_ASSERT(!ret);
  380. ret = ParseX509ValidityDateTimeDeprecated("091014165533+0300", t);
  381. UNIT_ASSERT(!ret);
  382. }
  383. Y_UNIT_TEST(TestTInstantTryParseDeprecated) {
  384. {
  385. const TStringBuf s = "2009-09-19 03:37:08.1+04:00";
  386. const auto i = TInstant::ParseIso8601Deprecated(s);
  387. TInstant iTry;
  388. UNIT_ASSERT(TInstant::TryParseIso8601Deprecated(s, iTry));
  389. UNIT_ASSERT_VALUES_EQUAL(i, iTry);
  390. }
  391. {
  392. const TStringBuf s = "2009-09aslkdjfkljasdjfl4:00";
  393. TInstant iTry;
  394. UNIT_ASSERT_EXCEPTION(TInstant::ParseIso8601Deprecated(s), TDateTimeParseException);
  395. UNIT_ASSERT(!TInstant::TryParseIso8601Deprecated(s, iTry));
  396. }
  397. {
  398. const TStringBuf s = "Wed, 14 Oct 2009 16:55:33 GMT";
  399. const auto i = TInstant::ParseRfc822Deprecated(s);
  400. TInstant iTry;
  401. UNIT_ASSERT(TInstant::TryParseRfc822Deprecated(s, iTry));
  402. UNIT_ASSERT_VALUES_EQUAL(i, iTry);
  403. }
  404. {
  405. const TStringBuf s = "Wed, alsdjflkasjdfl:55:33 GMT";
  406. TInstant iTry;
  407. UNIT_ASSERT_EXCEPTION(TInstant::ParseRfc822Deprecated(s), TDateTimeParseException);
  408. UNIT_ASSERT(!TInstant::TryParseRfc822Deprecated(s, iTry));
  409. }
  410. {
  411. const TStringBuf s = "20091014165533Z";
  412. const auto i = TInstant::ParseX509ValidityDeprecated(s);
  413. TInstant iTry;
  414. UNIT_ASSERT(TInstant::TryParseX509Deprecated(s, iTry));
  415. UNIT_ASSERT_VALUES_EQUAL(i, iTry);
  416. }
  417. {
  418. const TStringBuf s = "200asdfasdf533Z";
  419. TInstant iTry;
  420. UNIT_ASSERT_EXCEPTION(TInstant::ParseX509ValidityDeprecated(s), TDateTimeParseException);
  421. UNIT_ASSERT(!TInstant::TryParseX509Deprecated(s, iTry));
  422. }
  423. {
  424. const TStringBuf s = "990104074212Z";
  425. const auto i = TInstant::ParseX509ValidityDeprecated(s);
  426. TInstant iTry;
  427. UNIT_ASSERT(TInstant::TryParseX509Deprecated(s, iTry));
  428. UNIT_ASSERT_VALUES_EQUAL(i, iTry);
  429. }
  430. {
  431. const TStringBuf s = "9901asdf4212Z";
  432. TInstant iTry;
  433. UNIT_ASSERT_EXCEPTION(TInstant::ParseX509ValidityDeprecated(s), TDateTimeParseException);
  434. UNIT_ASSERT(!TInstant::TryParseX509Deprecated(s, iTry));
  435. }
  436. }
  437. }
  438. Y_UNIT_TEST_SUITE(TDurationParseTestDeprecated) {
  439. Y_UNIT_TEST(TestParseDeprecated) {
  440. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(60), TDuration::Parse("1m"));
  441. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(90), TDuration::Parse("1.5m"));
  442. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(102), TDuration::Parse("1.7m"));
  443. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(119400), TDuration::Parse("1.99m"));
  444. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(119940), TDuration::Parse("1.999m"));
  445. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(119994), TDuration::Parse("1.9999m"));
  446. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(60), TDuration::Parse("1h"));
  447. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(90), TDuration::Parse("1.5h"));
  448. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(102), TDuration::Parse("1.7h"));
  449. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(7164), TDuration::Parse("1.99h"));
  450. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(7196400), TDuration::Parse("1.999h"));
  451. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(7199640), TDuration::Parse("1.9999h"));
  452. UNIT_ASSERT_EQUAL(TDuration::Minutes(15), TDuration::Parse("15m"));
  453. UNIT_ASSERT_EQUAL(TDuration::Hours(10), TDuration::Parse("10h"));
  454. UNIT_ASSERT_EQUAL(TDuration::Days(365), TDuration::Parse("365d"));
  455. UNIT_ASSERT_EQUAL(TDuration::Hours(36), TDuration::Parse("1.5d"));
  456. UNIT_ASSERT_VALUES_EQUAL(TDuration::Hours(24), TDuration::Parse("1d"));
  457. UNIT_ASSERT_VALUES_EQUAL(TDuration::Hours(36), TDuration::Parse("1.5d"));
  458. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(2448), TDuration::Parse("1.7d"));
  459. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(171936), TDuration::Parse("1.99d"));
  460. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(172713600), TDuration::Parse("1.999d"));
  461. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(172791360), TDuration::Parse("1.9999d"));
  462. #if 0 // not implemented
  463. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(90), TDuration::Parse("1m30s"));
  464. UNIT_ASSERT_VALUES_EQUAL(TDuration::Minutes(90), TDuration::Parse("1h30m"));
  465. UNIT_ASSERT_VALUES_EQUAL(TDuration::Hours(36), TDuration::Parse("1d12h"));
  466. #endif
  467. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(10), TDuration::Parse("10s"));
  468. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(10), TDuration::Parse("10.000s"));
  469. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(4), TDuration::Parse("0.000004s"));
  470. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(3456), TDuration::Parse("3.456s"));
  471. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(3450), TDuration::Parse("3.450s"));
  472. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(3450), TDuration::Parse("3.45000000s"));
  473. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(3450), TDuration::Parse("3.45s"));
  474. UNIT_ASSERT_VALUES_EQUAL(TDuration::MilliSeconds(1), TDuration::Parse("1ms"));
  475. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(1100), TDuration::Parse("1.1ms"));
  476. UNIT_ASSERT_VALUES_EQUAL(TDuration::Seconds(112), TDuration::Parse("112"));
  477. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(14456), TDuration::Parse("14456us"));
  478. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(1), TDuration::Parse("1000ns"));
  479. UNIT_ASSERT_VALUES_EQUAL(TDuration::MicroSeconds(1), TDuration::Parse("0.000001s"));
  480. UNIT_ASSERT_EQUAL(TDuration(), TDuration::Parse("10ns")); // TDuration has 1us precision.
  481. }
  482. }