001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.type.dt;
020
021
022import java.util.Calendar;
023import java.util.Date;
024import java.util.TimeZone;
025
026import lucee.commons.date.DateTimeUtil;
027import lucee.commons.lang.CFTypes;
028import lucee.commons.lang.SizeOf;
029import lucee.runtime.PageContext;
030import lucee.runtime.config.Config;
031import lucee.runtime.dump.DumpData;
032import lucee.runtime.dump.DumpProperties;
033import lucee.runtime.dump.DumpTable;
034import lucee.runtime.dump.SimpleDumpData;
035import lucee.runtime.engine.ThreadLocalPageContext;
036import lucee.runtime.exp.ExpressionException;
037import lucee.runtime.exp.PageException;
038import lucee.runtime.op.Operator;
039import lucee.runtime.reflection.Reflector;
040import lucee.runtime.type.Collection.Key;
041import lucee.runtime.type.Objects;
042import lucee.runtime.type.SimpleValue;
043import lucee.runtime.type.Sizeable;
044import lucee.runtime.type.Struct;
045import lucee.runtime.type.util.MemberUtil;
046
047/**
048 * Printable and Castable DateTime Object
049 */
050public final class DateTimeImpl extends DateTime implements SimpleValue,Sizeable,Objects {
051        
052        public DateTimeImpl(PageContext pc) {
053                this(pc,System.currentTimeMillis(),true);
054        }
055        
056        public DateTimeImpl(Config config) {
057                this(config,System.currentTimeMillis(),true);
058        }
059        
060        public DateTimeImpl() {
061                this(System.currentTimeMillis(),true);
062        }
063        
064        public DateTimeImpl(PageContext pc, long utcTime, boolean doOffset) {
065                super(doOffset?addOffset(ThreadLocalPageContext.getConfig(pc), utcTime):utcTime);
066        }
067
068        public DateTimeImpl(Config config, long utcTime, boolean doOffset) {
069                super(doOffset?addOffset(ThreadLocalPageContext.getConfig(config), utcTime):utcTime);
070        }
071        
072        public DateTimeImpl(long utcTime, boolean doOffset) {
073                super(doOffset?addOffset(ThreadLocalPageContext.getConfig(), utcTime):utcTime);
074        }
075        
076        /*public DateTimeImpl(Config config, long utcTime) {
077                super(addOffset(ThreadLocalPageContext.getConfig(config),utcTime));
078        }*/
079
080        public DateTimeImpl(Date date) {
081                this(date.getTime(),false);
082        }
083        
084        
085        public DateTimeImpl(Calendar calendar) {
086                super(calendar.getTimeInMillis());
087                //this.timezone=ThreadLocalPageContext.getTimeZone(calendar.getTimeZone());
088        }
089
090        public static long addOffset(Config config, long utcTime) {
091                if(config!=null) return utcTime+config.getTimeServerOffset();
092                return utcTime;
093        }
094        
095        
096        @Override
097        public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
098                String str=castToString(pageContext.getTimeZone());
099        DumpTable table=new DumpTable("date","#ff6600","#ffcc99","#000000");
100        if(dp.getMetainfo())
101                table.appendRow(1, new SimpleDumpData("Date Time ("+pageContext.getTimeZone().getID()+")"));
102        else
103                table.appendRow(1, new SimpleDumpData("Date Time"));
104        table.appendRow(0, new SimpleDumpData(str));
105        return table;
106        }
107
108        @Override
109        public String castToString() {
110                return castToString((TimeZone)null);
111    }
112
113
114    @Override
115    public String castToString(String defaultValue) {
116        return castToString((TimeZone)null);
117    }
118    
119        public String castToString(TimeZone tz) {// MUST move to DateTimeUtil
120                return DateTimeUtil.getInstance().toString(this,tz);
121                
122        }
123        
124        @Override
125        public boolean castToBooleanValue() throws ExpressionException {
126        return DateTimeUtil.getInstance().toBooleanValue(this);
127        }
128    
129    @Override
130    public Boolean castToBoolean(Boolean defaultValue) {
131        return defaultValue;
132    }
133
134        @Override
135        public double castToDoubleValue() {
136            return toDoubleValue();
137        }
138    
139    @Override
140    public double castToDoubleValue(double defaultValue) {
141        return toDoubleValue();
142    }
143
144        @Override
145        public DateTime castToDateTime() {
146                return this;
147        }
148    
149    @Override
150    public DateTime castToDateTime(DateTime defaultValue) {
151        return this;
152    }
153        
154        @Override
155        public double toDoubleValue() {
156            return DateTimeUtil.getInstance().toDoubleValue(this);
157        }
158
159
160        @Override
161        public int compareTo(boolean b) {
162                return Operator.compare(castToDoubleValue(), b?1D:0D);
163        }
164
165        @Override
166        public int compareTo(DateTime dt) throws PageException {
167                return Operator.compare((java.util.Date)this, (java.util.Date)dt);
168        }
169
170        @Override
171        public int compareTo(double d) throws PageException {
172                return Operator.compare(castToDoubleValue(), d);
173        }
174
175        @Override
176        public int compareTo(String str) {
177                return Operator.compare(castToString(), str);
178        }
179        
180        public String toString() {
181                return castToString();
182        /*synchronized (javaFormatter) {
183                javaFormatter.setTimeZone(timezone);
184            return javaFormatter.format(this);
185        }*/
186        }
187
188        @Override
189        public long sizeOf() {
190                return SizeOf.LONG_SIZE+SizeOf.REF_SIZE;
191        }
192
193
194        @Override
195        public Object get(PageContext pc, Key key, Object defaultValue) {
196                return Reflector.getField(this, key.getString(),defaultValue);
197        }
198
199        @Override
200        public Object get(PageContext pc, Key key) throws PageException {
201                return Reflector.getField(this, key.getString());
202        }
203
204        @Override
205        public Object set(PageContext pc, Key propertyName, Object value) throws PageException {
206                return Reflector.setField(this, propertyName.getString(),value);
207        }
208
209        @Override
210        public Object setEL(PageContext pc, Key propertyName, Object value) {
211                try {
212                        return Reflector.setField(this, propertyName.getString(),value);
213                } catch (PageException e) {
214                        return value;
215                }
216        }
217
218        @Override
219        public Object call(PageContext pc, Key methodName, Object[] args) throws PageException {
220                return MemberUtil.call(pc, this, methodName, args, CFTypes.TYPE_DATETIME, "datetime");
221        }
222
223        @Override
224        public Object callWithNamedValues(PageContext pc, Key methodName, Struct args) throws PageException {
225                return MemberUtil.callWithNamedValues(pc,this,methodName,args, CFTypes.TYPE_DATETIME, "datetime");
226        }
227        
228}