📄 sqltime.java
字号:
amPm = parser.parseChoice( AM_PM); break; default: validSyntax = false; } } catch( StandardException se) { validSyntax = false; thrownSE = se; } if( validSyntax) { if( amPm == 0) // AM { if( hour == 12) { if( minute == 0 && second == 0) hour = 24; else hour = 0; } else if( hour > 12) throw StandardException.newException( SQLState.LANG_DATE_RANGE_EXCEPTION); } else if( amPm == 1) // PM { if( hour < 12) hour += 12; else if( hour > 12) throw StandardException.newException( SQLState.LANG_DATE_RANGE_EXCEPTION); } valueString = parser.checkEnd(); encodedTime = computeEncodedTime( hour, minute, second); } else { // See if it is a localized time or timestamp timeStr = StringUtil.trimTrailing( timeStr); DateFormat timeFormat = null; if(localeFinder == null) timeFormat = DateFormat.getTimeInstance(); else if( cal == null) timeFormat = localeFinder.getTimeFormat(); else timeFormat = (DateFormat) localeFinder.getTimeFormat().clone(); if( cal != null) timeFormat.setCalendar( cal); try { encodedTime = computeEncodedTime( timeFormat.parse( timeStr), cal); } catch( ParseException pe) { // Maybe it is a localized timestamp try { encodedTime = SQLTimestamp.parseLocalTimestamp( timeStr, localeFinder, cal)[1]; } catch( ParseException pe2) { if( thrownSE != null) throw thrownSE; throw StandardException.newException( SQLState.LANG_DATE_SYNTAX_EXCEPTION); } } valueString = timeStr; } } // end of parseTime public void setValue(Object theValue) throws StandardException { restoreToNull(); if (theValue != null) { 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 SQLTime) { restoreToNull(); SQLTime tvst = (SQLTime) theValue; encodedTime = tvst.encodedTime; encodedTimeFraction = tvst.encodedTimeFraction; } else { Calendar cal = new GregorianCalendar(); setValue(theValue.getTime( cal), cal); } } /** @see DateTimeDataValue#setValue @exception StandardException thrown on failure. */ public void setValue(Time value, Calendar cal) throws StandardException { restoreToNull(); encodedTime = computeEncodedTime(value, cal); } /** @see DateTimeDataValue#setValue @exception StandardException thrown on failure. */ public void setValue(Timestamp value, Calendar cal) throws StandardException { restoreToNull(); encodedTime = computeEncodedTime(value, cal); } public void setValue(String theValue) throws StandardException { restoreToNull(); if (theValue != null) { DatabaseContext databaseContext = (DatabaseContext) ContextService.getContext(DatabaseContext.CONTEXT_ID); parseTime( theValue, false, (databaseContext == null) ? null : databaseContext.getDatabase(), (Calendar) null); } } /* ** 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 null."); } throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, "getYear", "Time"); } /** * @see DateTimeDataValue#getMonth * * @exception StandardException Thrown on error */ public NumberDataValue getMonth(NumberDataValue result) throws StandardException { if (SanityManager.DEBUG) { SanityManager.ASSERT(!isNull(), "getMonth called on null."); } throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, "getMonth", "Time"); } /** * @see DateTimeDataValue#getDate * * @exception StandardException Thrown on error */ public NumberDataValue getDate(NumberDataValue result) throws StandardException { if (SanityManager.DEBUG) { SanityManager.ASSERT(!isNull(), "getDate called on null."); } throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, "getDate", "Time"); } /** * @see DateTimeDataValue#getHours * * @exception StandardException Thrown on error */ public NumberDataValue getHours(NumberDataValue result) throws StandardException { if (SanityManager.DEBUG) { SanityManager.ASSERT(!isNull(), "getHours called on null"); } return SQLDate.setSource(getHour(encodedTime), result); } /** * @see DateTimeDataValue#getMinutes * * @exception StandardException Thrown on error */ public NumberDataValue getMinutes(NumberDataValue result) throws StandardException { if (SanityManager.DEBUG) { SanityManager.ASSERT(!isNull(), "getMinutes called on null"); } return SQLDate.setSource(getMinute(encodedTime), result); } /** * @see DateTimeDataValue#getSeconds * * @exception StandardException Thrown on error */ public NumberDataValue getSeconds(NumberDataValue result) throws StandardException { if (SanityManager.DEBUG) { SanityManager.ASSERT(!isNull(), "getMinutes called on null"); } return SQLDate.setSource(getSecond(encodedTime), result); } /* ** String display of value */ public String toString() { if (isNull()) { return "NULL"; } else { return getTime( (Calendar) null).toString(); } } /* * Hash code */ public int hashCode() { if (isNull()) { return 0; } // add 1 since 0 represents a valid time return encodedTime + encodedTimeFraction + 1; } /** @see DataValueDescriptor#typePrecedence */ public int typePrecedence() { return TypeId.TIME_PRECEDENCE; } /** * Check if the value is null. * * @return Whether or not value is logically null. */ public final boolean isNull() { return (encodedTime == -1); } /** * Get the time value * Since this is a JDBC object we use the JDBC definition * we use the JDBC definition, see JDBC API Tutorial and Reference * section 47.3.12 * Date is set to Jan. 1, 1970 * * @return The localized time value. */ public Time getTime(java.util.Calendar cal) { if (isNull()) return null; return newTime(cal); } protected Time newTime(java.util.Calendar cal) { if( cal == null) cal = new GregorianCalendar(); cal.set(Calendar.YEAR, 1970); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DATE, 1); cal.set(Calendar.HOUR_OF_DAY, getHour(encodedTime)); cal.set(Calendar.MINUTE, getMinute(encodedTime)); cal.set(Calendar.SECOND, getSecond(encodedTime)); cal.set(Calendar.MILLISECOND, 0); //only 0 fractional seconds currently return new Time(cal.getTime().getTime()); } /** * Get the encoded hour value (may be different than hour value for * current timezone if value encoded in a different timezone) * * @return hour value */ protected static int getHour(int encodedTime) { return (encodedTime >>> 16) & 0xff; } /** * Get the encoded minute value (may be different than the minute value for * current timezone if value encoded in a different timezone) * * @return minute value */ protected static int getMinute(int encodedTime) { return ((encodedTime >>> 8) & 0xff); } /** * Get the encoded second value (may be different than the second value for * current timezone if value encoded in a different timezone) * * @return second value */ protected static int getSecond(int encodedTime) { return (encodedTime & 0xff); } /** * Calculate the encoded time from a Calendar object * encoded time is hour << 16 + min << 8 + sec * this function is also used by SQLTimestamp * * @param calendar with time set * @return encoded time * * @exception StandardException if the time is not in the DB2 range */ static int computeEncodedTime(Calendar cal) throws StandardException { return computeEncodedTime(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND)); } static int computeEncodedTime( int hour, int minute, int second) throws StandardException { if( hour == 24) { if( minute != 0 || second != 0) throw StandardException.newException( SQLState.LANG_DATE_RANGE_EXCEPTION); } else if( hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) throw StandardException.newException( SQLState.LANG_DATE_RANGE_EXCEPTION); return (hour << 16) + (minute << 8) + second; } /** * Convert a time to a JDBC escape format string * * @param hour * @param minute * @param second * @param sb The resulting string is appended to this StringBuffer */ static void timeToString( int hour, int minute, int second, StringBuffer sb) { String hourStr = Integer.toString( hour); String minStr = Integer.toString( minute); String secondStr = Integer.toString( second); if (hourStr.length() == 1) sb.append("0"); sb.append( hourStr); sb.append( JIS_SEPARATOR); if (minStr.length() == 1) sb.append("0"); sb.append(minStr); sb.append( JIS_SEPARATOR); if (secondStr.length() == 1) sb.append("0"); sb.append(secondStr); } // end of timeToString /** * Get the String version from the encodedTime. * * @return string value. */ protected static String encodedTimeToString(int encodedTime) { StringBuffer vstr = new StringBuffer(); timeToString( SQLTime.getHour(encodedTime), SQLTime.getMinute(encodedTime), SQLTime.getSecond(encodedTime), vstr); return vstr.toString(); } // International Support /** * International version of getString(). Overrides getNationalString * in DataType for date, time, and timestamp. * * @exception StandardException Thrown on error */ protected String getNationalString(LocaleFinder localeFinder) throws StandardException { if (isNull()) { return getString(); } return localeFinder.getTimeFormat().format(getTime( (Calendar) null)); } /** * Compute encoded time value * Time is represented by hour << 16 + minute << 8 + seconds */ private int computeEncodedTime(java.util.Date value) throws StandardException { return computeEncodedTime( value, (Calendar) null); } static int computeEncodedTime(java.util.Date value, Calendar currentCal) throws StandardException { if (value == null) return -1; if( currentCal == null) currentCal = new GregorianCalendar(); currentCal.setTime(value); return computeEncodedTime(currentCal); } /** Adding this method to ensure that super class' setInto method doesn't get called * that leads to the violation of JDBC spec( untyped nulls ) when batching is turned on. */ public void setInto(PreparedStatement ps, int position) throws SQLException, StandardException { ps.setTime(position, getTime((Calendar) null)); } /** * Add a number of intervals to a datetime value. Implements the JDBC escape TIMESTAMPADD function. * * @param intervalType One of FRAC_SECOND_INTERVAL, SECOND_INTERVAL, MINUTE_INTERVAL, HOUR_INTERVAL, * DAY_INTERVAL, WEEK_INTERVAL, MONTH_INTERVAL, QUARTER_INTERVAL, or YEAR_INTERVAL * @param intervalCount The number of intervals to add * @param currentDate Used to convert time to timestamp * @param resultHolder If non-null a DateTimeDataValue that can be used to hold the result. If null then * generate a new holder * * @return startTime + intervalCount intervals, as a timestamp * * @exception StandardException */ public DateTimeDataValue timestampAdd( int intervalType, NumberDataValue intervalCount, java.sql.Date currentDate, DateTimeDataValue resultHolder) throws StandardException { return toTimestamp( currentDate).timestampAdd( intervalType, intervalCount, currentDate, resultHolder); } private SQLTimestamp toTimestamp(java.sql.Date currentDate) throws StandardException { return new SQLTimestamp( SQLDate.computeEncodedDate( currentDate, (Calendar) null), getEncodedTime(), 0 /* nanoseconds */); } /** * Finds the difference between two datetime values as a number of intervals. Implements the JDBC * TIMESTAMPDIFF escape function. * * @param intervalType One of FRAC_SECOND_INTERVAL, SECOND_INTERVAL, MINUTE_INTERVAL, HOUR_INTERVAL, * DAY_INTERVAL, WEEK_INTERVAL, MONTH_INTERVAL, QUARTER_INTERVAL, or YEAR_INTERVAL * @param time1 * @param currentDate Used to convert time to timestamp * @param resultHolder If non-null a NumberDataValue that can be used to hold the result. If null then * generate a new holder * * @return the number of intervals by which this datetime is greater than time1 * * @exception StandardException */ public NumberDataValue timestampDiff( int intervalType, DateTimeDataValue time1, java.sql.Date currentDate, NumberDataValue resultHolder) throws StandardException { return toTimestamp( currentDate ).timestampDiff( intervalType, time1, currentDate, resultHolder); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -