1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "dateparser.h"
35 bool DateParser::DayComposer::Write(FixedArray* output) {
36 if (index_ < 1) return false;
37 // Day and month defaults to 1.
38 while (index_ < kSize) {
42 int year = 0; // Default year is 0 (=> 2000) for KJS compatibility.
46 if (named_month_ == kNone) {
47 if (is_iso_date_ || (index_ == 3 && !IsDay(comp_[0]))) {
56 if (index_ == 3) year = comp_[2];
63 } else if (!IsDay(comp_[0])) {
75 if (Between(year, 0, 49)) year += 2000;
76 else if (Between(year, 50, 99)) year += 1900;
79 if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false;
81 output->set(YEAR, Smi::FromInt(year));
82 output->set(MONTH, Smi::FromInt(month - 1)); // 0-based
83 output->set(DAY, Smi::FromInt(day));
88 bool DateParser::TimeComposer::Write(FixedArray* output) {
89 // All time slots default to 0
90 while (index_ < kSize) {
95 int& minute = comp_[1];
96 int& second = comp_[2];
97 int& millisecond = comp_[3];
99 if (hour_offset_ != kNone) {
100 if (!IsHour12(hour)) return false;
102 hour += hour_offset_;
105 if (!IsHour(hour) || !IsMinute(minute) ||
106 !IsSecond(second) || !IsMillisecond(millisecond)) return false;
108 output->set(HOUR, Smi::FromInt(hour));
109 output->set(MINUTE, Smi::FromInt(minute));
110 output->set(SECOND, Smi::FromInt(second));
111 output->set(MILLISECOND, Smi::FromInt(millisecond));
115 bool DateParser::TimeZoneComposer::Write(FixedArray* output) {
116 if (sign_ != kNone) {
117 if (hour_ == kNone) hour_ = 0;
118 if (minute_ == kNone) minute_ = 0;
119 int total_seconds = sign_ * (hour_ * 3600 + minute_ * 60);
120 if (!Smi::IsValid(total_seconds)) return false;
121 output->set(UTC_OFFSET, Smi::FromInt(total_seconds));
123 output->set_null(UTC_OFFSET);
128 const int8_t DateParser::KeywordTable::
129 array[][DateParser::KeywordTable::kEntrySize] = {
130 {'j', 'a', 'n', DateParser::MONTH_NAME, 1},
131 {'f', 'e', 'b', DateParser::MONTH_NAME, 2},
132 {'m', 'a', 'r', DateParser::MONTH_NAME, 3},
133 {'a', 'p', 'r', DateParser::MONTH_NAME, 4},
134 {'m', 'a', 'y', DateParser::MONTH_NAME, 5},
135 {'j', 'u', 'n', DateParser::MONTH_NAME, 6},
136 {'j', 'u', 'l', DateParser::MONTH_NAME, 7},
137 {'a', 'u', 'g', DateParser::MONTH_NAME, 8},
138 {'s', 'e', 'p', DateParser::MONTH_NAME, 9},
139 {'o', 'c', 't', DateParser::MONTH_NAME, 10},
140 {'n', 'o', 'v', DateParser::MONTH_NAME, 11},
141 {'d', 'e', 'c', DateParser::MONTH_NAME, 12},
142 {'a', 'm', '\0', DateParser::AM_PM, 0},
143 {'p', 'm', '\0', DateParser::AM_PM, 12},
144 {'u', 't', '\0', DateParser::TIME_ZONE_NAME, 0},
145 {'u', 't', 'c', DateParser::TIME_ZONE_NAME, 0},
146 {'z', '\0', '\0', DateParser::TIME_ZONE_NAME, 0},
147 {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0},
148 {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5},
149 {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6},
150 {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4},
151 {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5},
152 {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6},
153 {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7},
154 {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7},
155 {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8},
156 {'t', '\0', '\0', DateParser::TIME_SEPARATOR, 0},
157 {'\0', '\0', '\0', DateParser::INVALID, 0},
161 // We could use perfect hashing here, but this is not a bottleneck.
162 int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) {
164 for (i = 0; array[i][kTypeOffset] != INVALID; i++) {
166 while (j < kPrefixLength &&
167 pre[j] == static_cast<uint32_t>(array[i][j])) {
170 // Check if we have a match and the length is legal.
171 // Word longer than keyword is only allowed for month names.
172 if (j == kPrefixLength &&
173 (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) {
181 int DateParser::ReadMilliseconds(DateToken token) {
182 // Read first three significant digits of the original numeral,
183 // as inferred from the value and the number of digits.
184 // I.e., use the number of digits to see if there were
186 int number = token.number();
187 int length = token.length();
189 // Less than three digits. Multiply to put most significant digit
190 // in hundreds position.
193 } else if (length == 2) {
196 } else if (length > 3) {
197 if (length > kMaxSignificantDigits) length = kMaxSignificantDigits;
198 // More than three digits. Divide by 10^(length - 3) to get three
199 // most significant digits.
202 ASSERT(factor <= 100000000); // factor won't overflow.
205 } while (length > 3);
212 } } // namespace v8::internal