1 // Copyright 2008 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 int year = 0; // Default year is 0 (=> 2000) for KJS compatibility.
40 if (named_month_ == kNone) {
41 if (index_ < 2) return false;
42 if (index_ == 3 && !IsDay(comp_[0])) {
51 if (index_ == 3) year = comp_[2];
55 if (index_ < 1) return false;
59 } else if (!IsDay(comp_[0])) {
70 if (Between(year, 0, 49)) year += 2000;
71 else if (Between(year, 50, 99)) year += 1900;
73 if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false;
79 Smi::FromInt(month - 1),
80 SKIP_WRITE_BARRIER); // 0-based
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];
98 if (hour_offset_ != kNone) {
99 if (!IsHour12(hour)) return false;
101 hour += hour_offset_;
104 if (!IsHour(hour) || !IsMinute(minute) || !IsSecond(second)) return false;
110 Smi::FromInt(minute),
113 Smi::FromInt(second),
118 bool DateParser::TimeZoneComposer::Write(FixedArray* output) {
119 if (sign_ != kNone) {
120 if (hour_ == kNone) hour_ = 0;
121 if (minute_ == kNone) minute_ = 0;
122 int total_seconds = sign_ * (hour_ * 3600 + minute_ * 60);
123 if (!Smi::IsValid(total_seconds)) return false;
124 output->set(UTC_OFFSET,
125 Smi::FromInt(total_seconds),
128 output->set(UTC_OFFSET,
135 const int8_t DateParser::KeywordTable::
136 array[][DateParser::KeywordTable::kEntrySize] = {
137 {'j', 'a', 'n', DateParser::MONTH_NAME, 1},
138 {'f', 'e', 'b', DateParser::MONTH_NAME, 2},
139 {'m', 'a', 'r', DateParser::MONTH_NAME, 3},
140 {'a', 'p', 'r', DateParser::MONTH_NAME, 4},
141 {'m', 'a', 'y', DateParser::MONTH_NAME, 5},
142 {'j', 'u', 'n', DateParser::MONTH_NAME, 6},
143 {'j', 'u', 'l', DateParser::MONTH_NAME, 7},
144 {'a', 'u', 'g', DateParser::MONTH_NAME, 8},
145 {'s', 'e', 'p', DateParser::MONTH_NAME, 9},
146 {'o', 'c', 't', DateParser::MONTH_NAME, 10},
147 {'n', 'o', 'v', DateParser::MONTH_NAME, 11},
148 {'d', 'e', 'c', DateParser::MONTH_NAME, 12},
149 {'a', 'm', '\0', DateParser::AM_PM, 0},
150 {'p', 'm', '\0', DateParser::AM_PM, 12},
151 {'u', 't', '\0', DateParser::TIME_ZONE_NAME, 0},
152 {'u', 't', 'c', DateParser::TIME_ZONE_NAME, 0},
153 {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0},
154 {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5},
155 {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6},
156 {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4},
157 {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5},
158 {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6},
159 {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7},
160 {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7},
161 {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8},
162 {'\0', '\0', '\0', DateParser::INVALID, 0},
166 // We could use perfect hashing here, but this is not a bottleneck.
167 int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) {
169 for (i = 0; array[i][kTypeOffset] != INVALID; i++) {
171 while (j < kPrefixLength &&
172 pre[j] == static_cast<uint32_t>(array[i][j])) {
175 // Check if we have a match and the length is legal.
176 // Word longer than keyword is only allowed for month names.
177 if (j == kPrefixLength &&
178 (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) {
186 } } // namespace v8::internal