| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.value; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.lib.ConversionRules; |
| 12 |
import net.sf.saxon.trans.XPathException; |
| 13 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 14 |
import net.sf.saxon.type.*; |
| 15 |
|
| 16 |
import java.time.LocalDate; |
| 17 |
import java.time.format.DateTimeParseException; |
| 18 |
import java.time.temporal.WeekFields; |
| 19 |
import java.util.Calendar; |
| 20 |
import java.util.GregorianCalendar; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class DateValue extends GDateValue implements Comparable { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private DateValue() { |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public DateValue(int year, byte month, byte day) { |
| 45 |
this.hasNoYearZero = true; |
| 46 |
this.year = year; |
| 47 |
this.month = month; |
| 48 |
this.day = day; |
| 49 |
typeLabel = BuiltInAtomicType.DATE; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public DateValue(int year, byte month, byte day, boolean xsd10) { |
| 64 |
this.hasNoYearZero = xsd10; |
| 65 |
this.year = year; |
| 66 |
this.month = month; |
| 67 |
this.day = day; |
| 68 |
typeLabel = BuiltInAtomicType.DATE; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public DateValue(int year, byte month, byte day, int tz, boolean xsd10) { |
| 85 |
|
| 86 |
this.hasNoYearZero = xsd10; |
| 87 |
this.year = year; |
| 88 |
this.month = month; |
| 89 |
this.day = day; |
| 90 |
setTimezoneInMinutes(tz); |
| 91 |
typeLabel = BuiltInAtomicType.DATE; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public DateValue(int year, byte month, byte day, int tz, AtomicType type) { |
| 108 |
this.year = year; |
| 109 |
this.month = month; |
| 110 |
this.day = day; |
| 111 |
setTimezoneInMinutes(tz); |
| 112 |
typeLabel = type; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public DateValue(CharSequence s) throws ValidationException { |
| 124 |
this(s, ConversionRules.DEFAULT); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public DateValue(CharSequence s, ConversionRules rules) throws ValidationException { |
| 136 |
setLexicalValue(this, s, rules.isAllowYearZero()).asAtomic(); |
| 137 |
typeLabel = BuiltInAtomicType.DATE; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public DateValue(GregorianCalendar calendar, int tz) { |
| 148 |
|
| 149 |
int era = calendar.get(GregorianCalendar.ERA); |
| 150 |
year = calendar.get(Calendar.YEAR); |
| 151 |
if (era == GregorianCalendar.BC) { |
| 152 |
year = 1 - year; |
| 153 |
} |
| 154 |
month = (byte) (calendar.get(Calendar.MONTH) + 1); |
| 155 |
day = (byte) calendar.get(Calendar.DATE); |
| 156 |
setTimezoneInMinutes(tz); |
| 157 |
typeLabel = BuiltInAtomicType.DATE; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public static ConversionResult makeDateValue(CharSequence in, ConversionRules rules) { |
| 170 |
DateValue d = new DateValue(); |
| 171 |
d.typeLabel = BuiltInAtomicType.DATE; |
| 172 |
return setLexicalValue(d, in, rules.isAllowYearZero()); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public static DateValue parse(CharSequence s) throws DateTimeParseException { |
| 188 |
ConversionResult result = makeDateValue(s, ConversionRules.DEFAULT); |
| 189 |
if (result instanceof ValidationFailure) { |
| 190 |
throw new DateTimeParseException(((ValidationFailure) result).getMessage(), s, 0); |
| 191 |
} else { |
| 192 |
return (DateValue) result; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public BuiltInAtomicType getPrimitiveType() { |
| 204 |
return BuiltInAtomicType.DATE; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public static DateValue tomorrow(int year, byte month, byte day) { |
| 217 |
if (DateValue.isValidDate(year, month, day + 1)) { |
| 218 |
return new DateValue(year, month, (byte) (day + 1), true); |
| 219 |
} else if (month < 12) { |
| 220 |
return new DateValue(year, (byte) (month + 1), (byte) 1, true); |
| 221 |
} else { |
| 222 |
return new DateValue(year + 1, (byte) 1, (byte) 1, true); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public static DateValue yesterday(int year, byte month, byte day) { |
| 236 |
if (day > 1) { |
| 237 |
return new DateValue(year, month, (byte) (day - 1), true); |
| 238 |
} else if (month > 1) { |
| 239 |
if (month == 3 && isLeapYear(year)) { |
| 240 |
return new DateValue(year, (byte) 2, (byte) 29, true); |
| 241 |
} else { |
| 242 |
return new DateValue(year, (byte) (month - 1), daysPerMonth[month - 2], true); |
| 243 |
} |
| 244 |
} else { |
| 245 |
return new DateValue(year - 1, (byte) 12, (byte) 31, true); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public CharSequence getPrimitiveStringValue() { |
| 256 |
|
| 257 |
FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C16); |
| 258 |
int yr = year; |
| 259 |
if (year <= 0) { |
| 260 |
yr = -yr + (hasNoYearZero ? 1 : 0); |
| 261 |
if (yr != 0) { |
| 262 |
sb.append('-'); |
| 263 |
} |
| 264 |
} |
| 265 |
appendString(sb, yr, yr > 9999 ? (yr + "").length() : 4); |
| 266 |
sb.append('-'); |
| 267 |
appendTwoDigits(sb, month); |
| 268 |
sb.append('-'); |
| 269 |
appendTwoDigits(sb, day); |
| 270 |
|
| 271 |
if (hasTimezone()) { |
| 272 |
appendTimezone(sb); |
| 273 |
} |
| 274 |
|
| 275 |
return sb; |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public CharSequence getCanonicalLexicalRepresentation() { |
| 289 |
DateValue target = this; |
| 290 |
if (hasTimezone()) { |
| 291 |
if (getTimezoneInMinutes() > 12 * 60) { |
| 292 |
target = adjustTimezone(getTimezoneInMinutes() - 24 * 60); |
| 293 |
} else if (getTimezoneInMinutes() <= -12 * 60) { |
| 294 |
target = adjustTimezone(getTimezoneInMinutes() + 24 * 60); |
| 295 |
} |
| 296 |
} |
| 297 |
return target.getStringValueCS(); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public AtomicValue copyAsSubType(AtomicType typeLabel) { |
| 309 |
DateValue v = new DateValue(year, month, day, getTimezoneInMinutes(), hasNoYearZero); |
| 310 |
v.typeLabel = typeLabel; |
| 311 |
return v; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public DateValue adjustTimezone(int timezone) { |
| 324 |
DateTimeValue dt = toDateTime().adjustTimezone(timezone); |
| 325 |
return new DateValue(dt.getYear(), dt.getMonth(), dt.getDay(), dt.getTimezoneInMinutes(), hasNoYearZero); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public DateValue add(DurationValue duration) throws XPathException { |
| 339 |
if (duration instanceof DayTimeDurationValue) { |
| 340 |
long microseconds = ((DayTimeDurationValue) duration).getLengthInMicroseconds(); |
| 341 |
boolean negative = microseconds < 0; |
| 342 |
microseconds = Math.abs(microseconds); |
| 343 |
int days = (int) Math.floor((double) microseconds / (1000000L * 60L * 60L * 24L)); |
| 344 |
boolean partDay = (microseconds % (1000000L * 60L * 60L * 24L)) > 0; |
| 345 |
int julian = getJulianDayNumber(); |
| 346 |
DateValue d = dateFromJulianDayNumber(julian + (negative ? -days : days)); |
| 347 |
if (partDay) { |
| 348 |
if (negative) { |
| 349 |
d = yesterday(d.year, d.month, d.day); |
| 350 |
} |
| 351 |
} |
| 352 |
d.setTimezoneInMinutes(getTimezoneInMinutes()); |
| 353 |
d.hasNoYearZero = this.hasNoYearZero; |
| 354 |
return d; |
| 355 |
} else if (duration instanceof YearMonthDurationValue) { |
| 356 |
int months = ((YearMonthDurationValue) duration).getLengthInMonths(); |
| 357 |
int m = (month - 1) + months; |
| 358 |
int y = year + m / 12; |
| 359 |
m = m % 12; |
| 360 |
if (m < 0) { |
| 361 |
m += 12; |
| 362 |
y -= 1; |
| 363 |
} |
| 364 |
m++; |
| 365 |
int d = day; |
| 366 |
while (!isValidDate(y, m, d)) { |
| 367 |
d -= 1; |
| 368 |
} |
| 369 |
return new DateValue(y, (byte) m, (byte) d, getTimezoneInMinutes(), hasNoYearZero); |
| 370 |
} else { |
| 371 |
XPathException err = new XPathException("Date arithmetic is not available for xs:duration, only for its subtypes"); |
| 372 |
err.setIsTypeError(true); |
| 373 |
err.setErrorCode("XPTY0004"); |
| 374 |
throw err; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
public DayTimeDurationValue subtract( CalendarValue other, XPathContext context) throws XPathException { |
| 389 |
if (!(other instanceof DateValue)) { |
| 390 |
XPathException err = new XPathException("First operand of '-' is a date, but the second is not"); |
| 391 |
err.setIsTypeError(true); |
| 392 |
err.setErrorCode("XPTY0004"); |
| 393 |
throw err; |
| 394 |
} |
| 395 |
return super.subtract(other, context); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public int compareTo(Object v2) { |
| 411 |
try { |
| 412 |
return compareTo((DateValue) v2, MISSING_TIMEZONE); |
| 413 |
} catch (Exception err) { |
| 414 |
throw new ClassCastException("Date comparison requires access to implicit timezone"); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public static int getJulianDayNumber(int year, int month, int day) { |
| 432 |
int z = year - (month < 3 ? 1 : 0); |
| 433 |
short f = monthData[month - 1]; |
| 434 |
if (z >= 0) { |
| 435 |
return day + f + 365 * z + z / 4 - z / 100 + z / 400 + 1721118; |
| 436 |
} else { |
| 437 |
|
| 438 |
z += 12000; |
| 439 |
int j = day + f + 365 * z + z / 4 - z / 100 + z / 400 + 1721118; |
| 440 |
return j - (365 * 12000 + 12000 / 4 - 12000 / 100 + 12000 / 400); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public int getJulianDayNumber() { |
| 452 |
return getJulianDayNumber(year, month, day); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public static DateValue dateFromJulianDayNumber(int julianDayNumber) { |
| 464 |
if (julianDayNumber >= 0) { |
| 465 |
int L = julianDayNumber + 68569 + 1; |
| 466 |
int n = (4 * L) / 146097; |
| 467 |
L = L - (146097 * n + 3) / 4; |
| 468 |
int i = (4000 * (L + 1)) / 1461001; |
| 469 |
L = L - (1461 * i) / 4 + 31; |
| 470 |
int j = (80 * L) / 2447; |
| 471 |
int d = L - (2447 * j) / 80; |
| 472 |
L = j / 11; |
| 473 |
int m = j + 2 - (12 * L); |
| 474 |
int y = 100 * (n - 49) + i + L; |
| 475 |
return new DateValue(y, (byte) m, (byte) d, true); |
| 476 |
} else { |
| 477 |
|
| 478 |
DateValue dt = dateFromJulianDayNumber(julianDayNumber + |
| 479 |
365 * 12000 + 12000 / 4 - 12000 / 100 + 12000 / 400); |
| 480 |
dt.year -= 12000; |
| 481 |
return dt; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
public static int getDayWithinYear(int year, int month, int day) { |
| 495 |
int j = getJulianDayNumber(year, month, day); |
| 496 |
int k = getJulianDayNumber(year, 1, 1); |
| 497 |
return j - k + 1; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
public static int getDayOfWeek(int year, int month, int day) { |
| 511 |
int d = getJulianDayNumber(year, month, day); |
| 512 |
d -= 2378500; |
| 513 |
while (d <= 0) { |
| 514 |
d += 70000000; |
| 515 |
} |
| 516 |
return (d - 1) % 7 + 1; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public static int getWeekNumber(int year, int month, int day) { |
| 531 |
LocalDate date = LocalDate.of(year, month, day); |
| 532 |
return date.get(WeekFields.ISO.weekOfWeekBasedYear()); |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
public static int getWeekNumberWithinMonth(int year, int month, int day) { |
| 551 |
int firstDay = getDayOfWeek(year, month, 1); |
| 552 |
if (firstDay > 4 && (firstDay + day) <= 8) { |
| 553 |
|
| 554 |
DateValue lastDayPrevMonth = yesterday(year, (byte) month, (byte) 1); |
| 555 |
return getWeekNumberWithinMonth(lastDayPrevMonth.year, lastDayPrevMonth.month, lastDayPrevMonth.day); |
| 556 |
} |
| 557 |
int inc = firstDay < 5 ? 1 : 0; |
| 558 |
return ((day + firstDay - 2) / 7) + inc; |
| 559 |
} |
| 560 |
|
| 561 |
} |
| 562 |
|