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