dateparser.cc 6.02 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

Yang Guo's avatar
Yang Guo committed
5
#include "src/date/dateparser.h"
6

7
#include "src/objects/objects-inl.h"
8
#include "src/strings/char-predicates-inl.h"
9

10 11
namespace v8 {
namespace internal {
12

13
bool DateParser::DayComposer::Write(double* output) {
14 15 16 17 18
  if (index_ < 1) return false;
  // Day and month defaults to 1.
  while (index_ < kSize) {
    comp_[index_++] = 1;
  }
19

20 21 22 23 24
  int year = 0;  // Default year is 0 (=> 2000) for KJS compatibility.
  int month = kNone;
  int day = kNone;

  if (named_month_ == kNone) {
25
    if (is_iso_date_ || (index_ == 3 && !IsDay(comp_[0]))) {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      // YMD
      year = comp_[0];
      month = comp_[1];
      day = comp_[2];
    } else {
      // MD(Y)
      month = comp_[0];
      day = comp_[1];
      if (index_ == 3) year = comp_[2];
    }
  } else {
    month = named_month_;
    if (index_ == 1) {
      // MD or DM
      day = comp_[0];
    } else if (!IsDay(comp_[0])) {
      // YMD, MYD, or YDM
      year = comp_[0];
      day = comp_[1];
    } else {
      // DMY, MDY, or DYM
      day = comp_[0];
      year = comp_[1];
    }
  }

52
  if (!is_iso_date_) {
Yang Guo's avatar
Yang Guo committed
53 54 55 56
    if (Between(year, 0, 49))
      year += 2000;
    else if (Between(year, 50, 99))
      year += 1900;
57
  }
58 59 60

  if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false;

61 62 63
  output[YEAR] = year;
  output[MONTH] = month - 1;  // 0-based
  output[DAY] = day;
64 65 66
  return true;
}

67
bool DateParser::TimeComposer::Write(double* output) {
68 69 70 71 72 73 74 75
  // All time slots default to 0
  while (index_ < kSize) {
    comp_[index_++] = 0;
  }

  int& hour = comp_[0];
  int& minute = comp_[1];
  int& second = comp_[2];
76
  int& millisecond = comp_[3];
77 78 79 80 81 82 83

  if (hour_offset_ != kNone) {
    if (!IsHour12(hour)) return false;
    hour %= 12;
    hour += hour_offset_;
  }

Yang Guo's avatar
Yang Guo committed
84 85
  if (!IsHour(hour) || !IsMinute(minute) || !IsSecond(second) ||
      !IsMillisecond(millisecond)) {
86 87 88 89 90
    // A 24th hour is allowed if minutes, seconds, and milliseconds are 0
    if (hour != 24 || minute != 0 || second != 0 || millisecond != 0) {
      return false;
    }
  }
91

92 93 94 95
  output[HOUR] = hour;
  output[MINUTE] = minute;
  output[SECOND] = second;
  output[MILLISECOND] = millisecond;
96 97 98
  return true;
}

99
bool DateParser::TimeZoneComposer::Write(double* output) {
100 101 102
  if (sign_ != kNone) {
    if (hour_ == kNone) hour_ = 0;
    if (minute_ == kNone) minute_ = 0;
103 104 105 106 107 108 109 110 111
    // Avoid signed integer overflow (undefined behavior) by doing unsigned
    // arithmetic.
    unsigned total_seconds_unsigned = hour_ * 3600U + minute_ * 60U;
    if (total_seconds_unsigned > Smi::kMaxValue) return false;
    int total_seconds = static_cast<int>(total_seconds_unsigned);
    if (sign_ < 0) {
      total_seconds = -total_seconds;
    }
    DCHECK(Smi::IsValid(total_seconds));
112
    output[UTC_OFFSET] = total_seconds;
113
  } else {
114
    output[UTC_OFFSET] = std::numeric_limits<double>::quiet_NaN();
115 116 117 118
  }
  return true;
}

Yang Guo's avatar
Yang Guo committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
const int8_t
    DateParser::KeywordTable::array[][DateParser::KeywordTable::kEntrySize] = {
        {'j', 'a', 'n', DateParser::MONTH_NAME, 1},
        {'f', 'e', 'b', DateParser::MONTH_NAME, 2},
        {'m', 'a', 'r', DateParser::MONTH_NAME, 3},
        {'a', 'p', 'r', DateParser::MONTH_NAME, 4},
        {'m', 'a', 'y', DateParser::MONTH_NAME, 5},
        {'j', 'u', 'n', DateParser::MONTH_NAME, 6},
        {'j', 'u', 'l', DateParser::MONTH_NAME, 7},
        {'a', 'u', 'g', DateParser::MONTH_NAME, 8},
        {'s', 'e', 'p', DateParser::MONTH_NAME, 9},
        {'o', 'c', 't', DateParser::MONTH_NAME, 10},
        {'n', 'o', 'v', DateParser::MONTH_NAME, 11},
        {'d', 'e', 'c', DateParser::MONTH_NAME, 12},
        {'a', 'm', '\0', DateParser::AM_PM, 0},
        {'p', 'm', '\0', DateParser::AM_PM, 12},
        {'u', 't', '\0', DateParser::TIME_ZONE_NAME, 0},
        {'u', 't', 'c', DateParser::TIME_ZONE_NAME, 0},
        {'z', '\0', '\0', DateParser::TIME_ZONE_NAME, 0},
        {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0},
        {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5},
        {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6},
        {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4},
        {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5},
        {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6},
        {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7},
        {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7},
        {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8},
        {'t', '\0', '\0', DateParser::TIME_SEPARATOR, 0},
        {'\0', '\0', '\0', DateParser::INVALID, 0},
149 150 151 152 153 154 155
};

// We could use perfect hashing here, but this is not a bottleneck.
int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) {
  int i;
  for (i = 0; array[i][kTypeOffset] != INVALID; i++) {
    int j = 0;
Yang Guo's avatar
Yang Guo committed
156
    while (j < kPrefixLength && pre[j] == static_cast<uint32_t>(array[i][j])) {
157 158 159 160 161 162 163 164 165 166 167 168
      j++;
    }
    // Check if we have a match and the length is legal.
    // Word longer than keyword is only allowed for month names.
    if (j == kPrefixLength &&
        (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) {
      return i;
    }
  }
  return i;
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
int DateParser::ReadMilliseconds(DateToken token) {
  // Read first three significant digits of the original numeral,
  // as inferred from the value and the number of digits.
  // I.e., use the number of digits to see if there were
  // leading zeros.
  int number = token.number();
  int length = token.length();
  if (length < 3) {
    // Less than three digits. Multiply to put most significant digit
    // in hundreds position.
    if (length == 1) {
      number *= 100;
    } else if (length == 2) {
      number *= 10;
    }
  } else if (length > 3) {
    if (length > kMaxSignificantDigits) length = kMaxSignificantDigits;
    // More than three digits. Divide by 10^(length - 3) to get three
    // most significant digits.
    int factor = 1;
    do {
190
      DCHECK_LE(factor, 100000000);  // factor won't overflow.
191 192 193 194 195 196 197 198
      factor *= 10;
      length--;
    } while (length > 3);
    number /= factor;
  }
  return number;
}

199 200
}  // namespace internal
}  // namespace v8