001 package railo.runtime.type.dt; 002 003 import railo.runtime.PageContext; 004 import railo.runtime.dump.DumpData; 005 import railo.runtime.dump.DumpProperties; 006 import railo.runtime.dump.DumpTable; 007 import railo.runtime.dump.SimpleDumpData; 008 import railo.runtime.exp.ExpressionException; 009 import railo.runtime.exp.PageException; 010 import railo.runtime.op.Caster; 011 import railo.runtime.op.Operator; 012 013 /** 014 * TimeSpan Object, represent a timespan 015 */ 016 public final class TimeSpanImpl implements TimeSpan { 017 018 private double value; 019 private long valueMillis; 020 021 private int day; 022 private int hour; 023 private int minute; 024 private int second; 025 private int milli; 026 027 028 public static TimeSpan fromDays(double value){ 029 return new TimeSpanImpl(value); 030 } 031 public static TimeSpan fromMillis(long value){ 032 return new TimeSpanImpl(value); 033 } 034 035 private TimeSpanImpl(double valueDays) { 036 this((long)(valueDays*86400000D)); 037 } 038 039 private TimeSpanImpl(long valueMillis) { 040 value=valueMillis/86400000D; 041 long tmp=valueMillis; 042 day=(int) (valueMillis/86400000L); 043 tmp-=day*86400000; 044 hour=(int) (tmp/3600000); 045 tmp-=hour*3600000; 046 minute=(int) (tmp/60000); 047 tmp-=minute*60000; 048 second=(int) (tmp/1000); 049 tmp-=second*1000; 050 milli=(int) tmp; 051 052 this.valueMillis=valueMillis; 053 /*day=(int)value; 054 double diff=value-day; 055 diff*=24; 056 hour=(int)diff; 057 diff=diff-hour; 058 diff*=60; 059 minute=(int)diff; 060 diff=diff-minute; 061 diff*=60; 062 second=(int)diff; 063 this.value=value; 064 milli=(int)(valueMillis-((second+(minute*60L)+(hour*3600L)+(day*3600L*24L))*1000)); 065 */ 066 //print.out("a("+hashCode()+"):"+day+":"+hour+":"+minute+":"+second+"+"+milli); 067 068 069 //total=(second+(minute*60L)+(hour*3600L)+(day*3600L*24L))*1000; 070 //total=(second+(minute*60L)+(hour*3600L)+(day*3600L*24L))*1000; 071 072 } 073 074 /** 075 * constructor of the timespan class 076 * @param day 077 * @param hour 078 * @param minute 079 * @param second 080 */ 081 public TimeSpanImpl(int day, int hour, int minute, int second) { 082 083 this.day=day; 084 this.hour=hour; 085 this.minute=minute; 086 this.second=second; 087 value=day+(((double)hour)/24)+(((double)minute)/24/60)+(((double)second)/24/60/60); 088 valueMillis=(second+(minute*60L)+(hour*3600L)+(day*3600L*24L))*1000; 089 } 090 091 /** 092 * constructor of the timespan class 093 * @param day 094 * @param hour 095 * @param minute 096 * @param second 097 */ 098 public TimeSpanImpl(int day, int hour, int minute, int second, int millisecond) { 099 this.day=day; 100 this.hour=hour; 101 this.minute=minute; 102 this.second=second; 103 this.milli=millisecond; 104 value=day+(((double)hour)/24)+(((double)minute)/24/60)+(((double)second)/24/60/60)+(((double)millisecond)/24/60/60/1000); 105 valueMillis=((second+(minute*60L)+(hour*3600L)+(day*3600L*24L))*1000)+millisecond; 106 } 107 108 @Override 109 public String castToString() { 110 return Caster.toString(value); 111 } 112 113 @Override 114 public String castToString(String defaultValue) { 115 return Caster.toString(value); 116 } 117 118 @Override 119 public boolean castToBooleanValue() throws ExpressionException { 120 throw new ExpressionException("can't cast Timespan to boolean"); 121 } 122 123 @Override 124 public Boolean castToBoolean(Boolean defaultValue) { 125 return defaultValue; 126 } 127 128 @Override 129 public double castToDoubleValue() { 130 return value; 131 } 132 133 @Override 134 public double castToDoubleValue(double defaultValue) { 135 return value; 136 } 137 138 @Override 139 public DateTime castToDateTime() throws ExpressionException { 140 throw new ExpressionException("can't cast Timespan to date"); 141 } 142 143 @Override 144 public DateTime castToDateTime(DateTime defaultValue) { 145 return defaultValue; 146 } 147 148 149 @Override 150 public int compareTo(boolean b) { 151 return Operator.compare(value, b?1D:0D); 152 } 153 154 @Override 155 public int compareTo(DateTime dt) throws PageException { 156 return Operator.compare(value, dt.castToDoubleValue()); 157 } 158 159 @Override 160 public int compareTo(double d) throws PageException { 161 return Operator.compare(value, d); 162 } 163 164 @Override 165 public int compareTo(String str) throws PageException { 166 return Operator.compare(value, str); 167 } 168 169 @Override 170 public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) { 171 DumpTable table=new DumpTable("timespan","#ff9900","#ffcc00","#000000"); 172 if(milli>0)table.appendRow(1, new SimpleDumpData("Timespan"), new SimpleDumpData("createTimeSpan("+day+","+hour+","+minute+","+second+","+milli+")")); 173 else table.appendRow(1, new SimpleDumpData("Timespan"), new SimpleDumpData("createTimeSpan("+day+","+hour+","+minute+","+second+")")); 174 175 176 177 return table; 178 } 179 @Override 180 public long getMillis() { 181 return valueMillis; 182 } 183 public long getMilli() { 184 return milli; 185 } 186 187 @Override 188 public long getSeconds() { 189 return valueMillis/1000; 190 } 191 192 @Override 193 public String toString() { 194 if(milli>0) 195 return "createTimeSpan("+day+","+hour+","+minute+","+second+","+milli+")"; 196 return "createTimeSpan("+day+","+hour+","+minute+","+second+")"; 197 } 198 199 @Override 200 public int getDay() { 201 return day; 202 } 203 @Override 204 public int getHour() { 205 return hour; 206 } 207 @Override 208 public int getMinute() { 209 return minute; 210 } 211 @Override 212 public int getSecond() { 213 return second; 214 } 215 }