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