Upgrade to v8 1.2.7
[platform/upstream/nodejs.git] / deps / v8 / src / dateparser.cc
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
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
32 namespace v8 {
33 namespace internal {
34
35 bool DateParser::DayComposer::Write(FixedArray* output) {
36   int year = 0;  // Default year is 0 (=> 2000) for KJS compatibility.
37   int month = kNone;
38   int day = kNone;
39
40   if (named_month_ == kNone) {
41     if (index_ < 2) return false;
42     if (index_ == 3 && !IsDay(comp_[0])) {
43       // YMD
44       year = comp_[0];
45       month = comp_[1];
46       day = comp_[2];
47     } else {
48       // MD(Y)
49       month = comp_[0];
50       day = comp_[1];
51       if (index_ == 3) year = comp_[2];
52     }
53   } else {
54     month = named_month_;
55     if (index_ < 1) return false;
56     if (index_ == 1) {
57       // MD or DM
58       day = comp_[0];
59     } else if (!IsDay(comp_[0])) {
60       // YMD, MYD, or YDM
61       year = comp_[0];
62       day = comp_[1];
63     } else {
64       // DMY, MDY, or DYM
65       day = comp_[0];
66       year = comp_[1];
67     }
68   }
69
70   if (Between(year, 0, 49)) year += 2000;
71   else if (Between(year, 50, 99)) year += 1900;
72
73   if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false;
74
75   output->set(YEAR,
76               Smi::FromInt(year),
77               SKIP_WRITE_BARRIER);
78   output->set(MONTH,
79               Smi::FromInt(month - 1),
80               SKIP_WRITE_BARRIER);  // 0-based
81   output->set(DAY,
82               Smi::FromInt(day),
83               SKIP_WRITE_BARRIER);
84   return true;
85 }
86
87
88 bool DateParser::TimeComposer::Write(FixedArray* output) {
89   // All time slots default to 0
90   while (index_ < kSize) {
91     comp_[index_++] = 0;
92   }
93
94   int& hour = comp_[0];
95   int& minute = comp_[1];
96   int& second = comp_[2];
97
98   if (hour_offset_ != kNone) {
99     if (!IsHour12(hour)) return false;
100     hour %= 12;
101     hour += hour_offset_;
102   }
103
104   if (!IsHour(hour) || !IsMinute(minute) || !IsSecond(second)) return false;
105
106   output->set(HOUR,
107               Smi::FromInt(hour),
108               SKIP_WRITE_BARRIER);
109   output->set(MINUTE,
110               Smi::FromInt(minute),
111               SKIP_WRITE_BARRIER);
112   output->set(SECOND,
113               Smi::FromInt(second),
114               SKIP_WRITE_BARRIER);
115   return true;
116 }
117
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),
126                 SKIP_WRITE_BARRIER);
127   } else {
128     output->set(UTC_OFFSET,
129                 Heap::null_value(),
130                 SKIP_WRITE_BARRIER);
131   }
132   return true;
133 }
134
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},
163 };
164
165
166 // We could use perfect hashing here, but this is not a bottleneck.
167 int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) {
168   int i;
169   for (i = 0; array[i][kTypeOffset] != INVALID; i++) {
170     int j = 0;
171     while (j < kPrefixLength &&
172            pre[j] == static_cast<uint32_t>(array[i][j])) {
173       j++;
174     }
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)) {
179       return i;
180     }
181   }
182   return i;
183 }
184
185
186 } }  // namespace v8::internal