ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 32 | namespace v8 { |
| 33 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | bool DateParser::DayComposer::Write(FixedArray* output) { |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 36 | if (index_ < 1) return false; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 37 | // Day and month defaults to 1. |
| 38 | while (index_ < kSize) { |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 39 | comp_[index_++] = 1; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 40 | } |
| 41 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | 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) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 47 | if (is_iso_date_ || (index_ == 3 && !IsDay(comp_[0]))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | 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 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 74 | if (!is_iso_date_) { |
| 75 | if (Between(year, 0, 49)) year += 2000; |
| 76 | else if (Between(year, 50, 99)) year += 1900; |
| 77 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | |
| 79 | if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false; |
| 80 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 81 | output->set(YEAR, Smi::FromInt(year)); |
| 82 | output->set(MONTH, Smi::FromInt(month - 1)); // 0-based |
| 83 | output->set(DAY, Smi::FromInt(day)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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]; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 97 | int& millisecond = comp_[3]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | |
| 99 | if (hour_offset_ != kNone) { |
| 100 | if (!IsHour12(hour)) return false; |
| 101 | hour %= 12; |
| 102 | hour += hour_offset_; |
| 103 | } |
| 104 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 105 | if (!IsHour(hour) || !IsMinute(minute) || |
| 106 | !IsSecond(second) || !IsMillisecond(millisecond)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 108 | output->set(HOUR, Smi::FromInt(hour)); |
| 109 | output->set(MINUTE, Smi::FromInt(minute)); |
| 110 | output->set(SECOND, Smi::FromInt(second)); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 111 | output->set(MILLISECOND, Smi::FromInt(millisecond)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 115 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | bool DateParser::TimeZoneComposer::Write(FixedArray* output) { |
| 117 | if (sign_ != kNone) { |
| 118 | if (hour_ == kNone) hour_ = 0; |
| 119 | if (minute_ == kNone) minute_ = 0; |
| 120 | int total_seconds = sign_ * (hour_ * 3600 + minute_ * 60); |
| 121 | if (!Smi::IsValid(total_seconds)) return false; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 122 | output->set(UTC_OFFSET, Smi::FromInt(total_seconds)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 124 | output->set_null(UTC_OFFSET); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 129 | const int8_t DateParser::KeywordTable:: |
| 130 | array[][DateParser::KeywordTable::kEntrySize] = { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | {'j', 'a', 'n', DateParser::MONTH_NAME, 1}, |
| 132 | {'f', 'e', 'b', DateParser::MONTH_NAME, 2}, |
| 133 | {'m', 'a', 'r', DateParser::MONTH_NAME, 3}, |
| 134 | {'a', 'p', 'r', DateParser::MONTH_NAME, 4}, |
| 135 | {'m', 'a', 'y', DateParser::MONTH_NAME, 5}, |
| 136 | {'j', 'u', 'n', DateParser::MONTH_NAME, 6}, |
| 137 | {'j', 'u', 'l', DateParser::MONTH_NAME, 7}, |
| 138 | {'a', 'u', 'g', DateParser::MONTH_NAME, 8}, |
| 139 | {'s', 'e', 'p', DateParser::MONTH_NAME, 9}, |
| 140 | {'o', 'c', 't', DateParser::MONTH_NAME, 10}, |
| 141 | {'n', 'o', 'v', DateParser::MONTH_NAME, 11}, |
| 142 | {'d', 'e', 'c', DateParser::MONTH_NAME, 12}, |
| 143 | {'a', 'm', '\0', DateParser::AM_PM, 0}, |
| 144 | {'p', 'm', '\0', DateParser::AM_PM, 12}, |
| 145 | {'u', 't', '\0', DateParser::TIME_ZONE_NAME, 0}, |
| 146 | {'u', 't', 'c', DateParser::TIME_ZONE_NAME, 0}, |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 147 | {'z', '\0', '\0', DateParser::TIME_ZONE_NAME, 0}, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0}, |
| 149 | {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5}, |
| 150 | {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6}, |
| 151 | {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4}, |
| 152 | {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5}, |
| 153 | {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6}, |
| 154 | {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7}, |
| 155 | {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7}, |
| 156 | {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8}, |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 157 | {'t', '\0', '\0', DateParser::TIME_SEPARATOR, 0}, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | {'\0', '\0', '\0', DateParser::INVALID, 0}, |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | // We could use perfect hashing here, but this is not a bottleneck. |
| 163 | int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) { |
| 164 | int i; |
| 165 | for (i = 0; array[i][kTypeOffset] != INVALID; i++) { |
| 166 | int j = 0; |
| 167 | while (j < kPrefixLength && |
| 168 | pre[j] == static_cast<uint32_t>(array[i][j])) { |
| 169 | j++; |
| 170 | } |
| 171 | // Check if we have a match and the length is legal. |
| 172 | // Word longer than keyword is only allowed for month names. |
| 173 | if (j == kPrefixLength && |
| 174 | (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) { |
| 175 | return i; |
| 176 | } |
| 177 | } |
| 178 | return i; |
| 179 | } |
| 180 | |
| 181 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 182 | int DateParser::ReadMilliseconds(DateToken token) { |
| 183 | // Read first three significant digits of the original numeral, |
| 184 | // as inferred from the value and the number of digits. |
| 185 | // I.e., use the number of digits to see if there were |
| 186 | // leading zeros. |
| 187 | int number = token.number(); |
| 188 | int length = token.length(); |
| 189 | if (length < 3) { |
| 190 | // Less than three digits. Multiply to put most significant digit |
| 191 | // in hundreds position. |
| 192 | if (length == 1) { |
| 193 | number *= 100; |
| 194 | } else if (length == 2) { |
| 195 | number *= 10; |
| 196 | } |
| 197 | } else if (length > 3) { |
| 198 | if (length > kMaxSignificantDigits) length = kMaxSignificantDigits; |
| 199 | // More than three digits. Divide by 10^(length - 3) to get three |
| 200 | // most significant digits. |
| 201 | int factor = 1; |
| 202 | do { |
| 203 | ASSERT(factor <= 100000000); // factor won't overflow. |
| 204 | factor *= 10; |
| 205 | length--; |
| 206 | } while (length > 3); |
| 207 | number /= factor; |
| 208 | } |
| 209 | return number; |
| 210 | } |
| 211 | |
| 212 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 213 | } } // namespace v8::internal |