001    package railo.runtime.type.dt;
002    
003    
004    import java.util.Calendar;
005    import java.util.Date;
006    import java.util.TimeZone;
007    
008    import railo.commons.date.DateTimeUtil;
009    import railo.commons.lang.CFTypes;
010    import railo.commons.lang.SizeOf;
011    import railo.runtime.PageContext;
012    import railo.runtime.config.Config;
013    import railo.runtime.dump.DumpData;
014    import railo.runtime.dump.DumpProperties;
015    import railo.runtime.dump.DumpTable;
016    import railo.runtime.dump.SimpleDumpData;
017    import railo.runtime.engine.ThreadLocalPageContext;
018    import railo.runtime.exp.ExpressionException;
019    import railo.runtime.exp.PageException;
020    import railo.runtime.op.Operator;
021    import railo.runtime.reflection.Reflector;
022    import railo.runtime.type.Collection.Key;
023    import railo.runtime.type.Objects;
024    import railo.runtime.type.SimpleValue;
025    import railo.runtime.type.Sizeable;
026    import railo.runtime.type.Struct;
027    import railo.runtime.type.util.MemberUtil;
028    
029    /**
030     * Printable and Castable DateTime Object
031     */
032    public final class DateTimeImpl extends DateTime implements SimpleValue,Sizeable,Objects {
033            
034            public DateTimeImpl(PageContext pc) {
035                    this(pc,System.currentTimeMillis(),true);
036            }
037            
038            public DateTimeImpl(Config config) {
039                    this(config,System.currentTimeMillis(),true);
040            }
041            
042            public DateTimeImpl() {
043                    this(System.currentTimeMillis(),true);
044            }
045            
046            public DateTimeImpl(PageContext pc, long utcTime, boolean doOffset) {
047                    super(doOffset?addOffset(ThreadLocalPageContext.getConfig(pc), utcTime):utcTime);
048            }
049    
050            public DateTimeImpl(Config config, long utcTime, boolean doOffset) {
051                    super(doOffset?addOffset(ThreadLocalPageContext.getConfig(config), utcTime):utcTime);
052            }
053            
054            public DateTimeImpl(long utcTime, boolean doOffset) {
055                    super(doOffset?addOffset(ThreadLocalPageContext.getConfig(), utcTime):utcTime);
056            }
057            
058            /*public DateTimeImpl(Config config, long utcTime) {
059                    super(addOffset(ThreadLocalPageContext.getConfig(config),utcTime));
060            }*/
061    
062            public DateTimeImpl(Date date) {
063                    this(date.getTime(),false);
064            }
065            
066            
067            public DateTimeImpl(Calendar calendar) {
068                    super(calendar.getTimeInMillis());
069                    //this.timezone=ThreadLocalPageContext.getTimeZone(calendar.getTimeZone());
070            }
071    
072            public static long addOffset(Config config, long utcTime) {
073                    if(config!=null) return utcTime+config.getTimeServerOffset();
074                    return utcTime;
075            }
076            
077            
078            @Override
079            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
080                    String str=castToString(pageContext.getTimeZone());
081            DumpTable table=new DumpTable("date","#ff6600","#ffcc99","#000000");
082            if(dp.getMetainfo())
083                    table.appendRow(1, new SimpleDumpData("Date Time ("+pageContext.getTimeZone().getID()+")"));
084            else
085                    table.appendRow(1, new SimpleDumpData("Date Time"));
086            table.appendRow(0, new SimpleDumpData(str));
087            return table;
088            }
089    
090            @Override
091            public String castToString() {
092                    return castToString((TimeZone)null);
093        }
094    
095    
096        @Override
097        public String castToString(String defaultValue) {
098            return castToString((TimeZone)null);
099        }
100        
101            public String castToString(TimeZone tz) {// MUST move to DateTimeUtil
102                    return DateTimeUtil.getInstance().toString(this,tz);
103                    
104            }
105            
106            @Override
107            public boolean castToBooleanValue() throws ExpressionException {
108            return DateTimeUtil.getInstance().toBooleanValue(this);
109            }
110        
111        @Override
112        public Boolean castToBoolean(Boolean defaultValue) {
113            return defaultValue;
114        }
115    
116            @Override
117            public double castToDoubleValue() {
118                return toDoubleValue();
119            }
120        
121        @Override
122        public double castToDoubleValue(double defaultValue) {
123            return toDoubleValue();
124        }
125    
126            @Override
127            public DateTime castToDateTime() {
128                    return this;
129            }
130        
131        @Override
132        public DateTime castToDateTime(DateTime defaultValue) {
133            return this;
134        }
135            
136            @Override
137            public double toDoubleValue() {
138                return DateTimeUtil.getInstance().toDoubleValue(this);
139            }
140    
141    
142            @Override
143            public int compareTo(boolean b) {
144                    return Operator.compare(castToDoubleValue(), b?1D:0D);
145            }
146    
147            @Override
148            public int compareTo(DateTime dt) throws PageException {
149                    return Operator.compare((java.util.Date)this, (java.util.Date)dt);
150            }
151    
152            @Override
153            public int compareTo(double d) throws PageException {
154                    return Operator.compare(castToDoubleValue(), d);
155            }
156    
157            @Override
158            public int compareTo(String str) {
159                    return Operator.compare(castToString(), str);
160            }
161            
162            public String toString() {
163                    return castToString();
164            /*synchronized (javaFormatter) {
165                    javaFormatter.setTimeZone(timezone);
166                return javaFormatter.format(this);
167            }*/
168            }
169    
170            @Override
171            public long sizeOf() {
172                    return SizeOf.LONG_SIZE+SizeOf.REF_SIZE;
173            }
174    
175    
176            @Override
177            public Object get(PageContext pc, Key key, Object defaultValue) {
178                    return Reflector.getField(this, key.getString(),defaultValue);
179            }
180    
181            @Override
182            public Object get(PageContext pc, Key key) throws PageException {
183                    return Reflector.getField(this, key.getString());
184            }
185    
186            @Override
187            public Object set(PageContext pc, Key propertyName, Object value) throws PageException {
188                    return Reflector.setField(this, propertyName.getString(),value);
189            }
190    
191            @Override
192            public Object setEL(PageContext pc, Key propertyName, Object value) {
193                    try {
194                            return Reflector.setField(this, propertyName.getString(),value);
195                    } catch (PageException e) {
196                            return value;
197                    }
198            }
199    
200            @Override
201            public Object call(PageContext pc, Key methodName, Object[] args) throws PageException {
202                    return MemberUtil.call(pc, this, methodName, args, CFTypes.TYPE_DATETIME, "datetime");
203            }
204    
205            @Override
206            public Object callWithNamedValues(PageContext pc, Key methodName, Struct args) throws PageException {
207                    return MemberUtil.callWithNamedValues(pc,this,methodName,args, CFTypes.TYPE_DATETIME, "datetime");
208            }
209            
210    }