home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Timestamp.java < prev    next >
Text File  |  1997-05-20  |  9KB  |  325 lines

  1. /*
  2.  * @(#)Timestamp.java    1.12 96/11/24
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.sql;
  24.  
  25. /**
  26.  * <P>This class is a thin wrapper around java.util.Date that allows
  27.  * JDBC to identify this as a SQL TIMESTAMP value. It adds the ability
  28.  * to hold the SQL TIMESTAMP nanos value and provides formatting and
  29.  * parsing operations to support the JDBC escape syntax for timestamp
  30.  * values.
  31.  *
  32.  * <P><B>Note:</B> This type is a composite of a java.util.Date and a
  33.  * separate nanos value. Only integral seconds are stored in the
  34.  * java.util.Date component. The fractional seconds - the nanos - are
  35.  * separate. The getTime method will only return integral seconds. If
  36.  * a time value that includes the fractional seconds is desired you
  37.  * must convert nanos to milliseconds (nanos/1000000) and add this to
  38.  * the getTime value. Also note that the hashcode() method uses the
  39.  * underlying java.util.Data implementation and therefore does not
  40.  * include nanos in its computation.  
  41.  */
  42. public class Timestamp extends java.util.Date {
  43.  
  44.     /**
  45.      * Construct a Timestamp Object
  46.      *
  47.      * @param year year-1900
  48.      * @param month 0 to 11 
  49.      * @param day 1 to 31
  50.      * @param hour 0 to 23
  51.      * @param minute 0 to 59
  52.      * @param second 0 to 59
  53.      * @param nano 0 to 999,999,999
  54.      */
  55.     public Timestamp(int year, int month, int date, 
  56.              int hour, int minute, int second, int nano) {
  57.     super(year, month, date, hour, minute, second);
  58.     if (nano > 999999999 || nano < 0) {
  59.         throw new IllegalArgumentException("nanos > 999999999 or < 0");
  60.     }
  61.     nanos = nano;
  62.     }
  63.  
  64.     /**
  65.      * Construct a Timestamp using a milliseconds time value. The
  66.      * integral seconds are stored in the underlying date value; the
  67.      * fractional seconds are stored in the nanos value.
  68.      *
  69.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT 
  70.      */
  71.     public Timestamp(long time) {
  72.     super((time/1000)*1000);
  73.     nanos = (int)((time%1000) * 1000000);
  74.     if (nanos < 0) {
  75.         nanos = 1000000000 + nanos;        
  76.         setTime(((time/1000)-1)*1000);
  77.     }
  78.     }
  79.  
  80.     private int nanos;
  81.  
  82.     /**
  83.      * Convert a string in JDBC timestamp escape format to a Timestamp value
  84.      *
  85.      * @param s timestamp in format "yyyy-mm-dd hh:mm:ss.fffffffff"
  86.      * @return corresponding Timestamp
  87.      */
  88.     public static Timestamp valueOf(String s) {
  89.     String date_s;
  90.     String time_s;
  91.     String nanos_s;
  92.     int year;
  93.     int month;
  94.     int day;
  95.     int hour;
  96.     int minute;
  97.     int second;
  98.     int a_nanos = 0;
  99.     int firstDash;
  100.     int secondDash;
  101.     int dividingSpace;
  102.     int firstColon = 0;
  103.     int secondColon = 0;
  104.     int period = 0;
  105.     String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff";
  106.     String zeros = "000000000";
  107.  
  108.     if (s == null) throw new java.lang.IllegalArgumentException("null string");
  109.  
  110.     // Split the string into date and time components
  111.     s = s.trim();
  112.     dividingSpace = s.indexOf(' ');
  113.     if (dividingSpace > 0) {
  114.         date_s = s.substring(0,dividingSpace);
  115.         time_s = s.substring(dividingSpace+1);
  116.     } else {
  117.         throw new java.lang.IllegalArgumentException(formatError);
  118.     }
  119.  
  120.  
  121.     // Parse the date
  122.     firstDash = date_s.indexOf('-');
  123.     secondDash = date_s.indexOf('-', firstDash+1);
  124.  
  125.     // Parse the time
  126.     if (time_s == null) 
  127.         throw new java.lang.IllegalArgumentException(formatError);
  128.     firstColon = time_s.indexOf(':');
  129.     secondColon = time_s.indexOf(':', firstColon+1);
  130.     period = time_s.indexOf('.', secondColon+1);
  131.  
  132.     // Convert the date
  133.     if ((firstDash > 0) & (secondDash > 0) & 
  134.         (secondDash < date_s.length()-1)) {
  135.         year = Integer.parseInt(date_s.substring(0, firstDash)) - 1900;
  136.         month = 
  137.         Integer.parseInt(date_s.substring
  138.                  (firstDash+1, secondDash)) - 1;
  139.         day = Integer.parseInt(date_s.substring(secondDash+1));
  140.     } else {        
  141.         throw new java.lang.IllegalArgumentException(formatError);
  142.     }
  143.  
  144.     // Convert the time; default missing nanos
  145.     if ((firstColon > 0) & (secondColon > 0) & 
  146.         (secondColon < time_s.length()-1)) {
  147.         hour = Integer.parseInt(time_s.substring(0, firstColon));
  148.         minute = 
  149.         Integer.parseInt(time_s.substring(firstColon+1, secondColon));
  150.         if ((period > 0) & (period < time_s.length()-1)) {
  151.         second = 
  152.             Integer.parseInt(time_s.substring(secondColon+1, period));
  153.         nanos_s = time_s.substring(period+1);
  154.         if (nanos_s.length() > 9) 
  155.             throw new java.lang.IllegalArgumentException(formatError);
  156.         if (!Character.isDigit(nanos_s.charAt(0)))
  157.             throw new java.lang.IllegalArgumentException(formatError);
  158.         nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length());
  159.         a_nanos = Integer.parseInt(nanos_s);
  160.         } else if (period > 0) {
  161.         throw new java.lang.IllegalArgumentException(formatError);
  162.         } else {
  163.         second = Integer.parseInt(time_s.substring(secondColon+1));
  164.         }
  165.     } else {
  166.         throw new java.lang.IllegalArgumentException();
  167.     }
  168.  
  169.     return new Timestamp(year, month, day, hour, minute, second, a_nanos);
  170.     }
  171.  
  172.     /**
  173.      * Format a timestamp in JDBC timestamp escape format
  174.      *
  175.      * @return a String in yyyy-mm-dd hh:mm:ss.fffffffff format
  176.      */
  177.     public String toString () {
  178.     int year = super.getYear() + 1900;
  179.     int month = super.getMonth() + 1;
  180.     int day = super.getDate();
  181.     int hour = super.getHours();
  182.     int minute = super.getMinutes();
  183.     int second = super.getSeconds();
  184.     String yearString;
  185.     String monthString;
  186.     String dayString;
  187.     String hourString;
  188.     String minuteString;
  189.     String secondString;
  190.     String nanosString;
  191.     String zeros = "000000000";
  192.  
  193.         
  194.     yearString = "" + year;
  195.     if (month < 10) {
  196.         monthString = "0" + month;
  197.     } else {        
  198.         monthString = Integer.toString(month);
  199.     }
  200.     if (day < 10) {
  201.         dayString = "0" + day;
  202.     } else {        
  203.         dayString = Integer.toString(day);
  204.     }
  205.     if (hour < 10) {
  206.         hourString = "0" + hour;
  207.     } else {        
  208.         hourString = Integer.toString(hour);
  209.     }
  210.     if (minute < 10) {
  211.         minuteString = "0" + minute;
  212.     } else {        
  213.         minuteString = Integer.toString(minute);
  214.     }
  215.     if (second < 10) {
  216.         secondString = "0" + second;
  217.     } else {        
  218.         secondString = Integer.toString(second);
  219.     }
  220.     if (nanos == 0) {
  221.         nanosString = "0";
  222.     } else {
  223.         nanosString = Integer.toString(nanos);
  224.  
  225.         // Add leading zeros
  226.         nanosString = zeros.substring(0,(9-nanosString.length())) + 
  227.         nanosString;
  228.         
  229.         // Truncate trailing zeros
  230.         char[] nanosChar = new char[nanosString.length()];
  231.         nanosString.getChars(0, nanosString.length(), nanosChar, 0);
  232.         int truncIndex = 8;        
  233.         while (nanosChar[truncIndex] == '0') {
  234.         truncIndex--;
  235.         }
  236.         nanosString = new String(nanosChar,0,truncIndex+1);
  237.     }
  238.     
  239.     return (yearString + "-" + monthString + "-" + dayString + " " + 
  240.         hourString + ":" + minuteString + ":" + secondString + "."
  241.                 + nanosString);
  242.     }
  243.  
  244.     /**
  245.      * Get the Timestamp's nanos value
  246.      *
  247.      * @return the Timestamp's fractional seconds component
  248.      */
  249.     public int getNanos() {
  250.     return nanos;
  251.     }
  252.  
  253.     /**
  254.      * Set the Timestamp's nanos value
  255.      *
  256.      * @param n the new fractional seconds component
  257.      */
  258.     public void setNanos(int n) {
  259.     if (n > 999999999 || n < 0) {
  260.         throw new IllegalArgumentException("nanos > 999999999 or < 0");
  261.     }
  262.     nanos = n;
  263.     }
  264.  
  265.     /**
  266.      * Test Timestamp values for equality
  267.      *
  268.      * @param ts the Timestamp value to compare with
  269.      */
  270.     public boolean equals(Timestamp ts) {
  271.     if (super.equals(ts)) {
  272.         if (nanos == ts.nanos) {
  273.         return true;
  274.         } else {
  275.         return false;
  276.         }
  277.     } else {
  278.         return false;
  279.     }
  280.     }
  281.  
  282.     /**
  283.      * Is this timestamp earlier than the timestamp argument?
  284.      *
  285.      * @param ts the Timestamp value to compare with
  286.      */
  287.     public boolean before(Timestamp ts) {
  288.     if (super.before(ts)) {
  289.         return true;
  290.     } else {
  291.         if (super.equals(ts)) {
  292.         if (nanos < ts.nanos) {
  293.             return true;
  294.         } else {
  295.             return false;
  296.         }
  297.         } else {
  298.         return false;
  299.         }
  300.     }
  301.     }
  302.  
  303.     /**
  304.      * Is this timestamp later than the timestamp argument?
  305.      *
  306.      * @param ts the Timestamp value to compare with
  307.      */
  308.     public boolean after(Timestamp ts) {
  309.     if (super.after(ts)) {
  310.         return true;
  311.     } else {
  312.         if (super.equals(ts)) {
  313.         if (nanos > ts.nanos) {
  314.             return true;
  315.         } else {
  316.             return false;
  317.         }
  318.         } else {
  319.         return false;
  320.         }
  321.     }
  322.     }
  323. }
  324.  
  325.