Пример #1
0
  /*
   * XXX: okay, this is vile.
   * Mon, 17 Jan 1994 11:14:55 -0500 (EST)
   *
   * In local time
   */
  public long parseLocalDate(String string) throws Exception {
    long time = parseDate(string);

    synchronized (this) {
      setLocalTime(time);
      return getGMTTime();
    }
  }
Пример #2
0
  /** Creates the date from local or GMT. */
  public QDate(TimeZone zone) {
    _timeZone = zone;

    if (zone == _gmtTimeZone) {
      _stdName = _gmtStdName;
      _dstName = _gmtDstName;
    } else if (zone == _localTimeZone) {
      _stdName = _localStdName;
      _dstName = _localDstName;
    } else {
      _stdName = _timeZone.getDisplayName(false, TimeZone.SHORT);
      _dstName = _timeZone.getDisplayName(true, TimeZone.SHORT);
    }

    _calendar = new GregorianCalendar(_timeZone);

    setLocalTime(System.currentTimeMillis());
  }
Пример #3
0
  private long parseISO8601Date(String string, int pos) throws Exception {
    int strlen = string.length();
    int year = 0;

    char ch = string.charAt(pos);

    if ('0' <= ch && ch <= '9') {
      year = scanISOInt(string, pos, strlen, 4);
      pos += 4;
    }

    if (pos < strlen && string.charAt(pos) == '-') pos++;

    int month = 0;
    if (pos < strlen && '0' <= (ch = string.charAt(pos)) && ch <= '9') {
      month = scanISOInt(string, pos, strlen, 2);
      month--;
      pos += 2;
    } else if (ch == 'W') return Long.MAX_VALUE;

    if (pos < strlen && string.charAt(pos) == '-') pos++;

    int day = 0;
    if (pos < strlen && '0' <= (ch = string.charAt(pos)) && ch <= '9') {
      day = scanISOInt(string, pos, strlen, 2);
      day--;
      pos += 2;
    }

    int hour = 0;
    int minute = 0;
    int second = 0;
    int millisecond = 0;
    if (pos < strlen && string.charAt(pos) == 'T') {
      pos++;

      if (pos < strlen && '0' <= (ch = string.charAt(pos)) && ch <= '9') {
        hour = scanISOInt(string, pos, strlen, 2);
        pos += 2;
      }

      // XXX: fractions can technically be used anywhere by using a
      // , or . instead of a :
      // e.g. 14:30,5 == 14:30:30

      if (pos < strlen && string.charAt(pos) == ':') pos++;

      if (pos < strlen && '0' <= (ch = string.charAt(pos)) && ch <= '9') {
        minute = scanISOInt(string, pos, strlen, 2);
        pos += 2;
      }

      if (pos < strlen && string.charAt(pos) == ':') pos++;

      if (pos < strlen && '0' <= (ch = string.charAt(pos)) && ch <= '9') {
        second = scanISOInt(string, pos, strlen, 2);
        pos += 2;
      }

      if (pos < strlen && (string.charAt(pos) == '.' || string.charAt(pos) == ',')) {
        pos++;
        // XXX: fractions can be any strlen, not just 3
        millisecond = scanISOInt(string, pos, strlen, 3);
        pos += 3;
      }
    }

    long timeOfDay = millisecond + 1000 * (second + 60 * (minute + 60 * hour));

    // XXX: gross hack
    if (year <= 1600) day--;

    long time =
        (MS_PER_DAY * (yearToDayOfEpoch(year) + monthToDayOfYear(month, isLeapYear(year)) + day)
            + timeOfDay);

    if (strlen <= pos) {
      setLocalTime(time);
      return _localTimeOfEpoch;
    }

    if (string.charAt(pos) == 'Z') {
      pos++;
    } else if (string.charAt(pos) == '-' || string.charAt(pos) == '+') {
      int sign = -1;
      if (string.charAt(pos) == '-') sign = 1;

      pos++;
      int tzHour = scanISOInt(string, pos, strlen, 2);
      pos += 2;
      int tzMinute = 0;

      if (pos < strlen && string.charAt(pos) == ':') pos++;
      if (pos < strlen && '0' <= (ch = string.charAt(pos)) && ch <= '9') {
        tzMinute = scanISOInt(string, pos, strlen, 2);
        pos += 2;
      }

      time += sign * 1000 * (60 * (tzMinute + 60 * tzHour));
    } else {
      setLocalTime(time);
      return _localTimeOfEpoch;
    }

    pos = skipWhitespace(string, strlen, pos);
    if (pos < strlen) throw new Exception("extra junk at end of ISO date");

    setGMTTime(time);

    return _localTimeOfEpoch;
  }
Пример #4
0
  /*
   * XXX: okay, this is vile.
   * Mon, 17 Jan 1994 11:14:55 -0500 (EST)
   *
   * In GMT time
   */
  public long parseDate(String string) throws Exception {
    try {
      int strlen = string.length();

      if (strlen == 0) return 0;

      int i = skipWhitespace(string, strlen, 0);

      int ch = string.charAt(i);
      if (ch >= '0' && ch <= '9'
          || (ch == 'T'
              && i + 1 < strlen
              && string.charAt(i + 1) >= '0'
              && string.charAt(i + 1) <= '9')) return parseISO8601Date(string, i);

      CharBuffer cb = new CharBuffer();

      i = scan(string, 0, cb, true);
      if (cb.length() == 0 || !Character.isDigit(cb.charAt(0))) i = scan(string, i, cb, true);

      int dayOfMonth = parseInt(cb);
      i = scan(string, i, cb, true);
      String smonth = cb.toString();
      int month;
      for (month = 0; month < 12; month++) {
        if (MONTH_NAMES[(int) month].equalsIgnoreCase(smonth)) break;
      }
      if (month == 12) throw new Exception("Unexpected month: " + month);

      i = scan(string, i, cb, true);

      int year = parseInt(cb);
      if (cb.length() < 3 && year < 50) year += 2000;
      else if (cb.length() < 3 && year < 100) year += 1900;

      i = scan(string, i, cb, false);
      long timeOfDay = parseInt(cb) * 3600000;

      i = scan(string, i, cb, false);
      timeOfDay += parseInt(cb) * 60000;

      i = scan(string, i, cb, false);
      timeOfDay += parseInt(cb) * 1000;

      // XXX: gross hack
      if (year <= 1600) dayOfMonth--;

      long time =
          (MS_PER_DAY
                  * (yearToDayOfEpoch(year)
                      + monthToDayOfYear(month, isLeapYear(year))
                      + dayOfMonth
                      - 1)
              + timeOfDay);

      try {
        i = scan(string, i, cb, false);
        for (int j = 0; j < cb.length(); j++) {
          if ((ch = cb.charAt(j)) == ';' || ch == ' ') cb.setLength(j);
        }

        ch = cb.length() > 0 ? cb.charAt(0) : 0;
        if (ch == '-' || ch == '+' || ch >= '0' && ch <= '9') {
          long zoneOffset;
          zoneOffset = parseInt(cb);
          zoneOffset = 60000 * (60 * (zoneOffset / 100) + zoneOffset % 100);

          time -= zoneOffset;

          setGMTTime(time);
        } else if (cb.equalsIgnoreCase("gmt") || cb.equalsIgnoreCase("utc")) {
          setGMTTime(time);
        } else {
          setLocalTime(time);
        }
      } catch (Exception e) {
        log.log(Level.FINER, e.toString(), e);
      }

      return _localTimeOfEpoch - _zoneOffset;
    } catch (Exception e) {
      log.log(Level.FINER, e.toString(), e);

      return Long.MAX_VALUE;
    }
  }