PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/v8/src/dateparser-inl.h

https://github.com/osfreak/node
C Header | 311 lines | 220 code | 16 blank | 75 comment | 99 complexity | 8999f8d94e3da908dc527814cf06fdeb MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause, WTFPL, MPL-2.0-no-copyleft-exception, GPL-2.0, Apache-2.0, MIT, AGPL-3.0, ISC
  1. // Copyright 2011 the V8 project authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef V8_DATEPARSER_INL_H_
  5. #define V8_DATEPARSER_INL_H_
  6. #include "dateparser.h"
  7. namespace v8 {
  8. namespace internal {
  9. template <typename Char>
  10. bool DateParser::Parse(Vector<Char> str,
  11. FixedArray* out,
  12. UnicodeCache* unicode_cache) {
  13. ASSERT(out->length() >= OUTPUT_SIZE);
  14. InputReader<Char> in(unicode_cache, str);
  15. DateStringTokenizer<Char> scanner(&in);
  16. TimeZoneComposer tz;
  17. TimeComposer time;
  18. DayComposer day;
  19. // Specification:
  20. // Accept ES5 ISO 8601 date-time-strings or legacy dates compatible
  21. // with Safari.
  22. // ES5 ISO 8601 dates:
  23. // [('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]]
  24. // where yyyy is in the range 0000..9999 and
  25. // +/-yyyyyy is in the range -999999..+999999 -
  26. // but -000000 is invalid (year zero must be positive),
  27. // MM is in the range 01..12,
  28. // DD is in the range 01..31,
  29. // MM and DD defaults to 01 if missing,,
  30. // HH is generally in the range 00..23, but can be 24 if mm, ss
  31. // and sss are zero (or missing), representing midnight at the
  32. // end of a day,
  33. // mm and ss are in the range 00..59,
  34. // sss is in the range 000..999,
  35. // hh is in the range 00..23,
  36. // mm, ss, and sss default to 00 if missing, and
  37. // timezone defaults to Z if missing
  38. // (following Safari, ISO actually demands local time).
  39. // Extensions:
  40. // We also allow sss to have more or less than three digits (but at
  41. // least one).
  42. // We allow hh:mm to be specified as hhmm.
  43. // Legacy dates:
  44. // Any unrecognized word before the first number is ignored.
  45. // Parenthesized text is ignored.
  46. // An unsigned number followed by ':' is a time value, and is
  47. // added to the TimeComposer. A number followed by '::' adds a second
  48. // zero as well. A number followed by '.' is also a time and must be
  49. // followed by milliseconds.
  50. // Any other number is a date component and is added to DayComposer.
  51. // A month name (or really: any word having the same first three letters
  52. // as a month name) is recorded as a named month in the Day composer.
  53. // A word recognizable as a time-zone is recorded as such, as is
  54. // '(+|-)(hhmm|hh:)'.
  55. // Legacy dates don't allow extra signs ('+' or '-') or umatched ')'
  56. // after a number has been read (before the first number, any garbage
  57. // is allowed).
  58. // Intersection of the two:
  59. // A string that matches both formats (e.g. 1970-01-01) will be
  60. // parsed as an ES5 date-time string - which means it will default
  61. // to UTC time-zone. That's unavoidable if following the ES5
  62. // specification.
  63. // After a valid "T" has been read while scanning an ES5 datetime string,
  64. // the input can no longer be a valid legacy date, since the "T" is a
  65. // garbage string after a number has been read.
  66. // First try getting as far as possible with as ES5 Date Time String.
  67. DateToken next_unhandled_token = ParseES5DateTime(&scanner, &day, &time, &tz);
  68. if (next_unhandled_token.IsInvalid()) return false;
  69. bool has_read_number = !day.IsEmpty();
  70. // If there's anything left, continue with the legacy parser.
  71. for (DateToken token = next_unhandled_token;
  72. !token.IsEndOfInput();
  73. token = scanner.Next()) {
  74. if (token.IsNumber()) {
  75. has_read_number = true;
  76. int n = token.number();
  77. if (scanner.SkipSymbol(':')) {
  78. if (scanner.SkipSymbol(':')) {
  79. // n + "::"
  80. if (!time.IsEmpty()) return false;
  81. time.Add(n);
  82. time.Add(0);
  83. } else {
  84. // n + ":"
  85. if (!time.Add(n)) return false;
  86. if (scanner.Peek().IsSymbol('.')) scanner.Next();
  87. }
  88. } else if (scanner.SkipSymbol('.') && time.IsExpecting(n)) {
  89. time.Add(n);
  90. if (!scanner.Peek().IsNumber()) return false;
  91. int n = ReadMilliseconds(scanner.Next());
  92. if (n < 0) return false;
  93. time.AddFinal(n);
  94. } else if (tz.IsExpecting(n)) {
  95. tz.SetAbsoluteMinute(n);
  96. } else if (time.IsExpecting(n)) {
  97. time.AddFinal(n);
  98. // Require end, white space, "Z", "+" or "-" immediately after
  99. // finalizing time.
  100. DateToken peek = scanner.Peek();
  101. if (!peek.IsEndOfInput() &&
  102. !peek.IsWhiteSpace() &&
  103. !peek.IsKeywordZ() &&
  104. !peek.IsAsciiSign()) return false;
  105. } else {
  106. if (!day.Add(n)) return false;
  107. scanner.SkipSymbol('-');
  108. }
  109. } else if (token.IsKeyword()) {
  110. // Parse a "word" (sequence of chars. >= 'A').
  111. KeywordType type = token.keyword_type();
  112. int value = token.keyword_value();
  113. if (type == AM_PM && !time.IsEmpty()) {
  114. time.SetHourOffset(value);
  115. } else if (type == MONTH_NAME) {
  116. day.SetNamedMonth(value);
  117. scanner.SkipSymbol('-');
  118. } else if (type == TIME_ZONE_NAME && has_read_number) {
  119. tz.Set(value);
  120. } else {
  121. // Garbage words are illegal if a number has been read.
  122. if (has_read_number) return false;
  123. // The first number has to be separated from garbage words by
  124. // whitespace or other separators.
  125. if (scanner.Peek().IsNumber()) return false;
  126. }
  127. } else if (token.IsAsciiSign() && (tz.IsUTC() || !time.IsEmpty())) {
  128. // Parse UTC offset (only after UTC or time).
  129. tz.SetSign(token.ascii_sign());
  130. // The following number may be empty.
  131. int n = 0;
  132. if (scanner.Peek().IsNumber()) {
  133. n = scanner.Next().number();
  134. }
  135. has_read_number = true;
  136. if (scanner.Peek().IsSymbol(':')) {
  137. tz.SetAbsoluteHour(n);
  138. tz.SetAbsoluteMinute(kNone);
  139. } else {
  140. tz.SetAbsoluteHour(n / 100);
  141. tz.SetAbsoluteMinute(n % 100);
  142. }
  143. } else if ((token.IsAsciiSign() || token.IsSymbol(')')) &&
  144. has_read_number) {
  145. // Extra sign or ')' is illegal if a number has been read.
  146. return false;
  147. } else {
  148. // Ignore other characters and whitespace.
  149. }
  150. }
  151. return day.Write(out) && time.Write(out) && tz.Write(out);
  152. }
  153. template<typename CharType>
  154. DateParser::DateToken DateParser::DateStringTokenizer<CharType>::Scan() {
  155. int pre_pos = in_->position();
  156. if (in_->IsEnd()) return DateToken::EndOfInput();
  157. if (in_->IsAsciiDigit()) {
  158. int n = in_->ReadUnsignedNumeral();
  159. int length = in_->position() - pre_pos;
  160. return DateToken::Number(n, length);
  161. }
  162. if (in_->Skip(':')) return DateToken::Symbol(':');
  163. if (in_->Skip('-')) return DateToken::Symbol('-');
  164. if (in_->Skip('+')) return DateToken::Symbol('+');
  165. if (in_->Skip('.')) return DateToken::Symbol('.');
  166. if (in_->Skip(')')) return DateToken::Symbol(')');
  167. if (in_->IsAsciiAlphaOrAbove()) {
  168. ASSERT(KeywordTable::kPrefixLength == 3);
  169. uint32_t buffer[3] = {0, 0, 0};
  170. int length = in_->ReadWord(buffer, 3);
  171. int index = KeywordTable::Lookup(buffer, length);
  172. return DateToken::Keyword(KeywordTable::GetType(index),
  173. KeywordTable::GetValue(index),
  174. length);
  175. }
  176. if (in_->SkipWhiteSpace()) {
  177. return DateToken::WhiteSpace(in_->position() - pre_pos);
  178. }
  179. if (in_->SkipParentheses()) {
  180. return DateToken::Unknown();
  181. }
  182. in_->Next();
  183. return DateToken::Unknown();
  184. }
  185. template <typename Char>
  186. DateParser::DateToken DateParser::ParseES5DateTime(
  187. DateStringTokenizer<Char>* scanner,
  188. DayComposer* day,
  189. TimeComposer* time,
  190. TimeZoneComposer* tz) {
  191. ASSERT(day->IsEmpty());
  192. ASSERT(time->IsEmpty());
  193. ASSERT(tz->IsEmpty());
  194. // Parse mandatory date string: [('-'|'+')yy]yyyy[':'MM[':'DD]]
  195. if (scanner->Peek().IsAsciiSign()) {
  196. // Keep the sign token, so we can pass it back to the legacy
  197. // parser if we don't use it.
  198. DateToken sign_token = scanner->Next();
  199. if (!scanner->Peek().IsFixedLengthNumber(6)) return sign_token;
  200. int sign = sign_token.ascii_sign();
  201. int year = scanner->Next().number();
  202. if (sign < 0 && year == 0) return sign_token;
  203. day->Add(sign * year);
  204. } else if (scanner->Peek().IsFixedLengthNumber(4)) {
  205. day->Add(scanner->Next().number());
  206. } else {
  207. return scanner->Next();
  208. }
  209. if (scanner->SkipSymbol('-')) {
  210. if (!scanner->Peek().IsFixedLengthNumber(2) ||
  211. !DayComposer::IsMonth(scanner->Peek().number())) return scanner->Next();
  212. day->Add(scanner->Next().number());
  213. if (scanner->SkipSymbol('-')) {
  214. if (!scanner->Peek().IsFixedLengthNumber(2) ||
  215. !DayComposer::IsDay(scanner->Peek().number())) return scanner->Next();
  216. day->Add(scanner->Next().number());
  217. }
  218. }
  219. // Check for optional time string: 'T'HH':'mm[':'ss['.'sss]]Z
  220. if (!scanner->Peek().IsKeywordType(TIME_SEPARATOR)) {
  221. if (!scanner->Peek().IsEndOfInput()) return scanner->Next();
  222. } else {
  223. // ES5 Date Time String time part is present.
  224. scanner->Next();
  225. if (!scanner->Peek().IsFixedLengthNumber(2) ||
  226. !Between(scanner->Peek().number(), 0, 24)) {
  227. return DateToken::Invalid();
  228. }
  229. // Allow 24:00[:00[.000]], but no other time starting with 24.
  230. bool hour_is_24 = (scanner->Peek().number() == 24);
  231. time->Add(scanner->Next().number());
  232. if (!scanner->SkipSymbol(':')) return DateToken::Invalid();
  233. if (!scanner->Peek().IsFixedLengthNumber(2) ||
  234. !TimeComposer::IsMinute(scanner->Peek().number()) ||
  235. (hour_is_24 && scanner->Peek().number() > 0)) {
  236. return DateToken::Invalid();
  237. }
  238. time->Add(scanner->Next().number());
  239. if (scanner->SkipSymbol(':')) {
  240. if (!scanner->Peek().IsFixedLengthNumber(2) ||
  241. !TimeComposer::IsSecond(scanner->Peek().number()) ||
  242. (hour_is_24 && scanner->Peek().number() > 0)) {
  243. return DateToken::Invalid();
  244. }
  245. time->Add(scanner->Next().number());
  246. if (scanner->SkipSymbol('.')) {
  247. if (!scanner->Peek().IsNumber() ||
  248. (hour_is_24 && scanner->Peek().number() > 0)) {
  249. return DateToken::Invalid();
  250. }
  251. // Allow more or less than the mandated three digits.
  252. time->Add(ReadMilliseconds(scanner->Next()));
  253. }
  254. }
  255. // Check for optional timezone designation: 'Z' | ('+'|'-')hh':'mm
  256. if (scanner->Peek().IsKeywordZ()) {
  257. scanner->Next();
  258. tz->Set(0);
  259. } else if (scanner->Peek().IsSymbol('+') ||
  260. scanner->Peek().IsSymbol('-')) {
  261. tz->SetSign(scanner->Next().symbol() == '+' ? 1 : -1);
  262. if (scanner->Peek().IsFixedLengthNumber(4)) {
  263. // hhmm extension syntax.
  264. int hourmin = scanner->Next().number();
  265. int hour = hourmin / 100;
  266. int min = hourmin % 100;
  267. if (!TimeComposer::IsHour(hour) || !TimeComposer::IsMinute(min)) {
  268. return DateToken::Invalid();
  269. }
  270. tz->SetAbsoluteHour(hour);
  271. tz->SetAbsoluteMinute(min);
  272. } else {
  273. // hh:mm standard syntax.
  274. if (!scanner->Peek().IsFixedLengthNumber(2) ||
  275. !TimeComposer::IsHour(scanner->Peek().number())) {
  276. return DateToken::Invalid();
  277. }
  278. tz->SetAbsoluteHour(scanner->Next().number());
  279. if (!scanner->SkipSymbol(':')) return DateToken::Invalid();
  280. if (!scanner->Peek().IsFixedLengthNumber(2) ||
  281. !TimeComposer::IsMinute(scanner->Peek().number())) {
  282. return DateToken::Invalid();
  283. }
  284. tz->SetAbsoluteMinute(scanner->Next().number());
  285. }
  286. }
  287. if (!scanner->Peek().IsEndOfInput()) return DateToken::Invalid();
  288. }
  289. // Successfully parsed ES5 Date Time String. Default to UTC if no TZ given.
  290. if (tz->IsEmpty()) tz->Set(0);
  291. day->set_iso_date();
  292. return DateToken::EndOfInput();
  293. }
  294. } } // namespace v8::internal
  295. #endif // V8_DATEPARSER_INL_H_