001    
002    package railo.runtime.type.dt;
003    
004    import java.text.SimpleDateFormat;
005    import java.util.Locale;
006    
007    import railo.commons.date.DateTimeUtil;
008    import railo.runtime.PageContext;
009    import railo.runtime.dump.DumpData;
010    import railo.runtime.dump.DumpProperties;
011    import railo.runtime.dump.DumpTable;
012    import railo.runtime.dump.SimpleDumpData;
013    import railo.runtime.engine.ThreadLocalPageContext;
014    import railo.runtime.exp.ExpressionException;
015    import railo.runtime.exp.PageException;
016    import railo.runtime.op.Operator;
017    import railo.runtime.type.SimpleValue;
018    
019    
020    
021    /**
022     * Printable and Castable Time Object (at the moment, same as DateTime)
023     */
024    public final class TimeImpl extends Time implements SimpleValue {
025        
026            private static SimpleDateFormat railoFormatter= new SimpleDateFormat("HH:mm:ss",Locale.US);
027    
028            //private TimeZone timezone;
029            public TimeImpl(long utcTime) {
030                    this(null,utcTime,false); 
031            }
032    
033            public TimeImpl(boolean addOffset) { 
034                    this(null,System.currentTimeMillis(),addOffset); 
035            }
036            
037            public TimeImpl(long utcTime, boolean addOffset) {
038                    this(null,utcTime,addOffset); 
039            }
040    
041            public TimeImpl(PageContext pc, boolean addOffset) { 
042                    this(pc,System.currentTimeMillis(),addOffset); 
043            }
044    
045            public TimeImpl(PageContext pc, long utcTime, boolean addOffset) { 
046                    super(addOffset?DateTimeImpl.addOffset(ThreadLocalPageContext.getConfig(pc), utcTime):utcTime);  
047            }
048            
049            public TimeImpl(java.util.Date date) {
050                    this(date.getTime(),false);
051            }
052            
053    
054        @Override
055            public String castToString() {
056                    synchronized (railoFormatter) {
057                    railoFormatter.setTimeZone(ThreadLocalPageContext.getTimeZone());
058                return "{t '"+railoFormatter.format(this)+"'}";
059            }
060            }
061    
062            @Override
063            public String castToString(String defaultValue) {
064                    synchronized (railoFormatter) {
065                    railoFormatter.setTimeZone(ThreadLocalPageContext.getTimeZone());
066                return "{t '"+railoFormatter.format(this)+"'}";
067            }
068            }
069    
070    
071            @Override
072            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
073                    String str=castToString("");
074            DumpTable table=new DumpTable("date","#ff9900","#ffcc00","#000000");
075            table.appendRow(1, new SimpleDumpData("Time"), new SimpleDumpData(str));
076            return table;
077        }
078        
079        @Override
080        public boolean castToBooleanValue() throws ExpressionException {
081            return DateTimeUtil.getInstance().toBooleanValue(this);
082        }
083        
084        @Override
085        public Boolean castToBoolean(Boolean defaultValue) {
086            return defaultValue;
087        }
088    
089        @Override
090        public double castToDoubleValue() {
091            return toDoubleValue();
092        }
093        
094        @Override
095        public double castToDoubleValue(double defaultValue) {
096            return toDoubleValue();
097        }
098    
099        @Override
100        public DateTime castToDateTime() {
101            return this;
102        }
103        
104        @Override
105        public DateTime castToDateTime(DateTime defaultValue) {
106            return this;
107        }
108        
109        @Override
110        public double toDoubleValue() {
111            return DateTimeUtil.getInstance().toDoubleValue(this);
112        }
113    
114    
115            @Override
116            public int compareTo(boolean b) {
117                    return Operator.compare(castToDoubleValue(), b?1D:0D);
118            }
119    
120            @Override
121            public int compareTo(DateTime dt) throws PageException {
122                    return Operator.compare((java.util.Date)this, (java.util.Date)dt);
123            }
124    
125            @Override
126            public int compareTo(double d) throws PageException {
127                    return Operator.compare(castToDoubleValue(), d);
128            }
129    
130            @Override
131            public int compareTo(String str) throws PageException {
132                    return Operator.compare(castToString(), str);
133            }
134    }