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, Smi::FromInt(year)); 76 output->set(MONTH, Smi::FromInt(month - 1)); // 0-based 77 output->set(DAY, Smi::FromInt(day)); 78 return true; 79 } 80 81 82 bool DateParser::TimeComposer::Write(FixedArray* output) { 83 // All time slots default to 0 84 while (index_ < kSize) { 85 comp_[index_++] = 0; 86 } 87 88 int& hour = comp_[0]; 89 int& minute = comp_[1]; 90 int& second = comp_[2]; 91 92 if (hour_offset_ != kNone) { 93 if (!IsHour12(hour)) return false; 94 hour %= 12; 95 hour += hour_offset_; 96 } 97 98 if (!IsHour(hour) || !IsMinute(minute) || !IsSecond(second)) return false; 99 100 output->set(HOUR, Smi::FromInt(hour)); 101 output->set(MINUTE, Smi::FromInt(minute)); 102 output->set(SECOND, Smi::FromInt(second)); 103 return true; 104 } 105 106 bool DateParser::TimeZoneComposer::Write(FixedArray* output) { 107 if (sign_ != kNone) { 108 if (hour_ == kNone) hour_ = 0; 109 if (minute_ == kNone) minute_ = 0; 110 int total_seconds = sign_ * (hour_ * 3600 + minute_ * 60); 111 if (!Smi::IsValid(total_seconds)) return false; 112 output->set(UTC_OFFSET, Smi::FromInt(total_seconds)); 113 } else { 114 output->set_null(UTC_OFFSET); 115 } 116 return true; 117 } 118 119 const int8_t DateParser::KeywordTable:: 120 array[][DateParser::KeywordTable::kEntrySize] = { 121 {'j', 'a', 'n', DateParser::MONTH_NAME, 1}, 122 {'f', 'e', 'b', DateParser::MONTH_NAME, 2}, 123 {'m', 'a', 'r', DateParser::MONTH_NAME, 3}, 124 {'a', 'p', 'r', DateParser::MONTH_NAME, 4}, 125 {'m', 'a', 'y', DateParser::MONTH_NAME, 5}, 126 {'j', 'u', 'n', DateParser::MONTH_NAME, 6}, 127 {'j', 'u', 'l', DateParser::MONTH_NAME, 7}, 128 {'a', 'u', 'g', DateParser::MONTH_NAME, 8}, 129 {'s', 'e', 'p', DateParser::MONTH_NAME, 9}, 130 {'o', 'c', 't', DateParser::MONTH_NAME, 10}, 131 {'n', 'o', 'v', DateParser::MONTH_NAME, 11}, 132 {'d', 'e', 'c', DateParser::MONTH_NAME, 12}, 133 {'a', 'm', '\0', DateParser::AM_PM, 0}, 134 {'p', 'm', '\0', DateParser::AM_PM, 12}, 135 {'u', 't', '\0', DateParser::TIME_ZONE_NAME, 0}, 136 {'u', 't', 'c', DateParser::TIME_ZONE_NAME, 0}, 137 {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0}, 138 {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5}, 139 {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6}, 140 {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4}, 141 {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5}, 142 {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6}, 143 {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7}, 144 {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7}, 145 {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8}, 146 {'\0', '\0', '\0', DateParser::INVALID, 0}, 147 }; 148 149 150 // We could use perfect hashing here, but this is not a bottleneck. 151 int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) { 152 int i; 153 for (i = 0; array[i][kTypeOffset] != INVALID; i++) { 154 int j = 0; 155 while (j < kPrefixLength && 156 pre[j] == static_cast<uint32_t>(array[i][j])) { 157 j++; 158 } 159 // Check if we have a match and the length is legal. 160 // Word longer than keyword is only allowed for month names. 161 if (j == kPrefixLength && 162 (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) { 163 return i; 164 } 165 } 166 return i; 167 } 168 169 170 } } // namespace v8::internal 171