public static synchronized String formatISO8601(long gmtTime) { if (_gmtDate == null) _gmtDate = new QDate(); _gmtDate.setGMTTime(gmtTime); return _gmtDate.printISO8601(); }
/** Returns the next time. */ public long getNextTimeout(long now) { _cal.setGMTTime(now); long zone = _cal.getZoneOffset(); if (_period > 0) return Period.periodEnd(now + zone, _period) - zone; now = now - now % 60000; long local = now + zone; long dayMinutes = (local / 60000) % (24 * 60); long hourMinutes = dayMinutes % 60; long nextDelta = Long.MAX_VALUE; for (int i = 0; _hourTimes != null && i < _hourTimes.size(); i++) { long time = _hourTimes.get(i); long delta = (time - dayMinutes + 24 * 60) % (24 * 60); if (delta == 0) delta = 24 * 60; if (delta < nextDelta && delta > 0) nextDelta = delta; } for (int i = 0; _minuteTimes != null && i < _minuteTimes.size(); i++) { long time = _minuteTimes.get(i); long delta = (time - hourMinutes + 60) % 60; if (delta == 0) delta = 60; if (delta < nextDelta && delta > 0) nextDelta = delta; } if (nextDelta < Integer.MAX_VALUE) return now + nextDelta * 60000L; else return Long.MAX_VALUE / 2; }
/** * Formats a time in the local time zone. * * @param time in milliseconds, GMT, from the epoch. * @param format formatting string. */ public static synchronized CharBuffer formatLocal(CharBuffer cb, long gmtTime, String format) { _localDate.setGMTTime(gmtTime); return _localDate.format(cb, format); }
/** * Formats a time in the local time zone, using the default format. * * @param time in milliseconds, GMT, from the epoch. */ public static synchronized String formatLocal(long gmtTime) { _localDate.setGMTTime(gmtTime); return _localDate.printDate(); }
/** * Formats a time in the local time zone. * * @param time in milliseconds, GMT, from the epoch. * @param format formatting string. */ public static synchronized String formatLocal(long gmtTime, String format) { _localDate.setGMTTime(gmtTime); return _localDate.format(new CharBuffer(), format).toString(); }
/** * Formats a date, using the default time format. * * @param time the time to format */ public static synchronized String formatGMT(long gmtTime) { _gmtDate.setGMTTime(gmtTime); return _gmtDate.printDate(); }
/** Creates the date for GMT. */ public QDate(long time) { this(_localTimeZone); setGMTTime(time); }
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; }
/* * 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; } }