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