Beispiel #1
0
  /*
   * Scan to whitespace or ':'
   */
  private int scan(String string, int i, CharBuffer cb, boolean dash) throws Exception {
    char ch;

    cb.setLength(0);

    int strlen = string.length();
    for (; i < strlen; i++) {
      if (!Character.isWhitespace(ch = string.charAt(i)) && (ch != ':' && (!dash || ch != '-')))
        break;
    }

    for (; i < strlen; i++) {
      if (!Character.isWhitespace(ch = string.charAt(i)) && (ch != ':' && (!dash || ch != '-')))
        cb.append((char) ch);
      else break;
    }

    if (cb.length() == 0) throw new Exception();

    return i;
  }
Beispiel #2
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;
    }
  }