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