1 30 31 32 package org.hsqldb; 33 34 import java.sql.Date ; 35 import java.sql.Time ; 36 import java.sql.Timestamp ; 37 import java.text.SimpleDateFormat ; 38 import java.util.Calendar ; 39 import java.util.GregorianCalendar ; 40 import java.util.TimeZone ; 41 42 48 71 public class HsqlDateTime { 72 73 77 private static Calendar today = new GregorianCalendar (); 78 private static Calendar tempCal = new GregorianCalendar (); 79 private static Calendar tempCalDefault = new GregorianCalendar (); 80 private static Calendar tempCalGMT = 81 new GregorianCalendar (TimeZone.getTimeZone("GMT")); 82 private static Date tempDate = new Date (0); 83 private static Date currentDate; 84 85 static { 86 resetToday(System.currentTimeMillis()); 87 } 88 89 static final String zerodatetime = "1970-01-01 00:00:00.000000000"; 90 static final String zeronanos = "000000000"; 91 92 102 public static Timestamp timestampValue(String s) throws HsqlException { 103 104 if (s == null) { 105 throw Trace.error(Trace.HsqlDateTime_null_string); 106 } 107 108 if (s.length() > zerodatetime.length()) { 109 throw Trace.error(Trace.STRING_DATA_TRUNCATION); 110 } 111 112 s = s + zerodatetime.substring(s.length()); 113 114 return Timestamp.valueOf(s); 115 } 116 117 120 public static Timestamp simpleTimestampValue(String s) { 121 return Timestamp.valueOf(s); 122 } 123 124 129 public static Timestamp timestampValue(long time, int nano) { 130 131 Timestamp ts = new Timestamp (time); 132 133 ts.setNanos(nano); 134 135 return ts; 136 } 137 138 147 public static Date dateValue(String s) throws HsqlException { 148 149 if (s == null) { 150 throw Trace.error(Trace.HsqlDateTime_null_string); 151 } 152 153 if (s.length() > sdfdPattern.length()) { 154 s = s.substring(0, sdfdPattern.length()); 155 } 156 157 return Date.valueOf(s); 158 } 159 160 169 public static Time timeValue(String s) { 170 171 if (s == null) { 172 throw new java.lang.IllegalArgumentException ( 173 Trace.getMessage(Trace.HsqlDateTime_null_string)); 174 } 175 176 return Time.valueOf(s); 177 } 178 179 static int compare(Date a, Date b) { 180 181 long atime = a.getTime(); 182 long btime = b.getTime(); 183 184 if (atime == btime) { 185 return 0; 186 } 187 188 return atime > btime ? 1 189 : -1; 190 } 191 192 static int compare(Time a, Time b) { 193 194 long atime = a.getTime(); 195 long btime = b.getTime(); 196 197 if (atime == btime) { 198 return 0; 199 } 200 201 return atime > btime ? 1 202 : -1; 203 } 204 205 static int compare(Timestamp a, Timestamp b) { 206 207 long atime = a.getTime(); 208 long btime = b.getTime(); 209 210 if (atime == btime) { 211 if (a.getNanos() == b.getNanos()) { 212 return 0; 213 } 214 215 return a.getNanos() > b.getNanos() ? 1 216 : -1; 217 } 218 219 return atime > btime ? 1 220 : -1; 221 } 222 223 public static synchronized Date getCurrentDate(long millis) { 224 225 getToday(millis); 226 227 return currentDate; 228 } 229 230 public static Timestamp getTimestamp(long millis) { 231 return new Timestamp (millis); 232 } 233 234 private static final String sdftPattern = "HH:mm:ss"; 235 private static final String sdfdPattern = "yyyy-MM-dd"; 236 private static final String sdftsPattern = "yyyy-MM-dd HH:mm:ss."; 237 private static final String sdftsSysPattern = "yyyy-MM-dd HH:mm:ss.SSS"; 238 static SimpleDateFormat sdfd = new SimpleDateFormat (sdfdPattern); 239 static SimpleDateFormat sdft = new SimpleDateFormat (sdftPattern); 240 static SimpleDateFormat sdfts = new SimpleDateFormat (sdftsPattern); 241 static SimpleDateFormat sdftsSys = new SimpleDateFormat (sdftsSysPattern); 242 243 247 public static String getTimestampString(Timestamp x) { 248 249 synchronized (sdfts) { 250 sdfts.setCalendar(tempCalDefault); 251 252 String n = String.valueOf(x.getNanos()); 253 254 return sdfts.format(x) + zeronanos.substring(n.length()) + n; 255 } 256 } 257 258 261 public static String getTimestampString(Timestamp x, Calendar cal) { 262 263 synchronized (sdfts) { 264 sdfts.setCalendar(cal == null ? tempCalDefault 265 : cal); 266 267 String n = String.valueOf(x.getNanos()); 268 269 return sdfts.format(x) + zeronanos.substring(n.length()) + n; 270 } 271 } 272 273 private static java.util.Date sysDate = new java.util.Date (); 274 275 public static String getSytemTimeString() { 276 277 synchronized (sdftsSys) { 278 sysDate.setTime(System.currentTimeMillis()); 279 280 return sdftsSys.format(sysDate); 281 } 282 } 283 284 public static String getTimestampString(long timestamp) { 285 286 synchronized (sdftsSys) { 287 sysDate.setTime(timestamp); 288 289 return sdftsSys.format(sysDate); 290 } 291 } 292 293 public static String getTimeString(java.util.Date x, Calendar cal) { 294 295 synchronized (sdft) { 296 sdft.setCalendar(cal == null ? tempCalDefault 297 : cal); 298 299 return sdft.format(x); 300 } 301 } 302 303 public static String getDateString(java.util.Date x, Calendar cal) { 304 305 synchronized (sdfd) { 306 sdfd.setCalendar(cal == null ? tempCalDefault 307 : cal); 308 309 return sdfd.format(x); 310 } 311 } 312 313 317 static synchronized Calendar getToday(long millis) { 318 319 if (millis - getTimeInMillis(today) >= 24 * 3600 * 1000) { 320 resetToday(millis); 321 } 322 323 return today; 324 } 325 326 public static void resetToDate(Calendar cal) { 327 328 cal.set(Calendar.HOUR_OF_DAY, 0); 329 cal.set(Calendar.MINUTE, 0); 330 cal.set(Calendar.SECOND, 0); 331 cal.set(Calendar.MILLISECOND, 0); 332 } 333 334 public static void resetToTime(Calendar cal) { 335 336 cal.set(Calendar.YEAR, 1970); 337 cal.set(Calendar.MONTH, 0); 338 cal.set(Calendar.DATE, 1); 339 cal.set(Calendar.MILLISECOND, 0); 340 } 341 342 345 private static synchronized void resetToday(long millis) { 346 347 today.setTimeInMillis(millis); 350 351 357 358 resetToDate(today); 360 361 currentDate = new Date (getTimeInMillis(today)); 362 } 363 364 371 private static void setTimeInMillis(Calendar cal, long millis) { 372 373 cal.setTimeInMillis(millis); 376 377 385 386 } 388 389 public static long getTimeInMillis(java.util.Date dt, Calendar source, 390 Calendar target) { 391 392 if (source == null) { 393 source = tempCalDefault; 394 } 395 396 if (target == null) { 397 target = tempCalDefault; 398 } 399 400 synchronized (tempCal) { 401 tempCal.setTimeZone(source.getTimeZone()); 402 tempCal.setTime(dt); 403 tempCal.setTimeZone(target.getTimeZone()); 404 405 return getTimeInMillis(tempCal); 406 } 407 } 408 409 416 public static long getTimeInMillis(Calendar cal) { 417 418 return (cal.getTimeInMillis()); 421 422 427 428 } 430 431 public static long getNormalisedTime(long t) { 432 433 synchronized (tempCalDefault) { 434 setTimeInMillis(tempCalDefault, t); 435 resetToTime(tempCalDefault); 436 437 return getTimeInMillis(tempCalDefault); 438 } 439 } 440 441 public static Time getNormalisedTime(Time t) { 442 return new Time (getNormalisedTime(t.getTime())); 443 } 444 445 public static Time getNormalisedTime(Timestamp ts) { 446 return new Time (getNormalisedTime(ts.getTime())); 447 } 448 449 public static long getNormalisedDate(long d) { 450 451 synchronized (tempCalDefault) { 452 setTimeInMillis(tempCalDefault, d); 453 resetToDate(tempCalDefault); 454 455 return getTimeInMillis(tempCalDefault); 456 } 457 } 458 459 public static Date getNormalisedDate(Timestamp ts) { 460 461 synchronized (tempCalDefault) { 462 setTimeInMillis(tempCalDefault, ts.getTime()); 463 resetToDate(tempCalDefault); 464 465 long value = getTimeInMillis(tempCalDefault); 466 467 return new Date (value); 468 } 469 } 470 471 public static Date getNormalisedDate(Date d) { 472 473 synchronized (tempCalDefault) { 474 setTimeInMillis(tempCalDefault, d.getTime()); 475 resetToDate(tempCalDefault); 476 477 long value = getTimeInMillis(tempCalDefault); 478 479 return new Date (value); 480 } 481 } 482 483 public static Timestamp getNormalisedTimestamp(Time t) { 484 485 synchronized (tempCalDefault) { 486 setTimeInMillis(tempCalDefault, System.currentTimeMillis()); 487 resetToDate(tempCalDefault); 488 489 long value = getTimeInMillis(tempCalDefault) + t.getTime(); 490 491 return new Timestamp (value); 492 } 493 } 494 495 public static Timestamp getNormalisedTimestamp(Date d) { 496 497 synchronized (tempCalDefault) { 498 setTimeInMillis(tempCalDefault, d.getTime()); 499 resetToDate(tempCalDefault); 500 501 long value = getTimeInMillis(tempCalDefault); 502 503 return new Timestamp (value); 504 } 505 } 506 507 513 static int getDateTimePart(java.util.Date d, int part) { 514 515 synchronized (tempCalDefault) { 516 tempCalDefault.setTime(d); 517 518 return tempCalDefault.get(part); 519 } 520 } 521 522 private static final char[][] dateTokens = { 523 { 524 'R', 'R', 'R', 'R' 525 }, { 526 'I', 'Y', 'Y', 'Y' 527 }, { 528 'Y', 'Y', 'Y', 'Y' 529 }, { 530 'I', 'Y' 531 }, { 532 'Y', 'Y' 533 }, { 534 'B', 'C' 535 }, { 536 'B', '.', 'C', '.' 537 }, { 538 'A', 'D' 539 }, { 540 'A', '.', 'D', '.' 541 }, { 542 'M', 'O', 'N' 543 }, { 544 'M', 'O', 'N', 'T', 'H' 545 }, { 'D' }, { 546 'I', 'W' 547 }, { 548 'D', 'D' 549 }, { 550 'D', 'D', 'D' 551 }, { 552 'H', 'H', '2', '4' 553 }, { 554 'H', 'H', '1', '2' 555 }, { 556 'H', 'H' 557 }, { 558 'M', 'I', 559 }, { 560 'S', 'S' 561 }, { 562 'A', 'M' 563 }, { 564 'P', 'M', 565 }, { 566 'A', '.', 'M', '.' 567 }, { 568 'P', '.', 'M', '.' 569 } 570 }; 571 private static final String [] javaDateTokens = { 572 "yyyy", "yyyy", "yyyy", "yy", "yy", "G", "G", "G", "G", "MMM", 573 "MMMMM", "E", "w", "dd", "D", "k", "K", "K", "mm", "ss", "aaa", "aaa", 574 "aaa", "aaa" 575 }; 576 577 578 public static final char e = 0xffff; 579 580 585 public static String toJavaDatePattern(String format) { 586 587 int len = format.length(); 588 char ch; 589 StringBuffer pattern = new StringBuffer (len); 590 Tokenizer tokenizer = new Tokenizer(); 591 592 for (int i = 0; i <= len; i++) { 593 ch = (i == len) ? e 594 : format.charAt(i); 595 596 if (!tokenizer.next(ch, dateTokens)) { 597 int index = tokenizer.getLastMatch(); 598 599 if (index >= 0) { 600 pattern.setLength(pattern.length() - tokenizer.length()); 601 pattern.append(javaDateTokens[index]); 602 } 603 604 tokenizer.reset(); 605 606 if (tokenizer.isConsumed()) { 607 continue; 608 } 609 } 610 611 pattern.append(ch); 612 } 613 614 pattern.setLength(pattern.length() - 1); 615 616 return pattern.toString(); 617 } 618 619 622 static class Tokenizer { 623 624 private int last; 625 private int offset; 626 private long state; 627 private boolean consumed; 628 629 public Tokenizer() { 630 reset(); 631 } 632 633 637 public void reset() { 638 639 last = -1; 640 offset = -1; 641 state = 0; 642 } 643 644 648 public int length() { 649 return offset; 650 } 651 652 656 public int getLastMatch() { 657 return last; 658 } 659 660 664 public boolean isConsumed() { 665 return consumed; 666 } 667 668 673 private boolean isZeroBit(int bit) { 674 return (state & (1L << bit)) == 0; 675 } 676 677 681 private void setBit(int bit) { 682 state |= (1L << bit); 683 } 684 685 691 public boolean next(char ch, char[][] tokens) { 692 693 int index = ++offset; 695 int len = offset + 1; 696 int left = 0; 697 698 consumed = false; 699 700 for (int i = tokens.length; --i >= 0; ) { 701 if (isZeroBit(i)) { 702 if (tokens[i][index] == ch) { 703 consumed = true; 704 705 if (tokens[i].length == len) { 706 setBit(i); 707 708 last = i; 709 } else { 710 ++left; 711 } 712 } else { 713 setBit(i); 714 } 715 } 716 } 717 718 return left > 0; 719 } 720 } 721 } 722 | Popular Tags |