blob: e68532f6866c5d58350ce9bbfa07478115305252 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
4// met:
5//
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.
15//
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.
27
28#include "v8.h"
29
30#include "dateparser.h"
31
32namespace v8 {
33namespace internal {
34
35bool DateParser::DayComposer::Write(FixedArray* output) {
Steve Block6ded16b2010-05-10 14:33:55 +010036 // Set year to 0 by default.
37 if (index_ < 1) {
38 comp_[index_++] = 1;
39 }
40
41 // Day and month defaults to 1.
42 while (index_ < kSize) {
43 comp_[index_++] = 1;
44 }
45
Steve Blocka7e24c12009-10-30 11:49:00 +000046 int year = 0; // Default year is 0 (=> 2000) for KJS compatibility.
47 int month = kNone;
48 int day = kNone;
49
50 if (named_month_ == kNone) {
51 if (index_ < 2) return false;
52 if (index_ == 3 && !IsDay(comp_[0])) {
53 // YMD
54 year = comp_[0];
55 month = comp_[1];
56 day = comp_[2];
57 } else {
58 // MD(Y)
59 month = comp_[0];
60 day = comp_[1];
61 if (index_ == 3) year = comp_[2];
62 }
63 } else {
64 month = named_month_;
65 if (index_ < 1) return false;
66 if (index_ == 1) {
67 // MD or DM
68 day = comp_[0];
69 } else if (!IsDay(comp_[0])) {
70 // YMD, MYD, or YDM
71 year = comp_[0];
72 day = comp_[1];
73 } else {
74 // DMY, MDY, or DYM
75 day = comp_[0];
76 year = comp_[1];
77 }
78 }
79
80 if (Between(year, 0, 49)) year += 2000;
81 else if (Between(year, 50, 99)) year += 1900;
82
83 if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false;
84
Leon Clarke4515c472010-02-03 11:58:03 +000085 output->set(YEAR, Smi::FromInt(year));
86 output->set(MONTH, Smi::FromInt(month - 1)); // 0-based
87 output->set(DAY, Smi::FromInt(day));
Steve Blocka7e24c12009-10-30 11:49:00 +000088 return true;
89}
90
91
92bool DateParser::TimeComposer::Write(FixedArray* output) {
93 // All time slots default to 0
94 while (index_ < kSize) {
95 comp_[index_++] = 0;
96 }
97
98 int& hour = comp_[0];
99 int& minute = comp_[1];
100 int& second = comp_[2];
Steve Block6ded16b2010-05-10 14:33:55 +0100101 int& millisecond = comp_[3];
Steve Blocka7e24c12009-10-30 11:49:00 +0000102
103 if (hour_offset_ != kNone) {
104 if (!IsHour12(hour)) return false;
105 hour %= 12;
106 hour += hour_offset_;
107 }
108
Steve Block6ded16b2010-05-10 14:33:55 +0100109 if (!IsHour(hour) || !IsMinute(minute) ||
110 !IsSecond(second) || !IsMillisecond(millisecond)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000111
Leon Clarke4515c472010-02-03 11:58:03 +0000112 output->set(HOUR, Smi::FromInt(hour));
113 output->set(MINUTE, Smi::FromInt(minute));
114 output->set(SECOND, Smi::FromInt(second));
Steve Block6ded16b2010-05-10 14:33:55 +0100115 output->set(MILLISECOND, Smi::FromInt(millisecond));
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 return true;
117}
118
119bool DateParser::TimeZoneComposer::Write(FixedArray* output) {
120 if (sign_ != kNone) {
121 if (hour_ == kNone) hour_ = 0;
122 if (minute_ == kNone) minute_ = 0;
123 int total_seconds = sign_ * (hour_ * 3600 + minute_ * 60);
124 if (!Smi::IsValid(total_seconds)) return false;
Leon Clarke4515c472010-02-03 11:58:03 +0000125 output->set(UTC_OFFSET, Smi::FromInt(total_seconds));
Steve Blocka7e24c12009-10-30 11:49:00 +0000126 } else {
Leon Clarke4515c472010-02-03 11:58:03 +0000127 output->set_null(UTC_OFFSET);
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 }
129 return true;
130}
131
132const int8_t DateParser::KeywordTable::
133 array[][DateParser::KeywordTable::kEntrySize] = {
134 {'j', 'a', 'n', DateParser::MONTH_NAME, 1},
135 {'f', 'e', 'b', DateParser::MONTH_NAME, 2},
136 {'m', 'a', 'r', DateParser::MONTH_NAME, 3},
137 {'a', 'p', 'r', DateParser::MONTH_NAME, 4},
138 {'m', 'a', 'y', DateParser::MONTH_NAME, 5},
139 {'j', 'u', 'n', DateParser::MONTH_NAME, 6},
140 {'j', 'u', 'l', DateParser::MONTH_NAME, 7},
141 {'a', 'u', 'g', DateParser::MONTH_NAME, 8},
142 {'s', 'e', 'p', DateParser::MONTH_NAME, 9},
143 {'o', 'c', 't', DateParser::MONTH_NAME, 10},
144 {'n', 'o', 'v', DateParser::MONTH_NAME, 11},
145 {'d', 'e', 'c', DateParser::MONTH_NAME, 12},
146 {'a', 'm', '\0', DateParser::AM_PM, 0},
147 {'p', 'm', '\0', DateParser::AM_PM, 12},
148 {'u', 't', '\0', DateParser::TIME_ZONE_NAME, 0},
149 {'u', 't', 'c', DateParser::TIME_ZONE_NAME, 0},
Steve Block6ded16b2010-05-10 14:33:55 +0100150 {'z', '\0', '\0', DateParser::TIME_ZONE_NAME, 0},
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0},
152 {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5},
153 {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6},
154 {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4},
155 {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5},
156 {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6},
157 {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7},
158 {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7},
159 {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8},
160 {'\0', '\0', '\0', DateParser::INVALID, 0},
161};
162
163
164// We could use perfect hashing here, but this is not a bottleneck.
165int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) {
166 int i;
167 for (i = 0; array[i][kTypeOffset] != INVALID; i++) {
168 int j = 0;
169 while (j < kPrefixLength &&
170 pre[j] == static_cast<uint32_t>(array[i][j])) {
171 j++;
172 }
173 // Check if we have a match and the length is legal.
174 // Word longer than keyword is only allowed for month names.
175 if (j == kPrefixLength &&
176 (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) {
177 return i;
178 }
179 }
180 return i;
181}
182
183
184} } // namespace v8::internal