1 21 package org.apache.derby.client.am; 22 23 import org.apache.derby.shared.common.i18n.MessageUtil; 24 import org.apache.derby.shared.common.reference.SQLState; 25 26 import java.io.UnsupportedEncodingException ; 27 import org.apache.derby.client.net.Typdef; 28 29 30 39 public class DateTime { 40 41 private DateTime() { 43 } 44 45 private static final int dateRepresentationLength = 10; 46 private static final int timeRepresentationLength = 8; 47 private static final int timestampRepresentationLength = 26; 48 49 53 64 public static final java.sql.Date dateBytesToDate(byte[] buffer, 65 int offset, 66 java.sql.Date recyclableDate, 67 String encoding) 68 throws UnsupportedEncodingException { 69 int year, month, day; 70 71 String date = new String (buffer, offset, 72 DateTime.dateRepresentationLength,encoding); 73 int yearIndx, monthIndx, dayIndx; 74 if (date.charAt(4) == '-') { 75 yearIndx = 0; 77 monthIndx = 5; 78 dayIndx = 8; 79 } else { 80 throw new java.lang.IllegalArgumentException ( 81 SqlException.getMessageUtil().getTextMessage( 82 SQLState.LANG_FORMAT_EXCEPTION)); 83 } 84 85 int zeroBase = ((int) '0'); 86 year = 89 1000 * (((int) date.charAt(yearIndx)) - zeroBase) + 90 100 * (((int) date.charAt(yearIndx + 1)) - zeroBase) + 91 10 * (((int) date.charAt(yearIndx + 2)) - zeroBase) + 92 (((int) date.charAt(yearIndx + 3)) - zeroBase) - 93 1900; 94 month = 95 10 * (((int) date.charAt(monthIndx)) - zeroBase) + 96 (((int) date.charAt(monthIndx + 1)) - zeroBase) - 97 1; 98 day = 99 10 * (((int) date.charAt(dayIndx)) - zeroBase) + 100 (((int) date.charAt(dayIndx + 1)) - zeroBase); 101 102 if (recyclableDate == null) { 103 return new java.sql.Date (year, month, day); 104 } else { 105 recyclableDate.setYear(year); 106 recyclableDate.setMonth(month); 107 recyclableDate.setDate(day); 108 return recyclableDate; 109 } 110 } 111 112 113 123 public static final java.sql.Time timeBytesToTime(byte[] buffer, 124 int offset, 125 java.sql.Time recyclableTime, 126 String encoding) 127 throws UnsupportedEncodingException { 128 int hour, minute, second; 129 130 String time = new String (buffer, offset, 131 DateTime.timeRepresentationLength, encoding); 132 int zeroBase = ((int) '0'); 133 134 hour = 136 10 * (((int) time.charAt(0)) - zeroBase) + 137 (((int) time.charAt(1)) - zeroBase); 138 minute = 140 10 * (((int) time.charAt(3)) - zeroBase) + 141 (((int) time.charAt(4)) - zeroBase); 142 second = 144 10 * (((int) time.charAt(6)) - zeroBase) + 145 (((int) time.charAt(7)) - zeroBase); 146 147 if (recyclableTime == null) { 148 return new java.sql.Time (hour, minute, second); 149 } else { 150 recyclableTime.setHours(hour); 151 recyclableTime.setMinutes(minute); 152 recyclableTime.setSeconds(second); 153 return recyclableTime; 154 } 155 } 156 157 168 public static final java.sql.Timestamp timestampBytesToTimestamp(byte[] buffer, 169 int offset, 170 java.sql.Timestamp recyclableTimestamp, 171 String encoding) 172 throws UnsupportedEncodingException 173 { 174 int year, month, day, hour, minute, second, fraction; 175 String timestamp = new String (buffer, offset, 176 DateTime.timestampRepresentationLength,encoding); 177 178 int zeroBase = ((int) '0'); 179 180 year = 181 1000 * (((int) timestamp.charAt(0)) - zeroBase) + 182 100 * (((int) timestamp.charAt(1)) - zeroBase) + 183 10 * (((int) timestamp.charAt(2)) - zeroBase) + 184 (((int) timestamp.charAt(3)) - zeroBase) - 185 1900; 186 month = 187 10 * (((int) timestamp.charAt(5)) - zeroBase) + 188 (((int) timestamp.charAt(6)) - zeroBase) - 189 1; 190 day = 191 10 * (((int) timestamp.charAt(8)) - zeroBase) + 192 (((int) timestamp.charAt(9)) - zeroBase); 193 hour = 194 10 * (((int) timestamp.charAt(11)) - zeroBase) + 195 (((int) timestamp.charAt(12)) - zeroBase); 196 minute = 197 10 * (((int) timestamp.charAt(14)) - zeroBase) + 198 (((int) timestamp.charAt(15)) - zeroBase); 199 second = 200 10 * (((int) timestamp.charAt(17)) - zeroBase) + 201 (((int) timestamp.charAt(18)) - zeroBase); 202 fraction = 203 100000 * (((int) timestamp.charAt(20)) - zeroBase) + 204 10000 * (((int) timestamp.charAt(21)) - zeroBase) + 205 1000 * (((int) timestamp.charAt(22)) - zeroBase) + 206 100 * (((int) timestamp.charAt(23)) - zeroBase) + 207 10 * (((int) timestamp.charAt(24)) - zeroBase) + 208 (((int) timestamp.charAt(25)) - zeroBase); 209 210 if (recyclableTimestamp == null) { 211 return new java.sql.Timestamp (year, month, day, hour, minute, second, fraction * 1000); 212 } else { 213 recyclableTimestamp.setYear(year); 214 recyclableTimestamp.setMonth(month); 215 recyclableTimestamp.setDate(day); 216 recyclableTimestamp.setHours(hour); 217 recyclableTimestamp.setMinutes(minute); 218 recyclableTimestamp.setSeconds(second); 219 recyclableTimestamp.setNanos(fraction * 1000); 220 return recyclableTimestamp; 221 } 222 } 223 224 228 239 public static final int dateToDateBytes(byte[] buffer, 240 int offset, 241 java.sql.Date date) 242 throws SqlException,UnsupportedEncodingException { 243 int year = date.getYear() + 1900; 244 if (year > 9999) { 245 throw new SqlException(null, 246 new ClientMessageId(SQLState.YEAR_EXCEEDS_MAXIMUM), 247 new Integer (year), "9999"); 248 } 249 int month = date.getMonth() + 1; 250 int day = date.getDate(); 251 252 char[] dateChars = new char[DateTime.dateRepresentationLength]; 253 int zeroBase = (int) '0'; 254 dateChars[0] = (char) (year / 1000 + zeroBase); 255 dateChars[1] = (char) ((year % 1000) / 100 + zeroBase); 256 dateChars[2] = (char) ((year % 100) / 10 + zeroBase); 257 dateChars[3] = (char) (year % 10 + +zeroBase); 258 dateChars[4] = '-'; 259 dateChars[5] = (char) (month / 10 + zeroBase); 260 dateChars[6] = (char) (month % 10 + zeroBase); 261 dateChars[7] = '-'; 262 dateChars[8] = (char) (day / 10 + zeroBase); 263 dateChars[9] = (char) (day % 10 + zeroBase); 264 265 byte[] dateBytes = (new String (dateChars)).getBytes(Typdef.UTF8ENCODING); 269 System.arraycopy(dateBytes, 0, buffer, offset, DateTime.dateRepresentationLength); 270 271 return DateTime.dateRepresentationLength; 272 } 273 274 286 public static final int timeToTimeBytes(byte[] buffer, 287 int offset, 288 java.sql.Time time) 289 throws UnsupportedEncodingException { 290 int hour = time.getHours(); 291 int minute = time.getMinutes(); 292 int second = time.getSeconds(); 293 294 char[] timeChars = new char[DateTime.timeRepresentationLength]; 295 int zeroBase = (int) '0'; 296 timeChars[0] = (char) (hour / 10 + zeroBase); 297 timeChars[1] = (char) (hour % 10 + +zeroBase); 298 timeChars[2] = ':'; 299 timeChars[3] = (char) (minute / 10 + zeroBase); 300 timeChars[4] = (char) (minute % 10 + zeroBase); 301 timeChars[5] = ':'; 302 timeChars[6] = (char) (second / 10 + zeroBase); 303 timeChars[7] = (char) (second % 10 + zeroBase); 304 305 byte[] timeBytes = (new String (timeChars)).getBytes(Typdef.UTF8ENCODING); 309 System.arraycopy(timeBytes, 0, buffer, offset, DateTime.timeRepresentationLength); 310 311 return DateTime.timeRepresentationLength; 312 } 313 314 326 public static final int timestampToTimestampBytes(byte[] buffer, 327 int offset, 328 java.sql.Timestamp timestamp) 329 throws SqlException,UnsupportedEncodingException { 330 int year = timestamp.getYear() + 1900; 331 if (year > 9999) { 332 throw new SqlException(null, 333 new ClientMessageId(SQLState.YEAR_EXCEEDS_MAXIMUM), 334 new Integer (year), "9999"); 335 } 336 int month = timestamp.getMonth() + 1; 337 int day = timestamp.getDate(); 338 int hour = timestamp.getHours(); 339 int minute = timestamp.getMinutes(); 340 int second = timestamp.getSeconds(); 341 int microsecond = timestamp.getNanos() / 1000; 342 343 char[] timestampChars = new char[DateTime.timestampRepresentationLength]; 344 int zeroBase = (int) '0'; 345 timestampChars[0] = (char) (year / 1000 + zeroBase); 346 timestampChars[1] = (char) ((year % 1000) / 100 + zeroBase); 347 timestampChars[2] = (char) ((year % 100) / 10 + zeroBase); 348 timestampChars[3] = (char) (year % 10 + +zeroBase); 349 timestampChars[4] = '-'; 350 timestampChars[5] = (char) (month / 10 + zeroBase); 351 timestampChars[6] = (char) (month % 10 + zeroBase); 352 timestampChars[7] = '-'; 353 timestampChars[8] = (char) (day / 10 + zeroBase); 354 timestampChars[9] = (char) (day % 10 + zeroBase); 355 timestampChars[10] = '-'; 356 timestampChars[11] = (char) (hour / 10 + zeroBase); 357 timestampChars[12] = (char) (hour % 10 + zeroBase); 358 timestampChars[13] = '.'; 359 timestampChars[14] = (char) (minute / 10 + zeroBase); 360 timestampChars[15] = (char) (minute % 10 + zeroBase); 361 timestampChars[16] = '.'; 362 timestampChars[17] = (char) (second / 10 + zeroBase); 363 timestampChars[18] = (char) (second % 10 + zeroBase); 364 timestampChars[19] = '.'; 365 timestampChars[20] = (char) (microsecond / 100000 + zeroBase); 366 timestampChars[21] = (char) ((microsecond % 100000) / 10000 + zeroBase); 367 timestampChars[22] = (char) ((microsecond % 10000) / 1000 + zeroBase); 368 timestampChars[23] = (char) ((microsecond % 1000) / 100 + zeroBase); 369 timestampChars[24] = (char) ((microsecond % 100) / 10 + zeroBase); 370 timestampChars[25] = (char) (microsecond % 10 + zeroBase); 371 372 byte[] timestampBytes = (new String (timestampChars)).getBytes(Typdef.UTF8ENCODING); 376 System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); 377 378 return DateTime.timestampRepresentationLength; 379 } 380 381 385 386 397 public static final java.sql.Timestamp dateBytesToTimestamp(byte[] buffer, 398 int offset, 399 java.sql.Timestamp recyclableTimestamp, 400 String encoding) 401 throws UnsupportedEncodingException { 402 int year, month, day; 403 404 String date = new String (buffer, offset, DateTime.dateRepresentationLength, 405 encoding); 406 int yearIndx, monthIndx, dayIndx; 407 408 yearIndx = 0; 409 monthIndx = 5; 410 dayIndx = 8; 411 412 int zeroBase = ((int) '0'); 413 year = 416 1000 * (((int) date.charAt(yearIndx)) - zeroBase) + 417 100 * (((int) date.charAt(yearIndx + 1)) - zeroBase) + 418 10 * (((int) date.charAt(yearIndx + 2)) - zeroBase) + 419 (((int) date.charAt(yearIndx + 3)) - zeroBase) - 420 1900; 421 month = 422 10 * (((int) date.charAt(monthIndx)) - zeroBase) + 423 (((int) date.charAt(monthIndx + 1)) - zeroBase) - 424 1; 425 day = 426 10 * (((int) date.charAt(dayIndx)) - zeroBase) + 427 (((int) date.charAt(dayIndx + 1)) - zeroBase); 428 429 if (recyclableTimestamp == null) { 430 return new java.sql.Timestamp (year, month, day, 0, 0, 0, 0); 431 } else { 432 recyclableTimestamp.setYear(year); 433 recyclableTimestamp.setMonth(month); 434 recyclableTimestamp.setDate(day); 435 recyclableTimestamp.setHours(0); 436 recyclableTimestamp.setMinutes(0); 437 recyclableTimestamp.setSeconds(0); 438 recyclableTimestamp.setNanos(0); 439 return recyclableTimestamp; 440 } 441 } 442 443 444 456 public static final java.sql.Timestamp timeBytesToTimestamp(byte[] buffer, 457 int offset, 458 java.sql.Timestamp recyclableTimestamp, 459 String encoding) 460 throws UnsupportedEncodingException { 461 int hour, minute, second; 462 463 String time = new String (buffer, offset, 464 DateTime.timeRepresentationLength, encoding); 465 int zeroBase = ((int) '0'); 466 467 hour = 469 10 * (((int) time.charAt(0)) - zeroBase) + 470 (((int) time.charAt(1)) - zeroBase); 471 minute = 473 10 * (((int) time.charAt(3)) - zeroBase) + 474 (((int) time.charAt(4)) - zeroBase); 475 second = 477 10 * (((int) time.charAt(6)) - zeroBase) + 478 (((int) time.charAt(7)) - zeroBase); 479 480 if (recyclableTimestamp == null) { 481 return new java.sql.Timestamp (0, 0, 1, hour, minute, second, 0); 482 } else { 483 recyclableTimestamp.setYear(0); 484 recyclableTimestamp.setMonth(0); 485 recyclableTimestamp.setDate(1); 486 recyclableTimestamp.setHours(hour); 487 recyclableTimestamp.setMinutes(minute); 488 recyclableTimestamp.setSeconds(second); 489 recyclableTimestamp.setNanos(0); 490 return recyclableTimestamp; 491 } 492 } 493 494 495 506 public static final java.sql.Date timestampBytesToDate(byte[] buffer, 507 int offset, 508 java.sql.Date recyclableDate, 509 String encoding) 510 throws UnsupportedEncodingException 511 { 512 int year, month, day; 513 514 String timestamp = new String (buffer, offset, 515 DateTime.timestampRepresentationLength, encoding); 516 int zeroBase = ((int) '0'); 517 518 year = 519 1000 * (((int) timestamp.charAt(0)) - zeroBase) + 520 100 * (((int) timestamp.charAt(1)) - zeroBase) + 521 10 * (((int) timestamp.charAt(2)) - zeroBase) + 522 (((int) timestamp.charAt(3)) - zeroBase) - 523 1900; 524 month = 525 10 * (((int) timestamp.charAt(5)) - zeroBase) + 526 (((int) timestamp.charAt(6)) - zeroBase) - 527 1; 528 day = 529 10 * (((int) timestamp.charAt(8)) - zeroBase) + 530 (((int) timestamp.charAt(9)) - zeroBase); 531 532 if (recyclableDate == null) { 533 return new java.sql.Date (year, month, day); 534 } else { 535 recyclableDate.setYear(year); 536 recyclableDate.setMonth(month); 537 recyclableDate.setDate(day); 538 return recyclableDate; 539 } 540 } 541 542 543 554 public static final java.sql.Time timestampBytesToTime(byte[] buffer, 555 int offset, 556 java.sql.Time recyclableTime, 557 String encoding) 558 throws UnsupportedEncodingException { 559 int hour, minute, second; 560 561 String timestamp = new String (buffer, offset, 562 DateTime.timestampRepresentationLength, encoding); 563 int zeroBase = ((int) '0'); 564 565 hour = 566 10 * (((int) timestamp.charAt(11)) - zeroBase) + 567 (((int) timestamp.charAt(12)) - zeroBase); 568 minute = 569 10 * (((int) timestamp.charAt(14)) - zeroBase) + 570 (((int) timestamp.charAt(15)) - zeroBase); 571 second = 572 10 * (((int) timestamp.charAt(17)) - zeroBase) + 573 (((int) timestamp.charAt(18)) - zeroBase); 574 575 if (recyclableTime == null) { 576 return new java.sql.Time (hour, minute, second); 577 } else { 578 recyclableTime.setYear(hour); 579 recyclableTime.setMonth(minute); 580 recyclableTime.setDate(second); 581 return recyclableTime; 582 } 583 } 584 585 589 601 public static final int timestampToDateBytes(byte[] buffer, 602 int offset, 603 java.sql.Timestamp timestamp) 604 throws SqlException,UnsupportedEncodingException { 605 int year = timestamp.getYear() + 1900; 606 if (year > 9999) { 607 throw new SqlException(null, 608 new ClientMessageId(SQLState.YEAR_EXCEEDS_MAXIMUM), 609 new Integer (year), "9999"); 610 } 611 int month = timestamp.getMonth() + 1; 612 int day = timestamp.getDate(); 613 614 char[] dateChars = new char[DateTime.dateRepresentationLength]; 615 int zeroBase = (int) '0'; 616 dateChars[0] = (char) (year / 1000 + zeroBase); 617 dateChars[1] = (char) ((year % 1000) / 100 + zeroBase); 618 dateChars[2] = (char) ((year % 100) / 10 + zeroBase); 619 dateChars[3] = (char) (year % 10 + +zeroBase); 620 dateChars[4] = '-'; 621 dateChars[5] = (char) (month / 10 + zeroBase); 622 dateChars[6] = (char) (month % 10 + zeroBase); 623 dateChars[7] = '-'; 624 dateChars[8] = (char) (day / 10 + zeroBase); 625 dateChars[9] = (char) (day % 10 + zeroBase); 626 byte[] dateBytes = (new String (dateChars)).getBytes(Typdef.UTF8ENCODING); 630 System.arraycopy(dateBytes, 0, buffer, offset, DateTime.dateRepresentationLength); 631 632 return DateTime.dateRepresentationLength; 633 } 634 635 647 public static final int timestampToTimeBytes(byte[] buffer, 648 int offset, 649 java.sql.Timestamp timestamp) 650 throws UnsupportedEncodingException { 651 int hour = timestamp.getHours(); 652 int minute = timestamp.getMinutes(); 653 int second = timestamp.getSeconds(); 654 655 char[] timeChars = new char[DateTime.timeRepresentationLength]; 656 int zeroBase = (int) '0'; 657 timeChars[0] = (char) (hour / 10 + zeroBase); 658 timeChars[1] = (char) (hour % 10 + +zeroBase); 659 timeChars[2] = ':'; 660 timeChars[3] = (char) (minute / 10 + zeroBase); 661 timeChars[4] = (char) (minute % 10 + zeroBase); 662 timeChars[5] = ':'; 663 timeChars[6] = (char) (second / 10 + zeroBase); 664 timeChars[7] = (char) (second % 10 + zeroBase); 665 666 byte[] timeBytes = (new String (timeChars)).getBytes(Typdef.UTF8ENCODING); 670 System.arraycopy(timeBytes, 0, buffer, offset, DateTime.timeRepresentationLength); 671 672 return DateTime.timeRepresentationLength; 673 } 674 675 687 public static final int dateToTimestampBytes(byte[] buffer, 688 int offset, 689 java.sql.Date date) 690 throws SqlException, UnsupportedEncodingException { 691 int year = date.getYear() + 1900; 692 if (year > 9999) { 693 throw new SqlException(null, 694 new ClientMessageId(SQLState.YEAR_EXCEEDS_MAXIMUM), 695 new Integer (year), "9999"); 696 } 697 int month = date.getMonth() + 1; 698 int day = date.getDate(); 699 700 char[] timestampChars = new char[DateTime.timestampRepresentationLength]; 701 int zeroBase = (int) '0'; 702 timestampChars[0] = (char) (year / 1000 + zeroBase); 703 timestampChars[1] = (char) ((year % 1000) / 100 + zeroBase); 704 timestampChars[2] = (char) ((year % 100) / 10 + zeroBase); 705 timestampChars[3] = (char) (year % 10 + +zeroBase); 706 timestampChars[4] = '-'; 707 timestampChars[5] = (char) (month / 10 + zeroBase); 708 timestampChars[6] = (char) (month % 10 + zeroBase); 709 timestampChars[7] = '-'; 710 timestampChars[8] = (char) (day / 10 + zeroBase); 711 timestampChars[9] = (char) (day % 10 + zeroBase); 712 timestampChars[10] = '-'; 713 timestampChars[11] = '0'; 714 timestampChars[12] = '0'; 715 timestampChars[13] = '.'; 716 timestampChars[14] = '0'; 717 timestampChars[15] = '0'; 718 timestampChars[16] = '.'; 719 timestampChars[17] = '0'; 720 timestampChars[18] = '0'; 721 timestampChars[19] = '.'; 722 timestampChars[20] = '0'; 723 timestampChars[21] = '0'; 724 timestampChars[22] = '0'; 725 timestampChars[23] = '0'; 726 timestampChars[24] = '0'; 727 timestampChars[25] = '0'; 728 729 byte[] timestampBytes = (new String (timestampChars)).getBytes(Typdef.UTF8ENCODING); 733 System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); 734 735 return DateTime.timestampRepresentationLength; 736 } 737 738 748 public static final int timeToTimestampBytes(byte[] buffer, 749 int offset, 750 java.sql.Time time) 751 throws UnsupportedEncodingException { 752 int hour = time.getHours(); 753 int minute = time.getMinutes(); 754 int second = time.getSeconds(); 755 756 char[] timestampChars = new char[DateTime.timestampRepresentationLength]; 757 int zeroBase = (int) '0'; 758 timestampChars[0] = '1'; 759 timestampChars[1] = '9'; 760 timestampChars[2] = '0'; 761 timestampChars[3] = '0'; 762 timestampChars[4] = '-'; 763 timestampChars[5] = '0'; 764 timestampChars[6] = '1'; 765 timestampChars[7] = '-'; 766 timestampChars[8] = '0'; 767 timestampChars[9] = '1'; 768 timestampChars[10] = '-'; 769 timestampChars[11] = (char) (hour / 10 + zeroBase); 770 timestampChars[12] = (char) (hour % 10 + zeroBase); 771 timestampChars[13] = '.'; 772 timestampChars[14] = (char) (minute / 10 + zeroBase); 773 timestampChars[15] = (char) (minute % 10 + zeroBase); 774 timestampChars[16] = '.'; 775 timestampChars[17] = (char) (second / 10 + zeroBase); 776 timestampChars[18] = (char) (second % 10 + zeroBase); 777 timestampChars[19] = '.'; 778 timestampChars[20] = '0'; 779 timestampChars[21] = '0'; 780 timestampChars[22] = '0'; 781 timestampChars[23] = '0'; 782 timestampChars[24] = '0'; 783 timestampChars[25] = '0'; 784 785 byte[] timestampBytes = (new String (timestampChars)).getBytes(Typdef.UTF8ENCODING); 789 System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength); 790 791 return DateTime.timestampRepresentationLength; 792 } 793 } 794 795 | Popular Tags |