Search in sources :

Example 1 with TimeImpl

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

the class DateCaster method toTime.

/**
 * converts a String to a Time Object, returns null if invalid string
 * @param str String to convert
 * @param defaultValue
 * @return Time Object
 * @throws
 */
public static Time toTime(TimeZone timeZone, String str, Time defaultValue) {
    if (str == null || str.length() < 3) {
        return defaultValue;
    }
    DateString ds = new DateString(str);
    // Timestamp
    if (ds.isCurrent('{') && ds.isLast('}')) {
        // "^\\{t '([0-9]{1,2}):([0-9]{1,2}):([0-9]{2})'\\}$"
        if (ds.fwIfNext('t')) {
            // Time
            if (!(ds.fwIfNext(' ') && ds.fwIfNext('\'')))
                return defaultValue;
            ds.next();
            // hour
            int hour = ds.readDigits();
            if (hour == -1)
                return defaultValue;
            if (!ds.fwIfCurrent(':'))
                return defaultValue;
            // minute
            int minute = ds.readDigits();
            if (minute == -1)
                return defaultValue;
            if (!ds.fwIfCurrent(':'))
                return defaultValue;
            // second
            int second = ds.readDigits();
            if (second == -1)
                return defaultValue;
            if (!(ds.fwIfCurrent('\'') && ds.fwIfCurrent('}')))
                return defaultValue;
            if (ds.isAfterLast()) {
                long time = util.toTime(timeZone, 1899, 12, 30, hour, minute, second, 0, DEFAULT_VALUE);
                if (time == DEFAULT_VALUE)
                    return defaultValue;
                return new TimeImpl(time, false);
            }
            return defaultValue;
        }
        return defaultValue;
    }
    // Time start with int
    /*else if(ds.isDigit()) {
		    char sec=ds.charAt(1);
		    char third=ds.charAt(2);
		    // 16.10.2004 (02:15)?
			if(sec==':' || third==':') {
				// hour
				int hour=ds.readDigits();
				if(hour==-1) return defaultValue;
				
				if(!ds.fwIfCurrent(':'))return defaultValue;
				
				// minutes
				int minutes=ds.readDigits();
				if(minutes==-1) return defaultValue;
				
				if(ds.isAfterLast()) {
					long time=util.toTime(timeZone,1899,12,30,hour,minutes,0,0,DEFAULT_VALUE);
					if(time==DEFAULT_VALUE) return defaultValue;
					
					return new TimeImpl(time,false);
				}
				//else if(!ds.fwIfCurrent(':'))return null;
				else if(!ds.fwIfCurrent(':')) {
				    if(!ds.fwIfCurrent(' '))return defaultValue;
				    
				    if(ds.fwIfCurrent('a') || ds.fwIfCurrent('A'))	{
				        if(ds.fwIfCurrent('m') || ds.fwIfCurrent('M')) {
				            if(ds.isAfterLast()) {
				            	long time=util.toTime(timeZone,1899,12,30,hour,minutes,0,0,DEFAULT_VALUE);
								if(time==DEFAULT_VALUE) return defaultValue;
				            	return new TimeImpl(time,false);
				            }
				        }
				        return defaultValue;
				    }
				    else if(ds.fwIfCurrent('p') || ds.fwIfCurrent('P'))	{
				        if(ds.fwIfCurrent('m') || ds.fwIfCurrent('M')) {
				            if(ds.isAfterLast()) {
				            	long time=util.toTime(timeZone,1899,12,30,hour<13?hour+12:hour,minutes,0,0,DEFAULT_VALUE);
								if(time==DEFAULT_VALUE) return defaultValue;
								return new TimeImpl(time,false);
				            }
				        }
				        return defaultValue;
				    }
				    return defaultValue;
				}
				
				
				// seconds
				int seconds=ds.readDigits();
				if(seconds==-1) return defaultValue;
				
				if(ds.isAfterLast()) {
					long time=util.toTime(timeZone,1899,12,30,hour,minutes,seconds,0,DEFAULT_VALUE);
					if(time==DEFAULT_VALUE) return defaultValue;
					return new TimeImpl(time,false);
				}
								
		    }
		}*/
    // TODO bessere impl
    ds.reset();
    DateTime rtn = parseTime(timeZone, new int[] { 1899, 12, 30 }, ds, defaultValue, -1);
    if (rtn == defaultValue)
        return defaultValue;
    return new TimeImpl(rtn);
// return defaultValue;
}
Also used : DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) TimeImpl(lucee.runtime.type.dt.TimeImpl) DateTime(lucee.runtime.type.dt.DateTime)

Example 2 with TimeImpl

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

the class DateCaster method _toDateSimpleTS.

private static DateTime _toDateSimpleTS(DateString ds, TimeZone timeZone, DateTime defaultValue) {
    // "^\\{d '([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})'\\}$"
    if (ds.fwIfNext('d')) {
        if (!(ds.fwIfNext(' ') && ds.fwIfNext('\'')))
            return defaultValue;
        ds.next();
        // year
        int year = ds.readDigits();
        if (year == -1)
            return defaultValue;
        if (!ds.fwIfCurrent('-'))
            return defaultValue;
        // month
        int month = ds.readDigits();
        if (month == -1)
            return defaultValue;
        if (!ds.fwIfCurrent('-'))
            return defaultValue;
        // day
        int day = ds.readDigits();
        if (day == -1)
            return defaultValue;
        if (!(ds.fwIfCurrent('\'') && ds.fwIfCurrent('}')))
            return defaultValue;
        // new DateTimeImpl(year,month,day);
        if (ds.isAfterLast())
            return util.toDateTime(timeZone, year, month, day, 0, 0, 0, 0, defaultValue);
        return defaultValue;
    } else // "^\\{ts '([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{2}):([0-9]{1,2}):([0-9]{2})'\\}$"
    if (ds.fwIfNext('t')) {
        if (!(ds.fwIfNext('s') && ds.fwIfNext(' ') && ds.fwIfNext('\''))) {
            // Time
            if (!(ds.fwIfNext(' ') && ds.fwIfNext('\'')))
                return defaultValue;
            ds.next();
            // hour
            int hour = ds.readDigits();
            if (hour == -1)
                return defaultValue;
            if (!ds.fwIfCurrent(':'))
                return defaultValue;
            // minute
            int minute = ds.readDigits();
            if (minute == -1)
                return defaultValue;
            if (!ds.fwIfCurrent(':'))
                return defaultValue;
            // second
            int second = ds.readDigits();
            if (second == -1)
                return defaultValue;
            // Milli Second
            int millis = 0;
            if (ds.fwIfCurrent('.')) {
                millis = ds.readDigits();
            }
            int before = ds.getPos();
            DateTime tmp = parseOffset(ds, timeZone, new int[] { 1899, 12, 30 }, hour, minute, second, millis, false, defaultValue);
            if (tmp == null && before != ds.getPos())
                return defaultValue;
            if (!(ds.fwIfCurrent('\'') && ds.fwIfCurrent('}')))
                return defaultValue;
            if (ds.isAfterLast()) {
                if (tmp != null) {
                    return new TimeImpl(tmp.getTime(), false);
                }
                long time = util.toTime(timeZone, 1899, 12, 30, hour, minute, second, millis, DEFAULT_VALUE);
                if (time == DEFAULT_VALUE)
                    return defaultValue;
                return new TimeImpl(time, false);
            }
            return defaultValue;
        }
        ds.next();
        // year
        int year = ds.readDigits();
        if (year == -1)
            return defaultValue;
        if (!ds.fwIfCurrent('-'))
            return defaultValue;
        // month
        int month = ds.readDigits();
        if (month == -1)
            return defaultValue;
        if (!ds.fwIfCurrent('-'))
            return defaultValue;
        // day
        int day = ds.readDigits();
        if (day == -1)
            return defaultValue;
        if (!ds.fwIfCurrent(' '))
            return defaultValue;
        // hour
        int hour = ds.readDigits();
        if (hour == -1)
            return defaultValue;
        if (!ds.fwIfCurrent(':'))
            return defaultValue;
        // minute
        int minute = ds.readDigits();
        if (minute == -1)
            return defaultValue;
        if (!ds.fwIfCurrent(':'))
            return defaultValue;
        // second
        int second = ds.readDigits();
        if (second == -1)
            return defaultValue;
        // Milli Second
        int millis = 0;
        if (ds.fwIfCurrent('.')) {
            millis = ds.readDigits();
        }
        int before = ds.getPos();
        DateTime tmp = parseOffset(ds, timeZone, new int[] { year, month, day }, hour, minute, second, millis, false, defaultValue);
        if (tmp == null && before != ds.getPos())
            return defaultValue;
        if (!(ds.fwIfCurrent('\'') && ds.fwIfCurrent('}')))
            return defaultValue;
        if (ds.isAfterLast()) {
            if (tmp != null)
                return tmp;
            return util.toDateTime(timeZone, year, month, day, hour, minute, second, millis, defaultValue);
        }
        return defaultValue;
    } else
        return defaultValue;
}
Also used : DateTime(lucee.runtime.type.dt.DateTime) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) TimeImpl(lucee.runtime.type.dt.TimeImpl)

Aggregations

DateTime (lucee.runtime.type.dt.DateTime)2 DateTimeImpl (lucee.runtime.type.dt.DateTimeImpl)2 TimeImpl (lucee.runtime.type.dt.TimeImpl)2