Example #1
0
  public static synchronized String formatISO8601(long gmtTime) {
    if (_gmtDate == null) _gmtDate = new QDate();

    _gmtDate.setGMTTime(gmtTime);

    return _gmtDate.printISO8601();
  }
Example #2
0
  @Override
  public Object clone() {
    QDate newObj = new QDate(_timeZone);

    newObj.calculateSplit(_localTimeOfEpoch);

    return newObj;
  }
  /**
   * Returns the name of the archived file
   *
   * @param time the archive date
   */
  protected String getFormatName(String format, long time) {
    if (time <= 0) time = CurrentTime.getCurrentTime();

    if (format != null) return QDate.formatLocal(time, format);
    else if (_rolloverCron != null)
      return _rolloverPrefix + "." + QDate.formatLocal(time, "%Y%m%d.%H");
    else if (getRolloverPeriod() % (24 * 3600 * 1000L) == 0)
      return _rolloverPrefix + "." + QDate.formatLocal(time, "%Y%m%d");
    else return _rolloverPrefix + "." + QDate.formatLocal(time, "%Y%m%d.%H");
  }
Example #4
0
  protected CacheEntry getCache() {
    if (_cacheEntry == null) {
      synchronized (_cache) {
        _cacheEntry = _cache.get(getPath());
        if (_cacheEntry == null) {
          _cacheEntry = new CacheEntry();
          _cache.put(getPath(), _cacheEntry);
        }
      }
    }

    long now;

    now = Alarm.getCurrentTime();

    synchronized (_cacheEntry) {
      try {
        if (_cacheEntry.expires > now) return _cacheEntry;

        HttpStreamWrapper stream = (HttpStreamWrapper) openReadImpl();
        stream.setHead(true);
        stream.setSocketTimeout(120000);

        String status = (String) stream.getAttribute("status");
        if (status.equals("200")) {
          String lastModified = (String) stream.getAttribute("last-modified");

          _cacheEntry.lastModified = 0;
          if (lastModified != null) {
            QDate date = QDate.getGlobalDate();
            synchronized (date) {
              _cacheEntry.lastModified = date.parseDate(lastModified);
            }
          }

          String length = (String) stream.getAttribute("content-length");
          _cacheEntry.length = 0;
          if (length != null) {
            _cacheEntry.length = Integer.parseInt(length);
          }
        } else _cacheEntry.lastModified = -1;

        _cacheEntry.expires = now + 5000;

        stream.close();
        return _cacheEntry;
      } catch (Exception e) {
        _cacheEntry.lastModified = -1;
        _cacheEntry.expires = now + 5000;

        return _cacheEntry;
      }
    }
  }
Example #5
0
  /**
   * Returns the type's configured value
   *
   * @param builder the context builder
   * @param node the configuration node
   * @param parent
   */
  @Override
  public Object valueOf(String text) {
    try {
      if (text == null) return null;
      else if ("".equals(text)) return new Date(Alarm.getCurrentTime());

      QDate date = new QDate();
      date.parseDate(text);

      return new Date(date.getGMTTime());
    } catch (Exception e) {
      throw ConfigException.create(e);
    }
  }
  /**
   * Logs a message to the error log.
   *
   * @param log the error log to write the message.
   * @param message the message to write
   * @param e the exception to write
   */
  public void log(
      String message,
      Throwable e,
      HttpServletRequest request,
      HttpServletResponse response,
      ServletContext application)
      throws IOException {
    WriteStream logStream = getLogStream();

    if (logStream == null) return;

    Throwable t = e;
    while (t != null) {
      e = t;

      if (e instanceof ServletException) t = ((ServletException) e).getRootCause();
      else if (e instanceof ExceptionWrapper) t = ((ExceptionWrapper) e).getRootCause();
      else t = null;
    }

    CharBuffer cb = CharBuffer.allocate();

    QDate.formatLocal(cb, CurrentTime.getCurrentTime(), "[%Y/%m/%d %H:%M:%S] ");

    cb.append(message);

    logStream.log(cb.close());

    if (e != null && !(e instanceof CompileException)) logStream.log(e);

    logStream.flush();
  }
Example #7
0
  /** 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;
  }
  /**
   * Logs an error.
   *
   * @param message the error message
   * @param request the servlet request
   * @param response the servlet response
   * @param application the servlet context
   */
  public void log(
      String message,
      HttpServletRequest request,
      HttpServletResponse response,
      ServletContext application)
      throws IOException {
    WriteStream logStream = getLogStream();

    if (logStream == null) return;

    CharBuffer cb = CharBuffer.allocate();

    QDate.formatLocal(cb, CurrentTime.getCurrentTime(), "[%Y/%m/%d %H:%M:%S] ");

    cb.append(message);

    logStream.log(cb.close());

    logStream.flush();
  }
  /**
   * Fills the time buffer with the formatted time.
   *
   * @param date current time in milliseconds
   */
  private void fillTime(long date) throws IOException {
    synchronized (_timeBuffer) {
      if (date / 1000 == _lastTime / 1000) return;

      if (_timeFormatSecondOffset >= 0 && date / 3600000 == _lastTime / 3600000) {
        byte[] bBuf = _timeBuffer.getBuffer();

        int min = (int) (date / 60000 % 60);
        int sec = (int) (date / 1000 % 60);

        bBuf[_timeFormatMinuteOffset + 0] = (byte) ('0' + min / 10);
        bBuf[_timeFormatMinuteOffset + 1] = (byte) ('0' + min % 10);

        bBuf[_timeFormatSecondOffset + 0] = (byte) ('0' + sec / 10);
        bBuf[_timeFormatSecondOffset + 1] = (byte) ('0' + sec % 10);

        _lastTime = date;

        return;
      }

      _timeCharBuffer.clear();
      QDate.formatLocal(_timeCharBuffer, date, _timeFormat);

      if (_timeFormatSecondOffset >= 0) {
        _timeFormatSecondOffset = _timeCharBuffer.lastIndexOf(':') + 1;
        _timeFormatMinuteOffset = _timeFormatSecondOffset - 3;
      }

      char[] cBuf = _timeCharBuffer.getBuffer();
      int length = _timeCharBuffer.getLength();

      _timeBuffer.setLength(length);
      byte[] bBuf = _timeBuffer.getBuffer();

      for (int i = length - 1; i >= 0; i--) bBuf[i] = (byte) cBuf[i];
    }

    _lastTime = date;
  }
Example #10
0
  /**
   * 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);
  }
Example #11
0
  /**
   * 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();
  }
Example #12
0
  /**
   * 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();
  }
Example #13
0
  /**
   * 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();
  }
 /**
  * Evaluates the expression as a string.
  *
  * @param rows the current tuple being evaluated
  * @return the string value
  */
 public String evalString(QueryContext context) throws SQLException {
   return QDate.formatLocal(evalLong(context));
 }
Example #15
0
/** Configuration for a run-at */
public class RunAt {
  static L10N L = new L10N(RunAt.class);
  private static final Logger log = Logger.getLogger(RunAt.class.getName());

  private QDate _cal = QDate.createLocal();

  private long _period = -1;

  private IntArray _hourTimes;
  private IntArray _minuteTimes;

  /** Creates a new servlet configuration object. */
  public RunAt() {}

  /** Adds the text. */
  public void addText(String runAt) throws ConfigException {
    configureRunAt(runAt);
  }

  /** Sets the period */
  public void setPeriod(Period period) throws ConfigException {
    _period = period.getPeriod();
  }

  /** 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;
  }

  /** Configures the run-at time. */
  private void configureRunAt(String string) throws ConfigException {
    int len = string.length();
    char ch = 0;
    int i = 0;
    while (true) {
      for (; i < len && (Character.isWhitespace(ch = string.charAt(i)) || ch == ','); i++) {}

      if (i >= len) return;

      if (!(ch >= '0' && ch <= '9' || ch == ':'))
        throw new ConfigException(
            L.l(
                "illegal run-at time `{0}'.  Run-at values are either hour (0:00, 6:30, 12:15) or minute (:15, :30, :45).",
                string));

      int hour = 0;
      int minute = 0;
      boolean hasHour = false;
      for (; i < len && (ch = string.charAt(i)) >= '0' && ch <= '9'; i++) {
        hasHour = true;
        hour = 10 * hour + ch - '0';
      }

      if (ch == ':') {
        i++;
        for (; i < len && (ch = string.charAt(i)) >= '0' && ch <= '9'; i++)
          minute = 10 * minute + ch - '0';
      }

      if (hasHour) {
        if (_hourTimes == null) _hourTimes = new IntArray();
        _hourTimes.add(60 * hour + minute);
      } else {
        if (_minuteTimes == null) _minuteTimes = new IntArray();
        _minuteTimes.add(minute);
      }
    }
  }
}