parser.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #pragma once
  2. // probably you do not need to include this file directly, use "util/datetime/base.h"
  3. #include "base.h"
  4. struct TDateTimeFields {
  5. TDateTimeFields() {
  6. Zero(*this);
  7. ZoneOffsetMinutes = 0;
  8. Hour = 0;
  9. }
  10. ui32 Year;
  11. ui32 Month; // 1..12
  12. ui32 Day; // 1 .. 31
  13. ui32 Hour; // 0 .. 23
  14. ui32 Minute; // 0 .. 59
  15. ui32 Second; // 0 .. 60
  16. ui32 MicroSecond; // 0 .. 999999
  17. i32 ZoneOffsetMinutes;
  18. void SetLooseYear(ui32 year) {
  19. if (year < 60) {
  20. year += 100;
  21. }
  22. if (year < 160) {
  23. year += 1900;
  24. }
  25. Year = year;
  26. }
  27. bool IsOk() const noexcept {
  28. if (Year < 1970) {
  29. return false;
  30. }
  31. if (Month < 1 || Month > 12) {
  32. return false;
  33. }
  34. unsigned int maxMonthDay = 31;
  35. if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
  36. maxMonthDay = 30;
  37. } else if (Month == 2) {
  38. if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)) {
  39. // leap year
  40. maxMonthDay = 29;
  41. } else {
  42. maxMonthDay = 28;
  43. }
  44. }
  45. if (Day > maxMonthDay) {
  46. return false;
  47. }
  48. if (Hour > 23) {
  49. return false;
  50. }
  51. if (Minute > 59) {
  52. return false;
  53. }
  54. // handle leap second which is explicitly allowed by ISO 8601:2004(E) $2.2.2
  55. // https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
  56. if (Second > 60) {
  57. return false;
  58. }
  59. if (MicroSecond > 999999) {
  60. return false;
  61. }
  62. if (Year == 1970 && Month == 1 && Day == 1) {
  63. if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes)) {
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. TInstant ToInstant(TInstant defaultValue) const {
  70. time_t tt = ToTimeT(-1);
  71. if (tt == -1) {
  72. return defaultValue;
  73. }
  74. return TInstant::Seconds(tt) + TDuration::MicroSeconds(MicroSecond);
  75. }
  76. time_t ToTimeT(time_t defaultValue) const {
  77. if (!IsOk()) {
  78. return defaultValue;
  79. }
  80. struct tm tm;
  81. Zero(tm);
  82. tm.tm_year = Year - 1900;
  83. tm.tm_mon = Month - 1;
  84. tm.tm_mday = Day;
  85. tm.tm_hour = Hour;
  86. tm.tm_min = Minute;
  87. tm.tm_sec = Second;
  88. time_t tt = TimeGM(&tm);
  89. if (tt == -1) {
  90. return defaultValue;
  91. }
  92. return tt - ZoneOffsetMinutes * 60;
  93. }
  94. };
  95. class TDateTimeParserBase {
  96. public:
  97. const TDateTimeFields& GetDateTimeFields() const {
  98. return DateTimeFields;
  99. }
  100. protected:
  101. TDateTimeFields DateTimeFields;
  102. int cs; // for ragel
  103. int Sign;
  104. int I;
  105. int Dc;
  106. protected:
  107. TDateTimeParserBase()
  108. : DateTimeFields()
  109. , cs(0)
  110. , Sign(0)
  111. , I(0xDEADBEEF) // to guarantee unittest break if ragel code is incorrect
  112. , Dc(0xDEADBEEF)
  113. {
  114. }
  115. inline TInstant GetResult(int firstFinalState, TInstant defaultValue) const {
  116. if (cs < firstFinalState) {
  117. return defaultValue;
  118. }
  119. return DateTimeFields.ToInstant(defaultValue);
  120. }
  121. };
  122. #define DECLARE_PARSER(CLASS) \
  123. struct CLASS: public TDateTimeParserBase { \
  124. CLASS(); \
  125. bool ParsePart(const char* input, size_t len); \
  126. TInstant GetResult(TInstant defaultValue) const; \
  127. };
  128. DECLARE_PARSER(TIso8601DateTimeParser)
  129. DECLARE_PARSER(TRfc822DateTimeParser)
  130. DECLARE_PARSER(THttpDateTimeParser)
  131. DECLARE_PARSER(TX509ValidityDateTimeParser)
  132. DECLARE_PARSER(TX509Validity4yDateTimeParser)
  133. #undef DECLARE_PARSER
  134. struct TDurationParser {
  135. int cs;
  136. ui64 I;
  137. ui32 Dc;
  138. i32 MultiplierPower; // 6 for seconds, 0 for microseconds, -3 for nanoseconds
  139. i32 Multiplier;
  140. ui64 IntegerPart;
  141. ui32 FractionPart;
  142. ui32 FractionDigits;
  143. TDurationParser();
  144. bool ParsePart(const char* input, size_t len);
  145. TDuration GetResult(TDuration defaultValue) const;
  146. };
  147. /**
  148. Depcrecated cause of default hour offset (+4 hours)
  149. @see IGNIETFERRO-823
  150. */
  151. struct TDateTimeFieldsDeprecated {
  152. TDateTimeFieldsDeprecated() {
  153. Zero(*this);
  154. ZoneOffsetMinutes = (i32)TDuration::Hours(4).Minutes(); // legacy code
  155. Hour = 11;
  156. }
  157. ui32 Year;
  158. ui32 Month; // 1..12
  159. ui32 Day; // 1 .. 31
  160. ui32 Hour; // 0 .. 23
  161. ui32 Minute; // 0 .. 59
  162. ui32 Second; // 0 .. 60
  163. ui32 MicroSecond; // 0 .. 999999
  164. i32 ZoneOffsetMinutes;
  165. void SetLooseYear(ui32 year) {
  166. if (year < 60) {
  167. year += 100;
  168. }
  169. if (year < 160) {
  170. year += 1900;
  171. }
  172. Year = year;
  173. }
  174. bool IsOk() const noexcept {
  175. if (Year < 1970) {
  176. return false;
  177. }
  178. if (Month < 1 || Month > 12) {
  179. return false;
  180. }
  181. unsigned int maxMonthDay = 31;
  182. if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
  183. maxMonthDay = 30;
  184. } else if (Month == 2) {
  185. if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)) {
  186. // leap year
  187. maxMonthDay = 29;
  188. } else {
  189. maxMonthDay = 28;
  190. }
  191. }
  192. if (Day > maxMonthDay) {
  193. return false;
  194. }
  195. if (Hour > 23) {
  196. return false;
  197. }
  198. if (Minute > 59) {
  199. return false;
  200. }
  201. if (Second > 60) {
  202. return false;
  203. }
  204. if (MicroSecond > 999999) {
  205. return false;
  206. }
  207. if (Year == 1970 && Month == 1 && Day == 1) {
  208. if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes)) {
  209. return false;
  210. }
  211. }
  212. return true;
  213. }
  214. TInstant ToInstant(TInstant defaultValue) const {
  215. time_t tt = ToTimeT(-1);
  216. if (tt == -1) {
  217. return defaultValue;
  218. }
  219. return TInstant::Seconds(tt) + TDuration::MicroSeconds(MicroSecond);
  220. }
  221. time_t ToTimeT(time_t defaultValue) const {
  222. if (!IsOk()) {
  223. return defaultValue;
  224. }
  225. struct tm tm;
  226. Zero(tm);
  227. tm.tm_year = Year - 1900;
  228. tm.tm_mon = Month - 1;
  229. tm.tm_mday = Day;
  230. tm.tm_hour = Hour;
  231. tm.tm_min = Minute;
  232. tm.tm_sec = Second;
  233. time_t tt = TimeGM(&tm);
  234. if (tt == -1) {
  235. return defaultValue;
  236. }
  237. return tt - ZoneOffsetMinutes * 60;
  238. }
  239. };
  240. class TDateTimeParserBaseDeprecated {
  241. public:
  242. const TDateTimeFieldsDeprecated& GetDateTimeFields() const {
  243. return DateTimeFields;
  244. }
  245. protected:
  246. TDateTimeFieldsDeprecated DateTimeFields;
  247. int cs; // for ragel
  248. int Sign;
  249. int I;
  250. int Dc;
  251. protected:
  252. TDateTimeParserBaseDeprecated()
  253. : DateTimeFields()
  254. , cs(0)
  255. , Sign(0)
  256. , I(0xDEADBEEF) // to guarantee unittest break if ragel code is incorrect
  257. , Dc(0xDEADBEEF)
  258. {
  259. }
  260. inline TInstant GetResult(int firstFinalState, TInstant defaultValue) const {
  261. if (cs < firstFinalState) {
  262. return defaultValue;
  263. }
  264. return DateTimeFields.ToInstant(defaultValue);
  265. }
  266. };
  267. #define DECLARE_PARSER(CLASS) \
  268. struct CLASS: public TDateTimeParserBaseDeprecated { \
  269. CLASS(); \
  270. bool ParsePart(const char* input, size_t len); \
  271. TInstant GetResult(TInstant defaultValue) const; \
  272. };
  273. DECLARE_PARSER(TIso8601DateTimeParserDeprecated)
  274. DECLARE_PARSER(TRfc822DateTimeParserDeprecated)
  275. DECLARE_PARSER(THttpDateTimeParserDeprecated)
  276. DECLARE_PARSER(TX509ValidityDateTimeParserDeprecated)
  277. DECLARE_PARSER(TX509Validity4yDateTimeParserDeprecated)
  278. #undef DECLARE_PARSER