Search in sources :

Example 6 with DateTimeImpl

use of lucee.runtime.type.dt.DateTimeImpl in project Lucee by lucee.

the class DateCaster method toDateTime.

/**
 * parse a string to a Datetime Object, returns null if can't convert
 * @param locale
 * @param str String representation of a locale Date
 * @param tz
 * @param defaultValue
 * @return datetime object
 */
public static DateTime toDateTime(Locale locale, String str, TimeZone tz, DateTime defaultValue, boolean useCommomDateParserAsWell) {
    str = str.trim();
    tz = ThreadLocalPageContext.getTimeZone(tz);
    DateFormat[] df;
    // get Calendar
    Calendar c = JREDateTimeUtil.getThreadCalendar(locale, tz);
    // datetime
    ParsePosition pp = new ParsePosition(0);
    // dfc[FORMATS_DATE_TIME];
    df = FormatUtil.getDateTimeFormats(locale, tz, false);
    Date d;
    for (int i = 0; i < df.length; i++) {
        pp.setErrorIndex(-1);
        pp.setIndex(0);
        df[i].setTimeZone(tz);
        d = df[i].parse(str, pp);
        if (pp.getIndex() == 0 || d == null || pp.getIndex() < str.length())
            continue;
        optimzeDate(c, tz, d);
        return new DateTimeImpl(c.getTime());
    }
    // date
    df = FormatUtil.getDateFormats(locale, tz, false);
    for (int i = 0; i < df.length; i++) {
        pp.setErrorIndex(-1);
        pp.setIndex(0);
        df[i].setTimeZone(tz);
        d = df[i].parse(str, pp);
        if (pp.getIndex() == 0 || d == null || pp.getIndex() < str.length())
            continue;
        optimzeDate(c, tz, d);
        return new DateTimeImpl(c.getTime());
    }
    // time
    // dfc[FORMATS_TIME];
    df = FormatUtil.getTimeFormats(locale, tz, false);
    for (int i = 0; i < df.length; i++) {
        pp.setErrorIndex(-1);
        pp.setIndex(0);
        df[i].setTimeZone(tz);
        d = df[i].parse(str, pp);
        if (pp.getIndex() == 0 || d == null || pp.getIndex() < str.length())
            continue;
        c.setTimeZone(tz);
        c.setTime(d);
        c.set(Calendar.YEAR, 1899);
        c.set(Calendar.MONTH, 11);
        c.set(Calendar.DAY_OF_MONTH, 30);
        c.setTimeZone(tz);
        return new DateTimeImpl(c.getTime());
    }
    if (useCommomDateParserAsWell)
        return DateCaster.toDateSimple(str, CONVERTING_TYPE_NONE, true, tz, defaultValue);
    return defaultValue;
}
Also used : DateFormat(java.text.DateFormat) Calendar(java.util.Calendar) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 7 with DateTimeImpl

use of lucee.runtime.type.dt.DateTimeImpl in project Lucee by lucee.

the class DateCaster method numberToDate.

/**
 * @param timeZone
 * @param d
 * @param convertingType one of the following values:
 * - CONVERTING_TYPE_NONE: number are not converted at all
 * - CONVERTING_TYPE_YEAR: integers are handled as years
 * - CONVERTING_TYPE_OFFSET: numbers are handled as offset from 1899-12-30 00:00:00 UTC
 * @return
 */
private static DateTime numberToDate(TimeZone timeZone, double d, short convertingType, DateTime defaultValue) {
    if (!Decision.isValid(d))
        return defaultValue;
    if (convertingType == CONVERTING_TYPE_YEAR) {
        int i = (int) d;
        if (i == d) {
            timeZone = ThreadLocalPageContext.getTimeZone(timeZone);
            Calendar c = Calendar.getInstance(timeZone);
            c.set(Calendar.MILLISECOND, 0);
            c.set(i, 0, 1, 0, 0, 0);
            return new DateTimeImpl(c);
        }
    }
    if (convertingType == CONVERTING_TYPE_OFFSET)
        return util.toDateTime(d);
    return defaultValue;
}
Also used : Calendar(java.util.Calendar) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl)

Example 8 with DateTimeImpl

use of lucee.runtime.type.dt.DateTimeImpl in project Lucee by lucee.

the class TaskFileFilter method addQueryRow.

private void addQueryRow(lucee.runtime.type.Query qry, SpoolerTask task) {
    int row = qry.addRow();
    try {
        qry.setAt(KeyConstants._type, row, task.getType());
        qry.setAt(KeyConstants._name, row, task.subject());
        qry.setAt(KeyConstants._detail, row, task.detail());
        qry.setAt(KeyConstants._id, row, task.getId());
        qry.setAt(LAST_EXECUTION, row, new DateTimeImpl(task.lastExecution(), true));
        qry.setAt(NEXT_EXECUTION, row, new DateTimeImpl(task.nextExecution(), true));
        qry.setAt(CLOSED, row, Caster.toBoolean(task.closed()));
        qry.setAt(TRIES, row, Caster.toDouble(task.tries()));
        qry.setAt(TRIES_MAX, row, Caster.toDouble(task.tries()));
        qry.setAt(KeyConstants._exceptions, row, translateTime(task.getExceptions()));
        int triesMax = 0;
        ExecutionPlan[] plans = task.getPlans();
        for (int y = 0; y < plans.length; y++) {
            triesMax += plans[y].getTries();
        }
        qry.setAt(TRIES_MAX, row, Caster.toDouble(triesMax));
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
    }
}
Also used : DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl)

Example 9 with DateTimeImpl

use of lucee.runtime.type.dt.DateTimeImpl in project Lucee by lucee.

the class TaskFileFilter method translateTime.

private Array translateTime(Array exp) {
    exp = (Array) Duplicator.duplicate(exp, true);
    Iterator<Object> it = exp.valueIterator();
    Struct sct;
    while (it.hasNext()) {
        sct = (Struct) it.next();
        sct.setEL(KeyConstants._time, new DateTimeImpl(Caster.toLongValue(sct.get(KeyConstants._time, null), 0), true));
    }
    return exp;
}
Also used : DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) SerializableObject(lucee.commons.lang.SerializableObject) Struct(lucee.runtime.type.Struct)

Example 10 with DateTimeImpl

use of lucee.runtime.type.dt.DateTimeImpl in project Lucee by lucee.

the class Admin method doGetRemoteClientTasks.

private int doGetRemoteClientTasks(lucee.runtime.type.Query qry, SpoolerTask[] tasks, int row) {
    SpoolerTask task;
    for (int i = 0; i < tasks.length; i++) {
        row++;
        task = tasks[i];
        try {
            qry.setAt("type", row, task.getType());
            qry.setAt("name", row, task.subject());
            qry.setAt("detail", row, task.detail());
            qry.setAt("id", row, task.getId());
            qry.setAt("lastExecution", row, new DateTimeImpl(pageContext, task.lastExecution(), true));
            qry.setAt("nextExecution", row, new DateTimeImpl(pageContext, task.nextExecution(), true));
            qry.setAt("closed", row, Caster.toBoolean(task.closed()));
            qry.setAt("tries", row, Caster.toDouble(task.tries()));
            qry.setAt("triesmax", row, Caster.toDouble(task.tries()));
            qry.setAt("exceptions", row, translateTime(task.getExceptions()));
            int triesMax = 0;
            ExecutionPlan[] plans = task.getPlans();
            for (int y = 0; y < plans.length; y++) {
                triesMax += plans[y].getTries();
            }
            qry.setAt("triesmax", row, Caster.toDouble(triesMax));
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
    }
    return row;
}
Also used : ExecutionPlan(lucee.runtime.spooler.ExecutionPlan) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) SpoolerTask(lucee.runtime.spooler.SpoolerTask)

Aggregations

DateTimeImpl (lucee.runtime.type.dt.DateTimeImpl)41 Struct (lucee.runtime.type.Struct)12 StructImpl (lucee.runtime.type.StructImpl)11 Date (java.util.Date)7 PageException (lucee.runtime.exp.PageException)6 Calendar (java.util.Calendar)5 Resource (lucee.commons.io.res.Resource)5 Collection (lucee.runtime.type.Collection)5 QueryImpl (lucee.runtime.type.QueryImpl)5 IOException (java.io.IOException)4 ConfigWebImpl (lucee.runtime.config.ConfigWebImpl)4 TimeZone (java.util.TimeZone)3 PageContextImpl (lucee.runtime.PageContextImpl)3 ExpressionException (lucee.runtime.exp.ExpressionException)3 Array (lucee.runtime.type.Array)3 ArrayImpl (lucee.runtime.type.ArrayImpl)3 DateTime (lucee.runtime.type.dt.DateTime)3 File (java.io.File)2 Method (java.lang.reflect.Method)2 DateFormat (java.text.DateFormat)2