⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqltimestamp.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        {            SQLDate sqlDate = (SQLDate) date;            encodedDate = sqlDate.getEncodedDate();        }        else        {            cal = new GregorianCalendar();            encodedDate = computeEncodedDate( date.getDate( cal), cal);        }        if( time instanceof SQLTime)        {            SQLTime sqlTime = (SQLTime) time;            encodedTime = sqlTime.getEncodedTime();        }        else        {            if( cal == null)                cal = new GregorianCalendar();            encodedTime = computeEncodedTime( time.getTime( cal), cal);        }    }    /**     * Construct a timestamp from a string. The allowed formats are:     *<ol>     *<li>JDBC escape: yyyy-mm-dd hh:mm:ss[.fffff]     *<li>IBM: yyyy-mm-dd-hh.mm.ss[.nnnnnn]     *</ol>     * The format is specified by a parameter to the constructor. Leading zeroes may be omitted from the month, day,     * and hour part of the timestamp. The microsecond part may be omitted or truncated.     */    public SQLTimestamp( String timestampStr, boolean isJDBCEscape, LocaleFinder localeFinder)        throws StandardException    {        parseTimestamp( timestampStr, isJDBCEscape,localeFinder, (Calendar) null);    }        /**     * Construct a timestamp from a string. The allowed formats are:     *<ol>     *<li>JDBC escape: yyyy-mm-dd hh:mm:ss[.fffff]     *<li>IBM: yyyy-mm-dd-hh.mm.ss[.nnnnnn]     *</ol>     * The format is specified by a parameter to the constructor. Leading zeroes may be omitted from the month, day,     * and hour part of the timestamp. The microsecond part may be omitted or truncated.     */    public SQLTimestamp( String timestampStr, boolean isJDBCEscape, LocaleFinder localeFinder, Calendar cal)        throws StandardException    {        parseTimestamp( timestampStr, isJDBCEscape, localeFinder, cal);    }    static final char DATE_SEPARATOR = '-';    private static final char[] DATE_SEPARATORS = { DATE_SEPARATOR};    private static final char IBM_DATE_TIME_SEPARATOR = '-';    private static final char ODBC_DATE_TIME_SEPARATOR = ' ';    private static final char[] DATE_TIME_SEPARATORS = {IBM_DATE_TIME_SEPARATOR, ODBC_DATE_TIME_SEPARATOR};    private static final char[] DATE_TIME_SEPARATORS_OR_END    = {IBM_DATE_TIME_SEPARATOR, ODBC_DATE_TIME_SEPARATOR, (char) 0};    private static final char IBM_TIME_SEPARATOR = '.';    private static final char ODBC_TIME_SEPARATOR = ':';    private static final char[] TIME_SEPARATORS = {IBM_TIME_SEPARATOR, ODBC_TIME_SEPARATOR};    private static final char[] TIME_SEPARATORS_OR_END = {IBM_TIME_SEPARATOR, ODBC_TIME_SEPARATOR, (char) 0};    private static final char[] END_OF_STRING = {(char) 0};        private void parseTimestamp( String timestampStr, boolean isJDBCEscape, LocaleFinder localeFinder, Calendar cal)        throws StandardException    {        StandardException thrownSE = null;        DateTimeParser parser = new DateTimeParser( timestampStr);        try        {            int[] dateTimeNano = parseDateOrTimestamp( parser, true);            encodedDate = dateTimeNano[0];            encodedTime = dateTimeNano[1];            nanos = dateTimeNano[2];            valueString = parser.getTrimmedString();            return;        }        catch( StandardException se)        {            thrownSE = se;        }        // see if it is a localized timestamp        try        {            timestampStr = StringUtil.trimTrailing( timestampStr);            int[] dateAndTime = parseLocalTimestamp( timestampStr, localeFinder, cal);            encodedDate = dateAndTime[0];            encodedTime = dateAndTime[1];            valueString = timestampStr;            return;        }        catch( ParseException pe){}        catch( StandardException se){}        if( thrownSE != null)            throw thrownSE;        throw StandardException.newException( SQLState.LANG_DATE_SYNTAX_EXCEPTION);    } // end of parseTimestamp    /**     * Parse a localized timestamp.     *     * @param str the timestamp string, with trailing blanks removed.     * @param localFinder     *     * @return a {encodedDate, encodedTime} array.     *     * @exception ParseException If the string is not a valid timestamp.     */    static int[] parseLocalTimestamp( String str, LocaleFinder localeFinder, Calendar cal)        throws StandardException, ParseException    {        DateFormat timestampFormat = null;        if(localeFinder == null)            timestampFormat = DateFormat.getDateTimeInstance();        else if( cal == null)            timestampFormat = localeFinder.getTimestampFormat();        else            timestampFormat = (DateFormat) localeFinder.getTimestampFormat().clone();        if( cal == null)            cal = new GregorianCalendar();        else            timestampFormat.setCalendar( cal);        java.util.Date date = timestampFormat.parse( str);                    return new int[] { computeEncodedDate( date, cal), computeEncodedTime( date, cal)};    } // end of parseLocalTimestamp    /**     * Parse a timestamp or a date. DB2 allows timestamps to be used as dates or times. So     * date('2004-04-15-16.15.32') is valid, as is date('2004-04-15').     *     * This method does not handle localized timestamps.     *     * @param parser a DateTimeParser initialized with a string.     * @param timeRequired If true then an error will be thrown if the time is missing. If false then the time may     *                     be omitted.     *     * @return {encodedDate, encodedTime, nanosecond} array.     *     * @exception StandardException if the syntax is incorrect for an IBM standard timestamp.     */    static int[] parseDateOrTimestamp( DateTimeParser parser, boolean timeRequired)        throws StandardException    {        int year = parser.parseInt( 4, false, DATE_SEPARATORS, false);        int month = parser.parseInt( 2, true, DATE_SEPARATORS, false);        int day = parser.parseInt( 2, true, timeRequired ? DATE_TIME_SEPARATORS : DATE_TIME_SEPARATORS_OR_END, false);        int hour = 0;        int minute = 0;        int second = 0;        int nano = 0;        if( parser.getCurrentSeparator() != 0)        {            char timeSeparator = (parser.getCurrentSeparator() == ODBC_DATE_TIME_SEPARATOR)              ? ODBC_TIME_SEPARATOR : IBM_TIME_SEPARATOR;            hour = parser.parseInt( 2, true, TIME_SEPARATORS, false);            if( timeSeparator == parser.getCurrentSeparator())            {                minute = parser.parseInt( 2, false, TIME_SEPARATORS, false);                if( timeSeparator == parser.getCurrentSeparator())                {                    second = parser.parseInt( 2, false, TIME_SEPARATORS_OR_END, false);                    if( parser.getCurrentSeparator() == '.')                        nano = parser.parseInt( MAX_FRACTION_DIGITS, true, END_OF_STRING, true)*FRACTION_TO_NANO;                }            }        }        parser.checkEnd();        return new int[] { SQLDate.computeEncodedDate( year, month, day),                           SQLTime.computeEncodedTime( hour,minute,second),                           nano};    } // end of parseDateOrTimestamp	/**	 * @see DataValueDescriptor#setValue	 *	 */	public void setValue(Object theValue) throws StandardException	{		if (theValue == null)		{			setToNull();		}		else if (theValue instanceof Date)		{			this.setValue((Date)theValue, (Calendar) null);		}		else if (theValue instanceof Time)		{			this.setValue((Time)theValue, (Calendar) null);		}		else if (theValue instanceof Timestamp)		{			this.setValue((Timestamp)theValue, (Calendar) null);		}		else		{			genericSetObject(theValue);		}	}	protected void setFrom(DataValueDescriptor theValue) throws StandardException {		if (theValue instanceof SQLTimestamp) {			restoreToNull();			SQLTimestamp tvst = (SQLTimestamp) theValue;			encodedDate = tvst.encodedDate;			encodedTime = tvst.encodedTime;			nanos = tvst.nanos;        }		else        {            Calendar cal = new GregorianCalendar();			setValue(theValue.getTimestamp( cal), cal);        }	}	/**		@see DateTimeDataValue#setValue		When converting from a date to a timestamp, time is set to 00:00:00.0	 */	public void setValue(Date value, Calendar cal) throws StandardException	{		restoreToNull();        if( value != null)        {            if( cal == null)                cal = new GregorianCalendar();            encodedDate = computeEncodedDate(value, cal);        }		/* encodedTime and nanos are already set to zero by restoreToNull() */	}	/**		@see DateTimeDataValue#setValue	 */	public void setValue(Time value, Calendar cal) throws StandardException	{		restoreToNull();		if (value != null)		{			/*			** Create a new timestamp with today's date,			** and 'value' time.  			**			** We create a new calendar to get today's date			*/			Calendar today = GregorianCalendar.getInstance();			encodedDate = SQLDate.computeEncodedDate(today);            if( cal == null)                cal = today;			encodedTime = computeEncodedTime(value, cal);		}	}	/**		@see DateTimeDataValue#setValue	 */	public void setValue(Timestamp value, Calendar cal) 	    throws StandardException	{		restoreToNull();		setNumericTimestamp(value, cal);	}	public void setValue(String theValue)	    throws StandardException	{		restoreToNull();		if (theValue != null)		{            DatabaseContext databaseContext = (DatabaseContext) ContextService.getContext(DatabaseContext.CONTEXT_ID);            parseTimestamp( theValue,                            false,                            (databaseContext == null) ? null : databaseContext.getDatabase(),                            (Calendar) null);		}		/* restoreToNull will have already set the encoded date to 0 (null value) */	}	/*	** SQL Operators	*/	/**	 * @see DateTimeDataValue#getYear	 * 	 * @exception StandardException		Thrown on error	 */	public NumberDataValue getYear(NumberDataValue result)							throws StandardException	{		if (SanityManager.DEBUG)		{			SanityManager.ASSERT(!isNull(), "getYear called on a null");		}		return SQLDate.setSource(SQLDate.getYear(encodedDate), result);	}	/**	 * @see DateTimeDataValue#getMonth	 * 	 * @exception StandardException		Thrown on error	 */	public NumberDataValue getMonth(NumberDataValue result)							throws StandardException	{		if (SanityManager.DEBUG)		{			SanityManager.ASSERT(!isNull(), "getMonth called on a null");		}		return SQLDate.setSource(SQLDate.getMonth(encodedDate), result);	}	/**	 * @see DateTimeDataValue#getDate	 * 	 * @exception StandardException		Thrown on error	 */	public NumberDataValue getDate(NumberDataValue result)							throws StandardException	{		if (SanityManager.DEBUG)		{			SanityManager.ASSERT(!isNull(), "getDate called on a null");		}		return SQLDate.setSource(SQLDate.getDay(encodedDate), result);	}	/**	 * @see DateTimeDataValue#getHours	 * 	 * @exception StandardException		Thrown on error	 */	public NumberDataValue getHours(NumberDataValue result)							throws StandardException	{		if (SanityManager.DEBUG)		{			SanityManager.ASSERT(!isNull(), "getHours called on a null");		}		return SQLDate.setSource(SQLTime.getHour(encodedTime), result);	}	/**	 * @see DateTimeDataValue#getMinutes	 * 	 * @exception StandardException		Thrown on error	 */	public NumberDataValue getMinutes(NumberDataValue result)							throws StandardException	{		if (SanityManager.DEBUG)		{			SanityManager.ASSERT(!isNull(), "getMinute called on a null");		}		return SQLDate.setSource(SQLTime.getMinute(encodedTime), result);	}	/**	 * @see DateTimeDataValue#getSeconds	 * 	 * @exception StandardException		Thrown on error	 */	public NumberDataValue getSeconds(NumberDataValue source)							throws StandardException	{		if (SanityManager.DEBUG)		{			SanityManager.ASSERT(!isNull(), "getSeconds called on a null");			SanityManager.ASSERT(source == null || source instanceof SQLDouble,		"getSeconds for a timestamp was given a source other than a SQLDouble");		}		NumberDataValue result;		if (source != null)			result = source;		else			result = new SQLDouble();		result.setValue((double)(SQLTime.getSecond(encodedTime))				+ ((double)nanos)/1.0e9);		return result;	}	/*	** String display of value	*/	public String toString()	{		if (isNull())		{			return "NULL";		}		else		{			return getTimestamp( (Calendar) null).toString();		}	}	/*	 * Hash code	 */	public int hashCode()	{		if (isNull())		{			return 0;		}				return  encodedDate + encodedTime + nanos; //since 0 is null	}	/** @see DataValueDescriptor#typePrecedence */	public int	typePrecedence()	{		return TypeId.TIMESTAMP_PRECEDENCE;	}	/**	 * Check if the value is null.  encodedDate value of 0 is null	 *	 * @return Whether or not value is logically null.	 */	public final boolean isNull()	{		return (encodedDate == 0);	}	/**	 * Get the value field.  We instantiate the field	 * on demand.	 *	 * @return	The value field.	 */	public Timestamp getTimestamp(java.util.Calendar cal)	{		if (isNull())			return null;        return newTimestamp(cal);    }    protected Timestamp newTimestamp(Calendar currentCal)    {        if( currentCal == null)            currentCal = new GregorianCalendar();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -