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.
7 #include "src/dateparser.h"
12 bool DateParser::DayComposer::Write(FixedArray* output) {
13 if (index_ < 1) return false;
14 // Day and month defaults to 1.
15 while (index_ < kSize) {
19 int year = 0; // Default year is 0 (=> 2000) for KJS compatibility.
23 if (named_month_ == kNone) {
24 if (is_iso_date_ || (index_ == 3 && !IsDay(comp_[0]))) {
33 if (index_ == 3) year = comp_[2];
40 } else if (!IsDay(comp_[0])) {
52 if (Between(year, 0, 49)) year += 2000;
53 else if (Between(year, 50, 99)) year += 1900;
56 if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false;
58 output->set(YEAR, Smi::FromInt(year));
59 output->set(MONTH, Smi::FromInt(month - 1)); // 0-based
60 output->set(DAY, Smi::FromInt(day));
65 bool DateParser::TimeComposer::Write(FixedArray* output) {
66 // All time slots default to 0
67 while (index_ < kSize) {
72 int& minute = comp_[1];
73 int& second = comp_[2];
74 int& millisecond = comp_[3];
76 if (hour_offset_ != kNone) {
77 if (!IsHour12(hour)) return false;
82 if (!IsHour(hour) || !IsMinute(minute) ||
83 !IsSecond(second) || !IsMillisecond(millisecond)) return false;
85 output->set(HOUR, Smi::FromInt(hour));
86 output->set(MINUTE, Smi::FromInt(minute));
87 output->set(SECOND, Smi::FromInt(second));
88 output->set(MILLISECOND, Smi::FromInt(millisecond));
93 bool DateParser::TimeZoneComposer::Write(FixedArray* output) {
95 if (hour_ == kNone) hour_ = 0;
96 if (minute_ == kNone) minute_ = 0;
97 int total_seconds = sign_ * (hour_ * 3600 + minute_ * 60);
98 if (!Smi::IsValid(total_seconds)) return false;
99 output->set(UTC_OFFSET, Smi::FromInt(total_seconds));
101 output->set_null(UTC_OFFSET);
106 const int8_t DateParser::KeywordTable::
107 array[][DateParser::KeywordTable::kEntrySize] = {
108 {'j', 'a', 'n', DateParser::MONTH_NAME, 1},
109 {'f', 'e', 'b', DateParser::MONTH_NAME, 2},
110 {'m', 'a', 'r', DateParser::MONTH_NAME, 3},
111 {'a', 'p', 'r', DateParser::MONTH_NAME, 4},
112 {'m', 'a', 'y', DateParser::MONTH_NAME, 5},
113 {'j', 'u', 'n', DateParser::MONTH_NAME, 6},
114 {'j', 'u', 'l', DateParser::MONTH_NAME, 7},
115 {'a', 'u', 'g', DateParser::MONTH_NAME, 8},
116 {'s', 'e', 'p', DateParser::MONTH_NAME, 9},
117 {'o', 'c', 't', DateParser::MONTH_NAME, 10},
118 {'n', 'o', 'v', DateParser::MONTH_NAME, 11},
119 {'d', 'e', 'c', DateParser::MONTH_NAME, 12},
120 {'a', 'm', '\0', DateParser::AM_PM, 0},
121 {'p', 'm', '\0', DateParser::AM_PM, 12},
122 {'u', 't', '\0', DateParser::TIME_ZONE_NAME, 0},
123 {'u', 't', 'c', DateParser::TIME_ZONE_NAME, 0},
124 {'z', '\0', '\0', DateParser::TIME_ZONE_NAME, 0},
125 {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0},
126 {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5},
127 {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6},
128 {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4},
129 {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5},
130 {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6},
131 {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7},
132 {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7},
133 {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8},
134 {'t', '\0', '\0', DateParser::TIME_SEPARATOR, 0},
135 {'\0', '\0', '\0', DateParser::INVALID, 0},
139 // We could use perfect hashing here, but this is not a bottleneck.
140 int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) {
142 for (i = 0; array[i][kTypeOffset] != INVALID; i++) {
144 while (j < kPrefixLength &&
145 pre[j] == static_cast<uint32_t>(array[i][j])) {
148 // Check if we have a match and the length is legal.
149 // Word longer than keyword is only allowed for month names.
150 if (j == kPrefixLength &&
151 (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) {
159 int DateParser::ReadMilliseconds(DateToken token) {
160 // Read first three significant digits of the original numeral,
161 // as inferred from the value and the number of digits.
162 // I.e., use the number of digits to see if there were
164 int number = token.number();
165 int length = token.length();
167 // Less than three digits. Multiply to put most significant digit
168 // in hundreds position.
171 } else if (length == 2) {
174 } else if (length > 3) {
175 if (length > kMaxSignificantDigits) length = kMaxSignificantDigits;
176 // More than three digits. Divide by 10^(length - 3) to get three
177 // most significant digits.
180 DCHECK(factor <= 100000000); // factor won't overflow.
183 } while (length > 3);
190 } // namespace internal