blob: 6d8048876fb6d6ead2ccd3f7a33a76f7bf214bc1 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000032namespace v8 {
33namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035bool DateParser::DayComposer::Write(FixedArray* output) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000036 if (index_ < 1) return false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000037 // Day and month defaults to 1.
38 while (index_ < kSize) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000039 comp_[index_++] = 1;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000040 }
41
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042 int year = 0; // Default year is 0 (=> 2000) for KJS compatibility.
43 int month = kNone;
44 int day = kNone;
45
46 if (named_month_ == kNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047 if (index_ == 3 && !IsDay(comp_[0])) {
48 // YMD
49 year = comp_[0];
50 month = comp_[1];
51 day = comp_[2];
52 } else {
53 // MD(Y)
54 month = comp_[0];
55 day = comp_[1];
56 if (index_ == 3) year = comp_[2];
57 }
58 } else {
59 month = named_month_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060 if (index_ == 1) {
61 // MD or DM
62 day = comp_[0];
63 } else if (!IsDay(comp_[0])) {
64 // YMD, MYD, or YDM
65 year = comp_[0];
66 day = comp_[1];
67 } else {
68 // DMY, MDY, or DYM
69 day = comp_[0];
70 year = comp_[1];
71 }
72 }
73
74 if (Between(year, 0, 49)) year += 2000;
75 else if (Between(year, 50, 99)) year += 1900;
76
77 if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false;
78
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000079 output->set(YEAR, Smi::FromInt(year));
80 output->set(MONTH, Smi::FromInt(month - 1)); // 0-based
81 output->set(DAY, Smi::FromInt(day));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082 return true;
83}
84
85
86bool DateParser::TimeComposer::Write(FixedArray* output) {
87 // All time slots default to 0
88 while (index_ < kSize) {
89 comp_[index_++] = 0;
90 }
91
92 int& hour = comp_[0];
93 int& minute = comp_[1];
94 int& second = comp_[2];
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000095 int& millisecond = comp_[3];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096
97 if (hour_offset_ != kNone) {
98 if (!IsHour12(hour)) return false;
99 hour %= 12;
100 hour += hour_offset_;
101 }
102
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000103 if (!IsHour(hour) || !IsMinute(minute) ||
104 !IsSecond(second) || !IsMillisecond(millisecond)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000106 output->set(HOUR, Smi::FromInt(hour));
107 output->set(MINUTE, Smi::FromInt(minute));
108 output->set(SECOND, Smi::FromInt(second));
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000109 output->set(MILLISECOND, Smi::FromInt(millisecond));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110 return true;
111}
112
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113bool DateParser::TimeZoneComposer::Write(FixedArray* output) {
114 if (sign_ != kNone) {
115 if (hour_ == kNone) hour_ = 0;
116 if (minute_ == kNone) minute_ = 0;
117 int total_seconds = sign_ * (hour_ * 3600 + minute_ * 60);
118 if (!Smi::IsValid(total_seconds)) return false;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000119 output->set(UTC_OFFSET, Smi::FromInt(total_seconds));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000121 output->set_null(UTC_OFFSET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122 }
123 return true;
124}
125
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000126const int8_t DateParser::KeywordTable::
127 array[][DateParser::KeywordTable::kEntrySize] = {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128 {'j', 'a', 'n', DateParser::MONTH_NAME, 1},
129 {'f', 'e', 'b', DateParser::MONTH_NAME, 2},
130 {'m', 'a', 'r', DateParser::MONTH_NAME, 3},
131 {'a', 'p', 'r', DateParser::MONTH_NAME, 4},
132 {'m', 'a', 'y', DateParser::MONTH_NAME, 5},
133 {'j', 'u', 'n', DateParser::MONTH_NAME, 6},
134 {'j', 'u', 'l', DateParser::MONTH_NAME, 7},
135 {'a', 'u', 'g', DateParser::MONTH_NAME, 8},
136 {'s', 'e', 'p', DateParser::MONTH_NAME, 9},
137 {'o', 'c', 't', DateParser::MONTH_NAME, 10},
138 {'n', 'o', 'v', DateParser::MONTH_NAME, 11},
139 {'d', 'e', 'c', DateParser::MONTH_NAME, 12},
140 {'a', 'm', '\0', DateParser::AM_PM, 0},
141 {'p', 'm', '\0', DateParser::AM_PM, 12},
142 {'u', 't', '\0', DateParser::TIME_ZONE_NAME, 0},
143 {'u', 't', 'c', DateParser::TIME_ZONE_NAME, 0},
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000144 {'z', '\0', '\0', DateParser::TIME_ZONE_NAME, 0},
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0},
146 {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5},
147 {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6},
148 {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4},
149 {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5},
150 {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6},
151 {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7},
152 {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7},
153 {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8},
154 {'\0', '\0', '\0', DateParser::INVALID, 0},
155};
156
157
158// We could use perfect hashing here, but this is not a bottleneck.
159int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) {
160 int i;
161 for (i = 0; array[i][kTypeOffset] != INVALID; i++) {
162 int j = 0;
163 while (j < kPrefixLength &&
164 pre[j] == static_cast<uint32_t>(array[i][j])) {
165 j++;
166 }
167 // Check if we have a match and the length is legal.
168 // Word longer than keyword is only allowed for month names.
169 if (j == kPrefixLength &&
170 (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) {
171 return i;
172 }
173 }
174 return i;
175}
176
177
178} } // namespace v8::internal