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