/** * Convert a date to the specified time zone. * * @param x the date to convert * @param target the calendar with the target timezone * @return the milliseconds in UTC */ public static long convertToLocal(java.util.Date x, Calendar target) { if (target == null) { throw DbException.getInvalidValueException("calendar", null); } target = (Calendar) target.clone(); Calendar local = Calendar.getInstance(); synchronized (local) { local.setTime(x); convertTime(local, target); } return target.getTime().getTime(); }
@Override public Value add(Value v) { ValueTimestamp t = (ValueTimestamp) v.convertTo(TIMESTAMP); long d1 = DateTimeUtils.absoluteDayFromDateValue(dateValue); long d2 = DateTimeUtils.absoluteDayFromDateValue(t.dateValue); return DateTimeUtils.normalizeTimestamp(d1 + d2, timeNanos + t.timeNanos); }
@Override public Date getDate() { return DateTimeUtils.convertDateValueToDate(dateValue); }
/** * Parse a date string. The format is: [+|-]year-month-day * * @param s the string to parse * @param start the parse index start * @param end the parse index end * @return the date value * @throws IllegalArgumentException if there is a problem */ public static long parseDateValue(String s, int start, int end) { if (s.charAt(start) == '+') { // +year start++; } // start at position 1 to support "-year" int s1 = s.indexOf('-', start + 1); int s2 = s.indexOf('-', s1 + 1); if (s1 <= 0 || s2 <= s1) { throw new IllegalArgumentException(s); } int year = Integer.parseInt(s.substring(start, s1)); int month = Integer.parseInt(s.substring(s1 + 1, s2)); int day = Integer.parseInt(s.substring(s2 + 1, end)); if (!isValidDate(year, month, day)) { throw new IllegalArgumentException(year + "-" + month + "-" + day); } return dateValue(year, month, day); }
timeStart = dateEnd + 1; long dateValue = DateTimeUtils.parseDateValue(s, 0, dateEnd); long nanos; if (timeStart < 0) { nanos = DateTimeUtils.parseTimeNanos(s, dateEnd + 1, timeEnd, true); if (tz != null) { int year = DateTimeUtils.yearFromDateValue(dateValue); int month = DateTimeUtils.monthFromDateValue(dateValue); int day = DateTimeUtils.dayFromDateValue(dateValue); long ms = nanos / 1000000; nanos -= ms * 1000000; int hour = minute / 60; minute -= hour * 60; long millis = DateTimeUtils.getMillis( tz, year, month, day, hour, minute, (int) second, (int) ms); ms = DateTimeUtils.convertToLocal( new Date(millis), Calendar.getInstance(TimeZone.getTimeZone("UTC"))); long md = DateTimeUtils.MILLIS_PER_DAY; long absoluteDay = (ms >= 0 ? ms : ms - md + 1) / md; dateValue = DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay); ms -= absoluteDay * md; nanos += ms * 1000000;
DateTimeUtils.dateValue(1970, 1, 1)); case TIMESTAMP: return ValueDate.fromDateValue( switch (getType()) { case TIME: return DateTimeUtils.normalizeTimestamp( 0, ((ValueTime) this).getNanos()); case DATE:
/** * Returns the value of the specified column as a java.sql.Date using a * specified time zone. * * @param columnIndex (1,2,...) * @param calendar the calendar * @return the value * @throws SQLException if the column is not found or if the result set is * closed */ @Override public Date getDate(int columnIndex, Calendar calendar) throws SQLException { try { if (isDebugEnabled()) { debugCode("getDate(" + columnIndex + ", calendar)"); } return DateTimeUtils.convertDate(get(columnIndex), calendar); } catch (Exception e) { throw logAndConvert(e); } }
/** * Returns the value of the specified column as a java.sql.Timestamp using a * specified time zone. * * @param columnIndex (1,2,...) * @param calendar the calendar * @return the value * @throws SQLException if the column is not found or if the result set is * closed */ @Override public Timestamp getTimestamp(int columnIndex, Calendar calendar) throws SQLException { try { if (isDebugEnabled()) { debugCode("getTimestamp(" + columnIndex + ", calendar)"); } Value value = get(columnIndex); return DateTimeUtils.convertTimestamp(value, calendar); } catch (Exception e) { throw logAndConvert(e); } }
@Override public Timestamp getTimestamp() { return DateTimeUtils.convertDateValueToTimestamp(dateValue, timeNanos); }
@Override public Time getTime() { return DateTimeUtils.convertNanoToTime(nanos); }
m -= 12; return dateValue(y, m + 3, (int) d);
/** * Calculate the normalized timestamp. * * @param absoluteDay the absolute day * @param nanos the nanoseconds (may be negative or larger than one day) * @return the timestamp */ public static ValueTimestamp normalizeTimestamp(long absoluteDay, long nanos) { if (nanos > NANOS_PER_DAY || nanos < 0) { long d; if (nanos > NANOS_PER_DAY) { d = nanos / NANOS_PER_DAY; } else { d = (nanos - NANOS_PER_DAY + 1) / NANOS_PER_DAY; } nanos -= d * NANOS_PER_DAY; absoluteDay += d; } return ValueTimestamp.fromDateValueAndNanos( dateValueFromAbsoluteDay(absoluteDay), nanos); }
/** * Returns the value of the specified column as a java.sql.Date using a * specified time zone. * * @param columnLabel the column label * @param calendar the calendar * @return the value * @throws SQLException if the column is not found or if the result set is * closed */ @Override public Date getDate(String columnLabel, Calendar calendar) throws SQLException { try { if (isDebugEnabled()) { debugCode("getDate(" + StringUtils.quoteJavaString(columnLabel) + ", calendar)"); } return DateTimeUtils.convertDate(get(columnLabel), calendar); } catch (Exception e) { throw logAndConvert(e); } }
/** * Returns the value of the specified column as a java.sql.Timestamp. * * @param columnLabel the column label * @param calendar the calendar * @return the value * @throws SQLException if the column is not found or if the result set is * closed */ @Override public Timestamp getTimestamp(String columnLabel, Calendar calendar) throws SQLException { try { if (isDebugEnabled()) { debugCode("getTimestamp(" + StringUtils.quoteJavaString(columnLabel) + ", calendar)"); } Value value = get(columnLabel); return DateTimeUtils.convertTimestamp(value, calendar); } catch (Exception e) { throw logAndConvert(e); } }
/** * Returns the value of the specified column as a java.sql.Time using a * specified time zone. * * @param columnIndex (1,2,...) * @param calendar the calendar * @return the value * @throws SQLException if the column is not found or if the result set is * closed */ @Override public Time getTime(int columnIndex, Calendar calendar) throws SQLException { try { if (isDebugEnabled()) { debugCode("getTime(" + columnIndex + ", calendar)"); } return DateTimeUtils.convertTime(get(columnIndex), calendar); } catch (Exception e) { throw logAndConvert(e); } }
@Override public Value subtract(Value v) { ValueTimestamp t = (ValueTimestamp) v.convertTo(TIMESTAMP); long d1 = DateTimeUtils.absoluteDayFromDateValue(dateValue); long d2 = DateTimeUtils.absoluteDayFromDateValue(t.dateValue); return DateTimeUtils.normalizeTimestamp(d1 - d2, timeNanos - t.timeNanos); }
/** * Sets the date using a specified time zone. The value will be converted to * the local time zone. * * @param parameterIndex the parameter index (1, 2, ...) * @param x the value * @param calendar the calendar * @throws SQLException if this object is closed */ @Override public void setDate(int parameterIndex, java.sql.Date x, Calendar calendar) throws SQLException { try { if (isDebugEnabled()) { debugCode("setDate(" + parameterIndex + ", " + quoteDate(x) + ", calendar);"); } if (x == null) { setParameter(parameterIndex, ValueNull.INSTANCE); } else { setParameter(parameterIndex, DateTimeUtils.convertDate(x, calendar)); } } catch (Exception e) { throw logAndConvert(e); } }
/** * Sets the timestamp using a specified time zone. The value will be * converted to the local time zone. * * @param parameterIndex the parameter index (1, 2, ...) * @param x the value * @param calendar the calendar * @throws SQLException if this object is closed */ @Override public void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar calendar) throws SQLException { try { if (isDebugEnabled()) { debugCode("setTimestamp(" + parameterIndex + ", " + quoteTimestamp(x) + ", calendar);"); } if (x == null) { setParameter(parameterIndex, ValueNull.INSTANCE); } else { setParameter(parameterIndex, DateTimeUtils.convertTimestamp(x, calendar)); } } catch (Exception e) { throw logAndConvert(e); } }
/** * Returns the value of the specified column as a java.sql.Time using a * specified time zone. * * @param columnLabel the column label * @param calendar the calendar * @return the value * @throws SQLException if the column is not found or if the result set is * closed */ @Override public Time getTime(String columnLabel, Calendar calendar) throws SQLException { try { if (isDebugEnabled()) { debugCode("getTime(" + StringUtils.quoteJavaString(columnLabel) + ", calendar)"); } return DateTimeUtils.convertTime(get(columnLabel), calendar); } catch (Exception e) { throw logAndConvert(e); } }
/** * Sets the time using a specified time zone. The value will be converted to * the local time zone. * * @param parameterIndex the parameter index (1, 2, ...) * @param x the value * @param calendar the calendar * @throws SQLException if this object is closed */ @Override public void setTime(int parameterIndex, java.sql.Time x, Calendar calendar) throws SQLException { try { if (isDebugEnabled()) { debugCode("setTime(" + parameterIndex + ", " + quoteTime(x) + ", calendar);"); } if (x == null) { setParameter(parameterIndex, ValueNull.INSTANCE); } else { setParameter(parameterIndex, DateTimeUtils.convertTime(x, calendar)); } } catch (Exception e) { throw logAndConvert(e); } }