1 10 package com.hp.hpl.jena.datatypes.xsd; 11 12 26 public class AbstractDateTime { 27 28 29 protected int[] data; 30 31 32 protected double fractionalSeconds; 33 34 protected final static int CY = 0, M = 1, D = 2, h = 3, 36 m = 4, s = 5, ms = 6, utc=7, hh=0, mm=1; 37 38 protected final static int TOTAL_SIZE = 8; 41 42 43 public static final short LESS_THAN = -1; 44 45 public static final short EQUAL = 0; 46 47 public static final short GREATER_THAN = 1; 48 49 public static final short INDETERMINATE = 2; 50 51 55 public AbstractDateTime(Object value) { 56 data = (int[]) value; 57 if (data[utc] == 0) data[utc]='Z'; 58 extractFractionalSeconds(); 59 } 60 61 68 public int compare(AbstractDateTime other) { 69 return compareDates(data, other.data, true); 70 } 71 72 75 protected void extractFractionalSeconds() { 76 if (data[ms] != 0) { 77 int fs = data[ms]; 78 double log10 = Math.log((double)fs)/2.302585093; 79 int exp = 1 + (int)(log10 / 10); 80 fractionalSeconds = ((double)fs) / Math.pow(10.0, (double)exp); 81 } 82 } 83 84 87 public boolean equals(Object obj) { 88 if (obj instanceof AbstractDateTime) { 89 AbstractDateTime adt = (AbstractDateTime) obj; 90 for (int i = 0; i < data.length; i++) { 91 if (data[i] != adt.data[i]) return false; 92 } 93 return true; 94 } 95 return false; 96 } 97 98 101 public int hashCode() { 102 int hash = 0; 103 for (int i = 0; i < data.length; i++) { 104 hash = (hash << 1) ^ data[i]; 105 } 106 return hash; 107 } 108 109 115 123 protected short compareDates(int[] date1, int[] date2, boolean strict) { 124 if ( date1[utc]==date2[utc] ) { 125 return compareOrder(date1, date2); 126 } 127 short c1, c2; 128 129 int[] tempDate = new int[TOTAL_SIZE]; 130 int[] timeZone = new int[2]; 131 132 if ( date1[utc]=='Z' ) { 133 134 cloneDate(date2, tempDate); timeZone[hh]=14; 138 timeZone[mm]=0; 139 tempDate[utc]='+'; 140 normalize(tempDate, timeZone); 141 c1 = compareOrder(date1, tempDate); 142 if (c1 == LESS_THAN) 143 return c1; 144 145 cloneDate(date2, tempDate); timeZone[hh]=14; 149 timeZone[mm]=0; 150 tempDate[utc]='-'; 151 normalize(tempDate, timeZone); 152 c2 = compareOrder(date1, tempDate); 153 if (c2 == GREATER_THAN) 154 return c2; 155 156 return INDETERMINATE; 157 } 158 else if ( date2[utc]=='Z' ) { 159 160 cloneDate(date1, tempDate); timeZone[hh]=14; 164 timeZone[mm]=0; 165 tempDate[utc]='-'; 166 normalize(tempDate, timeZone); 167 c1 = compareOrder(tempDate, date2); 168 if (c1 == LESS_THAN) 169 return c1; 170 171 cloneDate(date1, tempDate); timeZone[hh]=14; 175 timeZone[mm]=0; 176 tempDate[utc]='+'; 177 normalize(tempDate, timeZone); 178 c2 = compareOrder(tempDate, date2); 179 if (c2 == GREATER_THAN) 180 return c2; 181 182 return INDETERMINATE; 183 } 184 return INDETERMINATE; 185 186 } 187 188 196 protected short compareOrder (int[] date1, int[] date2) { 197 198 for ( int i=0;i<TOTAL_SIZE;i++ ) { 199 if ( date1[i]<date2[i] ) { 200 return -1; 201 } 202 else if ( date1[i]>date2[i] ) { 203 return 1; 204 } 205 } 206 return 0; 207 } 208 214 public static void normalize (int[] date, int[] timeZone) { 215 216 220 int negate = 1; 222 if (date[utc]=='+') { 223 negate = -1; 224 } 225 int temp = date[m] + negate*timeZone[mm]; 226 int carry = fQuotient (temp, 60); 227 date[m]= mod(temp, 60, carry); 228 229 temp = date[h] + negate*timeZone[hh] + carry; 231 carry = fQuotient(temp, 24); 232 date[h]=mod(temp, 24, carry); 233 234 date[D]=date[D]+carry; 235 236 while ( true ) { 237 temp=maxDayInMonthFor(date[CY], date[M]); 238 if (date[D]<1) { 239 date[D] = date[D] + maxDayInMonthFor(date[CY], date[M]-1); 240 carry=-1; 241 } 242 else if ( date[D]>temp ) { 243 date[D]=date[D]-temp; 244 carry=1; 245 } 246 else { 247 break; 248 } 249 temp=date[M]+carry; 250 date[M]=modulo(temp, 1, 13); 251 date[CY]=date[CY]+fQuotient(temp, 1, 13); 252 } 253 date[utc]='Z'; 254 } 255 256 257 262 protected void resetDateObj (int[] data) { 263 for ( int i=0;i<TOTAL_SIZE;i++ ) { 264 data[i]=0; 265 } 266 } 267 268 276 protected static int maxDayInMonthFor(int year, int month) { 277 if ( month==4 || month==6 || month==9 || month==11 ) { 279 return 30; 280 } 281 else if ( month==2 ) { 282 if ( isLeapYear(year) ) { 283 return 29; 284 } 285 else { 286 return 28; 287 } 288 } 289 else { 290 return 31; 291 } 292 } 293 294 private static boolean isLeapYear(int year) { 295 296 return((year%4 == 0) && ((year%100 != 0) || (year%400 == 0))); 298 } 299 300 protected static int mod (int a, int b, int quotient) { 304 return (a - quotient*b) ; 306 } 307 308 protected static int fQuotient (int a, int b) { 312 313 return (int)Math.floor((float)a/b); 315 } 316 317 protected static int modulo (int temp, int low, int high) { 321 int a = temp - low; 323 int b = high - low; 324 return (mod (a, b, fQuotient(a, b)) + low) ; 325 } 326 327 protected static int fQuotient (int temp, int low, int high) { 331 333 return fQuotient(temp - low, high - low); 334 } 335 336 340 private void cloneDate (int[] finalValue, int[] tempDate) { 341 System.arraycopy(finalValue, 0, tempDate, 0, TOTAL_SIZE); 342 } 343 344 348 } 349 350 379 | Popular Tags |