1 10 package com.hp.hpl.jena.datatypes.xsd; 11 12 21 public class XSDDuration extends AbstractDateTime { 22 23 29 public XSDDuration(Object value) { 30 super(value); 31 } 32 33 36 public int getYears() { 37 return data[CY]; 38 } 39 40 43 public int getMonths() { 44 return data[M]; 45 } 46 47 50 public int getDays() { 51 return data[D]; 52 } 53 54 57 public int getHours() { 58 return data[h]; 59 } 60 61 64 public int getMinutes() { 65 return data[m]; 66 } 67 68 71 public int getFullSeconds() { 72 return data[s]; 73 } 74 75 78 public double getSeconds() { 79 return data[s] + fractionalSeconds; 80 } 81 82 86 public double getTimePart() { 87 return ((data[h]) * 60l + data[m]) * 60l + getSeconds(); 88 } 89 90 93 public String toString() { 94 StringBuffer message = new StringBuffer (30); 95 int negate = 1; 96 if ( data[CY]<0 ) { 97 message.append('-'); 98 negate=-1; 99 } 100 message.append('P'); 101 message.append(negate * data[CY]); 102 message.append('Y'); 103 message.append(negate * data[M]); 104 message.append('M'); 105 message.append(negate * data[D]); 106 message.append('D'); 107 message.append('T'); 108 message.append(negate * data[h]); 109 message.append('H'); 110 message.append(negate * data[m]); 111 message.append('M'); 112 message.append(negate * data[s]); 113 message.append('.'); 114 message.append(negate * data[ms]); 115 message.append('S'); 116 117 return message.toString(); 118 } 119 120 private final static int[][] DATETIMES= { 127 {1696, 9, 1, 0, 0, 0, 0, 'Z'}, 128 {1697, 2, 1, 0, 0, 0, 0, 'Z'}, 129 {1903, 3, 1, 0, 0, 0, 0, 'Z'}, 130 {1903, 7, 1, 0, 0, 0, 0, 'Z'}}; 131 132 private int[][] fDuration = null; 133 134 148 protected short compareDates(int[] date1, int[] date2, boolean strict) { 149 150 154 short resultA, resultB= INDETERMINATE; 156 157 resultA = compareOrder (date1, date2); 159 if ( resultA == 0 ) { 160 return 0; 161 } 162 if ( fDuration == null ) { 163 fDuration = new int[2][TOTAL_SIZE]; 164 } 165 int[] tempA = addDuration (date1, 0, fDuration[0]); 167 int[] tempB = addDuration (date2, 0, fDuration[1]); 168 resultA = compareOrder(tempA, tempB); 169 if ( resultA == INDETERMINATE ) { 170 return INDETERMINATE; 171 } 172 173 tempA = addDuration(date1, 1, fDuration[0]); 174 tempB = addDuration(date2, 1, fDuration[1]); 175 resultB = compareOrder(tempA, tempB); 176 resultA = compareResults(resultA, resultB, strict); 177 if (resultA == INDETERMINATE) { 178 return INDETERMINATE; 179 } 180 181 tempA = addDuration(date1, 2, fDuration[0]); 182 tempB = addDuration(date2, 2, fDuration[1]); 183 resultB = compareOrder(tempA, tempB); 184 resultA = compareResults(resultA, resultB, strict); 185 if (resultA == INDETERMINATE) { 186 return INDETERMINATE; 187 } 188 189 tempA = addDuration(date1, 3, fDuration[0]); 190 tempB = addDuration(date2, 3, fDuration[1]); 191 resultB = compareOrder(tempA, tempB); 192 resultA = compareResults(resultA, resultB, strict); 193 194 return resultA; 195 } 196 197 private short compareResults(short resultA, short resultB, boolean strict){ 198 199 if ( resultB == INDETERMINATE ) { 200 return INDETERMINATE; 201 } 202 else if ( resultA!=resultB && strict ) { 203 return INDETERMINATE; 204 } 205 else if ( resultA!=resultB && !strict ) { 206 if ( resultA!=0 && resultB!=0 ) { 207 return INDETERMINATE; 208 } 209 else { 210 return (resultA!=0)?resultA:resultB; 211 } 212 } 213 return resultA; 214 } 215 216 private int[] addDuration(int[] date, int index, int[] duration) { 217 218 222 resetDateObj(duration); 223 int temp = DATETIMES[index][M] + date[M]; 225 duration[M] = modulo (temp, 1, 13); 226 int carry = fQuotient (temp, 1, 13); 227 228 duration[CY]=DATETIMES[index][CY] + date[CY] + carry; 230 231 temp = DATETIMES[index][s] + date[s]; 233 carry = fQuotient (temp, 60); 234 duration[s] = mod(temp, 60, carry); 235 236 temp = DATETIMES[index][m] +date[m] + carry; 238 carry = fQuotient (temp, 60); 239 duration[m]= mod(temp, 60, carry); 240 241 temp = DATETIMES[index][h] + date[h] + carry; 243 carry = fQuotient(temp, 24); 244 duration[h] = mod(temp, 24, carry); 245 246 247 duration[D]=DATETIMES[index][D] + date[D] + carry; 248 249 while ( true ) { 250 251 temp=maxDayInMonthFor(duration[CY], duration[M]); 252 if ( duration[D] < 1 ) { duration[D] = duration[D] + maxDayInMonthFor(duration[CY], duration[M]-1); 254 carry=-1; 255 } 256 else if ( duration[D] > temp ) { 257 duration[D] = duration[D] - temp; 258 carry=1; 259 } 260 else { 261 break; 262 } 263 temp = duration[M]+carry; 264 duration[M] = modulo(temp, 1, 13); 265 duration[CY] = duration[CY]+fQuotient(temp, 1, 13); 266 } 267 268 duration[utc]='Z'; 269 return duration; 270 } 271 272 } 273 274 303 | Popular Tags |