KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > DateTime


1 /*
2
3    Derby - Class org.apache.derby.client.am.DateTime
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

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 JavaDoc;
27 import org.apache.derby.client.net.Typdef;
28
29
30 /**
31  * High performance converters from date/time byte encodings to JDBC Date, Time and Timestamp objects.
32  * <p/>
33  * Using this class for direct date/time conversions from bytes offers superior performance over the alternative method
34  * of first constructing a Java String from the encoded bytes, and then using {@link java.sql.Date#valueOf
35  * java.sql.Date.valueOf()}, {@link java.sql.Time#valueOf java.sql.Time.valueOf()} or {@link java.sql.Timestamp#valueOf
36  * java.sql.Timestamp.valueOf()}.
37  * <p/>
38  */

39 public class DateTime {
40
41     // Hide the default constructor
42
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     // *********************************************************
50
// ********** Output converters (byte[] -> class) **********
51
// *********************************************************
52

53     /**
54      * Expected character representation is DERBY string representation of a date,
55      * which is in JIS format: <code> yyyy-mm-dd </code>
56      *
57      * @param buffer
58      * @param offset
59      * @param recyclableDate
60      * @param encoding encoding of buffer data
61      * @return Date translated from buffer with specified encoding
62      * @throws UnsupportedEncodingException
63      */

64     public static final java.sql.Date JavaDoc dateBytesToDate(byte[] buffer,
65                                                       int offset,
66                                                       java.sql.Date JavaDoc recyclableDate,
67                                                       String JavaDoc encoding)
68     throws UnsupportedEncodingException JavaDoc {
69         int year, month, day;
70
71         String JavaDoc date = new String JavaDoc(buffer, offset,
72                 DateTime.dateRepresentationLength,encoding);
73         int yearIndx, monthIndx, dayIndx;
74         if (date.charAt(4) == '-') {
75             // JIS format: yyyy-mm-dd.
76
yearIndx = 0;
77             monthIndx = 5;
78             dayIndx = 8;
79         } else {
80             throw new java.lang.IllegalArgumentException JavaDoc(
81                 SqlException.getMessageUtil().getTextMessage(
82                     SQLState.LANG_FORMAT_EXCEPTION));
83         }
84
85         int zeroBase = ((int) '0');
86         // Character arithmetic is used rather than
87
// the less efficient Integer.parseInt (date.substring()).
88
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 JavaDoc(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     /**
114      * Expected character representation is DERBY string representation of time,
115      * which is in the format: <code> hh.mm.ss </code>
116      * @param buffer
117      * @param offset
118      * @param recyclableTime
119      * @param encoding encoding of buffer
120      * @return Time translated from buffer with specified encoding
121      * @throws UnsupportedEncodingException
122      */

123     public static final java.sql.Time JavaDoc timeBytesToTime(byte[] buffer,
124                                                       int offset,
125                                                       java.sql.Time JavaDoc recyclableTime,
126                                                       String JavaDoc encoding)
127     throws UnsupportedEncodingException JavaDoc {
128         int hour, minute, second;
129
130         String JavaDoc time = new String JavaDoc(buffer, offset,
131                 DateTime.timeRepresentationLength, encoding);
132         int zeroBase = ((int) '0');
133
134         // compute hour.
135
hour =
136                 10 * (((int) time.charAt(0)) - zeroBase) +
137                 (((int) time.charAt(1)) - zeroBase);
138         // compute minute.
139
minute =
140                 10 * (((int) time.charAt(3)) - zeroBase) +
141                 (((int) time.charAt(4)) - zeroBase);
142         // compute second.
143
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 JavaDoc(hour, minute, second);
149         } else {
150             recyclableTime.setHours(hour);
151             recyclableTime.setMinutes(minute);
152             recyclableTime.setSeconds(second);
153             return recyclableTime;
154         }
155     }
156
157     /**
158      * Expected character representation is DERBY string representation of a timestamp:
159      * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>.
160      *
161      * @param buffer
162      * @param offset
163      * @param recyclableTimestamp
164      * @param encoding encoding of buffer
165      * @return TimeStamp translated from buffer with specified encoding
166      * @throws UnsupportedEncodingException
167      */

168     public static final java.sql.Timestamp JavaDoc timestampBytesToTimestamp(byte[] buffer,
169                                                                      int offset,
170                                                                      java.sql.Timestamp JavaDoc recyclableTimestamp,
171                                                                      String JavaDoc encoding)
172     throws UnsupportedEncodingException JavaDoc
173     {
174         int year, month, day, hour, minute, second, fraction;
175         String JavaDoc timestamp = new String JavaDoc(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 JavaDoc(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     // ********************************************************
225
// ********** Input converters (class -> byte[]) **********
226
// ********************************************************
227

228     /**
229      * Date is converted to a char representation in JDBC date format: <code>yyyy-mm-dd</code> date format
230      * and then converted to bytes using UTF8 encoding
231      * @param buffer bytes in UTF8 encoding of the date
232      * @param offset write into the buffer from this offset
233      * @param date date value
234      * @return DateTime.dateRepresentationLength. This is the fixed length in
235      * bytes taken to represent the date value
236      * @throws SqlException
237      * @throws UnsupportedEncodingException if UTF8 Encoding is not supported
238      */

239     public static final int dateToDateBytes(byte[] buffer,
240                                             int offset,
241                                             java.sql.Date JavaDoc date)
242     throws SqlException,UnsupportedEncodingException JavaDoc {
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 JavaDoc(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         // Network server expects to read the date parameter value bytes with
266
// UTF-8 encoding. Reference - DERBY-1127
267
// see DRDAConnThread.readAndSetParams
268
byte[] dateBytes = (new String JavaDoc(dateChars)).getBytes(Typdef.UTF8ENCODING);
269         System.arraycopy(dateBytes, 0, buffer, offset, DateTime.dateRepresentationLength);
270
271         return DateTime.dateRepresentationLength;
272     }
273
274     /**
275      * java.sql.Time is converted to character representation which is in JDBC time escape
276      * format: <code>hh:mm:ss</code>, which is the same as JIS time format in DERBY string
277      * representation of a time. The char representation is converted to bytes using UTF8
278      * encoding.
279      * @param buffer bytes in UTF8 encoding of the time
280      * @param offset write into the buffer from this offset
281      * @param time java.sql.Time value
282      * @return DateTime.timeRepresentationLength. This is the fixed length in
283      * bytes taken to represent the time value
284      * @throws UnsupportedEncodingException
285      */

286     public static final int timeToTimeBytes(byte[] buffer,
287                                             int offset,
288                                             java.sql.Time JavaDoc time)
289     throws UnsupportedEncodingException JavaDoc {
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         // Network server expects to read the time parameter value bytes with
306
// UTF-8 encoding. Reference - DERBY-1127
307
// see DRDAConnThread.readAndSetParams
308
byte[] timeBytes = (new String JavaDoc(timeChars)).getBytes(Typdef.UTF8ENCODING);
309         System.arraycopy(timeBytes, 0, buffer, offset, DateTime.timeRepresentationLength);
310
311         return DateTime.timeRepresentationLength;
312     }
313
314     /**
315      * java.sql.Timestamp is converted to a character representation which is in DERBY string
316      * representation of a timestamp: <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>.
317      * and then converted to bytes using UTF8 encoding
318      * @param buffer bytes in UTF8 encoding of the timestamp
319      * @param offset write into the buffer from this offset
320      * @param timestamp timestamp value
321      * @return DateTime.timestampRepresentationLength. This is the fixed
322      * length in bytes, taken to represent the timestamp value
323      * @throws SqlException
324      * @throws UnsupportedEncodingException
325      */

326     public static final int timestampToTimestampBytes(byte[] buffer,
327                                                       int offset,
328                                                       java.sql.Timestamp JavaDoc timestamp)
329     throws SqlException,UnsupportedEncodingException JavaDoc {
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 JavaDoc(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         // Network server expects to read the timestamp parameter value bytes with
373
// UTF-8 encoding. Reference - DERBY-1127
374
// see DRDAConnThread.readAndSetParams
375
byte[] timestampBytes = (new String JavaDoc(timestampChars)).getBytes(Typdef.UTF8ENCODING);
376         System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength);
377
378         return DateTime.timestampRepresentationLength;
379     }
380
381     // *********************************************************
382
// ******* CROSS output converters (byte[] -> class) *******
383
// *********************************************************
384

385     
386     /**
387      * Expected character representation is DERBY string representation of a date
388      * which is in JIS format: <code> yyyy-mm-dd </code>
389      *
390      * @param buffer
391      * @param offset
392      * @param recyclableTimestamp
393      * @param encoding encoding of buffer
394      * @return Timestamp translated from buffer with specified encoding
395      * @throws UnsupportedEncodingException
396      */

397     public static final java.sql.Timestamp JavaDoc dateBytesToTimestamp(byte[] buffer,
398                                                                 int offset,
399                                                                 java.sql.Timestamp JavaDoc recyclableTimestamp,
400                                                                 String JavaDoc encoding)
401     throws UnsupportedEncodingException JavaDoc {
402         int year, month, day;
403
404         String JavaDoc date = new String JavaDoc(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         // Character arithmetic is used rather than
414
// the less efficient Integer.parseInt (date.substring()).
415
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 JavaDoc(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     /**
445      * Expected character representation is DERBY string representation of time
446      * which is in the format: <code> hh.mm.ss </code>
447      *
448      * @param buffer
449      * @param offset
450      * @param recyclableTimestamp
451      * @param encoding encoding of buffer
452      * @return Timestamp translated from buffer with specified encoding
453      * @throws UnsupportedEncodingException
454      *
455      */

456     public static final java.sql.Timestamp JavaDoc timeBytesToTimestamp(byte[] buffer,
457                                                                 int offset,
458                                                                 java.sql.Timestamp JavaDoc recyclableTimestamp,
459                                                                 String JavaDoc encoding)
460     throws UnsupportedEncodingException JavaDoc {
461         int hour, minute, second;
462
463         String JavaDoc time = new String JavaDoc(buffer, offset,
464                 DateTime.timeRepresentationLength, encoding);
465         int zeroBase = ((int) '0');
466
467         // compute hour.
468
hour =
469                 10 * (((int) time.charAt(0)) - zeroBase) +
470                 (((int) time.charAt(1)) - zeroBase);
471         // compute minute.
472
minute =
473                 10 * (((int) time.charAt(3)) - zeroBase) +
474                 (((int) time.charAt(4)) - zeroBase);
475         // compute second JIS format: hh:mm:ss.
476
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 JavaDoc(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     /**
496      * Expected character representation is DERBY string representation of a timestamp:
497      * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>.
498      *
499      * @param buffer
500      * @param offset
501      * @param recyclableDate
502      * @param encoding encoding of buffer
503      * @return Date translated from buffer with specified encoding
504      * @throws UnsupportedEncodingException
505      */

506     public static final java.sql.Date JavaDoc timestampBytesToDate(byte[] buffer,
507                                                            int offset,
508                                                            java.sql.Date JavaDoc recyclableDate,
509                                                            String JavaDoc encoding)
510     throws UnsupportedEncodingException JavaDoc
511      {
512         int year, month, day;
513
514         String JavaDoc timestamp = new String JavaDoc(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 JavaDoc(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     /**
544      * Expected character representation is DERBY string representation of a timestamp:
545      * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code>.
546      *
547      * @param buffer
548      * @param offset
549      * @param recyclableTime
550      * @param encoding encoding of buffer
551      * @return Time translated from buffer with specified Encoding
552      * @throws UnsupportedEncodingException
553      */

554     public static final java.sql.Time JavaDoc timestampBytesToTime(byte[] buffer,
555                                                            int offset,
556                                                            java.sql.Time JavaDoc recyclableTime,
557                                                            String JavaDoc encoding)
558     throws UnsupportedEncodingException JavaDoc {
559         int hour, minute, second;
560
561         String JavaDoc timestamp = new String JavaDoc(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 JavaDoc(hour, minute, second);
577         } else {
578             recyclableTime.setYear(hour);
579             recyclableTime.setMonth(minute);
580             recyclableTime.setDate(second);
581             return recyclableTime;
582         }
583     }
584
585     // *********************************************************
586
// ******* CROSS input converters (class -> byte[]) ********
587
// *********************************************************
588

589     /**
590      * java.sql.Timestamp is converted to character representation that is in JDBC date escape
591      * format: <code>yyyy-mm-dd</code>, which is the same as JIS date format in DERBY string representation of a date.
592      * and then converted to bytes using UTF8 encoding.
593      * @param buffer
594      * @param offset write into the buffer from this offset
595      * @param timestamp timestamp value
596      * @return DateTime.dateRepresentationLength. This is the fixed length
597      * in bytes, that is taken to represent the timestamp value as a date.
598      * @throws SqlException
599      * @throws UnsupportedEncodingException
600      */

601     public static final int timestampToDateBytes(byte[] buffer,
602                                                  int offset,
603                                                  java.sql.Timestamp JavaDoc timestamp)
604     throws SqlException,UnsupportedEncodingException JavaDoc {
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 JavaDoc(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         // Network server expects to read the date parameter value bytes with
627
// UTF-8 encoding. Reference - DERBY-1127
628
// see DRDAConnThread.readAndSetParams
629
byte[] dateBytes = (new String JavaDoc(dateChars)).getBytes(Typdef.UTF8ENCODING);
630         System.arraycopy(dateBytes, 0, buffer, offset, DateTime.dateRepresentationLength);
631
632         return DateTime.dateRepresentationLength;
633     }
634
635     /**
636      * java.sql.Timestamp is converted to character representation in JDBC time escape format:
637      * <code>hh:mm:ss</code>, which is the same as
638      * JIS time format in DERBY string representation of a time. The char representation is
639      * then converted to bytes using UTF8 encoding and written out into the buffer
640      * @param buffer
641      * @param offset write into the buffer from this offset
642      * @param timestamp timestamp value
643      * @return DateTime.timeRepresentationLength. This is the fixed length
644      * in bytes taken to represent the timestamp value as Time.
645      * @throws UnsupportedEncodingException
646      */

647     public static final int timestampToTimeBytes(byte[] buffer,
648                                                  int offset,
649                                                  java.sql.Timestamp JavaDoc timestamp)
650         throws UnsupportedEncodingException JavaDoc {
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         // Network server expects to read the time parameter value bytes with
667
// UTF-8 encoding. Reference - DERBY-1127
668
// see DRDAConnThread.readAndSetParams
669
byte[] timeBytes = (new String JavaDoc(timeChars)).getBytes(Typdef.UTF8ENCODING);
670         System.arraycopy(timeBytes, 0, buffer, offset, DateTime.timeRepresentationLength);
671
672         return DateTime.timeRepresentationLength;
673     }
674
675     /**
676      * java.sql.Date is converted to character representation that is in DERBY string
677      * representation of a timestamp:<code>yyyy-mm-dd-hh.mm.ss.ffffff</code> and then
678      * converted to bytes using UTF8 encoding and written out to the buffer
679      * @param buffer
680      * @param offset offset in buffer to start writing to
681      * @param date date value
682      * @return DateTime.timestampRepresentationLength. This is the fixed length
683      * in bytes, taken to represent the timestamp value.
684      * @throws SqlException
685      * @throws UnsupportedEncodingException
686      */

687     public static final int dateToTimestampBytes(byte[] buffer,
688                                                  int offset,
689                                                  java.sql.Date JavaDoc date)
690     throws SqlException, UnsupportedEncodingException JavaDoc {
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 JavaDoc(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         // Network server expects to read the timestamp parameter value bytes with
730
// UTF-8 encoding. Reference - DERBY-1127
731
// see DRDAConnThread.readAndSetParams
732
byte[] timestampBytes = (new String JavaDoc(timestampChars)).getBytes(Typdef.UTF8ENCODING);
733         System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength);
734
735         return DateTime.timestampRepresentationLength;
736     }
737
738     /**
739      * java.sql.Time is converted to a character representation that is in DERBY string representation of a timestamp:
740      * <code>yyyy-mm-dd-hh.mm.ss.ffffff</code> and converted to bytes using UTF8 encoding
741      * @param buffer
742      * @param offset offset in buffer to start writing to
743      * @param time time value
744      * @return DateTime.timestampRepresentationLength which is the fixed length
745      * taken up by the conversion of time to timestamp in bytes
746      * @throws UnsupportedEncodingException
747      */

748     public static final int timeToTimestampBytes(byte[] buffer,
749                                                  int offset,
750                                                  java.sql.Time JavaDoc time)
751     throws UnsupportedEncodingException JavaDoc {
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         // Network server expects to read the timestamp parameter value bytes with
786
// UTF-8 encoding. Reference - DERBY-1127
787
// see DRDAConnThread.readAndSetParams for TIMESTAMP
788
byte[] timestampBytes = (new String JavaDoc(timestampChars)).getBytes(Typdef.UTF8ENCODING);
789         System.arraycopy(timestampBytes, 0, buffer, offset, DateTime.timestampRepresentationLength);
790
791         return DateTime.timestampRepresentationLength;
792     }
793 }
794
795
Popular Tags