/** * Applies the local offset from UTC to each of the applicable time field * values and returns the new Timestamp. In short, this makes the Timestamp * represent local time. * * @return a new Timestamp in its local time */ private Timestamp make_localtime() { int offset = _offset != null ? _offset.intValue() : 0; Timestamp localtime = clone(); // explicitly apply the local offset to the time field values localtime.apply_offset(-offset); assert localtime._offset == _offset; return localtime; }
/** * Adds the given amount to the given {@link Calendar} field and returns a new Timestamp * with precision set to the maximum of the current precision and the given precision. * @param field the field. * @param amount an amount. * @param precision the precision corresponding to the given field. * @return a new Timestamp instance. */ private Timestamp calendarAdd(int field, int amount, Precision precision) { if (amount == 0 && _precision == precision) return this; Timestamp timestamp = make_localtime(); timestamp._calendar.add(field, amount); checkCalendarYear(timestamp._calendar); if (_offset != null) { timestamp.apply_offset(_offset); timestamp._offset = _offset; } timestamp._precision = _precision.includes(precision) ? timestamp._precision : precision; return timestamp; }
apply_offset(offset);
this.apply_offset(_offset);
private Timestamp(Calendar cal, Precision precision, BigDecimal fraction, Integer offset) { set_fields_from_calendar(cal, precision, false); _fraction = fraction; if (offset != null) { _offset = offset; apply_offset(offset); } }
private Timestamp(Calendar cal, Precision precision, BigDecimal fraction, Integer offset) { set_fields_from_calendar(cal, precision, false); _fraction = fraction; if (offset != null) { _offset = offset; apply_offset(offset); } }
APPLY_OFFSET_NO); localtime.apply_offset(-offset);
APPLY_OFFSET_NO); localtime.apply_offset(-offset);
/** * Returns a timestamp relative to this one by the given number of * milliseconds. * * @param amount a (positive or negative) number of milliseconds. */ public final Timestamp addMillis(long amount) { if (amount == 0) return this; // This strips off the local offset, expressing our fields as if they // were UTC. BigDecimal millis = make_localtime().getDecimalMillis(); millis = millis.add(BigDecimal.valueOf(amount)); Timestamp ts = new Timestamp(millis, _precision, _offset); // Anything with courser-than-millis precision will have been extended // to 3 decimal places due to use of getDecimalMillis(). Fix that. ts._fraction = _fraction; if (_offset != null && _offset != 0) { ts.apply_offset(_offset); } return ts; }
/** * Returns a timestamp relative to this one by the given number of * milliseconds. * * @param amount a (positive or negative) number of milliseconds. */ public final Timestamp addMillis(long amount) { if (amount == 0) return this; // This strips off the local offset, expressing our fields as if they // were UTC. BigDecimal millis = make_localtime().getDecimalMillis(); millis = millis.add(BigDecimal.valueOf(amount)); Timestamp ts = new Timestamp(millis, _precision, _offset); // Anything with courser-than-millis precision will have been extended // to 3 decimal places due to use of getDecimalMillis(). Fix that. ts._fraction = _fraction; if (_offset != null && _offset != 0) { ts.apply_offset(_offset); } return ts; }
this.apply_offset(_offset);
this.apply_offset(_offset);
/** * This method uses deprecated methods from {@link java.util.Date} * instead of {@link Calendar} so that this code can be used (more easily) * on the mobile Java platform (which has Date but does not have Calendar). */ @SuppressWarnings("deprecation") private void set_fields_from_millis(long millis) { if(millis < MINIMUM_ALLOWED_TIMESTAMP_IN_MILLIS){ throw new IllegalArgumentException("year is less than 1"); } Date date = new Date(millis); // These fields are in the system timezone! this._year = checkAndCastYear(date.getYear() + 1900); this._month = checkAndCastMonth(date.getMonth() + 1); // calendar months are 0 based, timestamp months are 1 based this._day = checkAndCastDay(date.getDate(), _year, _month); this._hour = checkAndCastHour(date.getHours()); this._minute = checkAndCastMinute(date.getMinutes()); this._second = checkAndCastSecond(date.getSeconds()); // this is done because the y-m-d values are in the local timezone // so this adjusts the value back to zulu time (UTC) // Note that the sign on this is opposite of Ion (and Calendar) offset. // Example: PST = 480 here but Ion/Calendar use -480 = -08:00 = UTC-8 int offset = date.getTimezoneOffset(); this.apply_offset(-offset); }
apply_offset(offset);
/** * This method uses deprecated methods from {@link java.util.Date} * instead of {@link Calendar} so that this code can be used (more easily) * on the mobile Java platform (which has Date but does not have Calendar). */ @SuppressWarnings("deprecation") private void set_fields_from_millis(long millis) { Date date = new Date(millis); // These fields are in the system timezone! this._year = checkAndCastYear(date.getYear() + 1900); this._month = checkAndCastMonth(date.getMonth() + 1); // calendar months are 0 based, timestamp months are 1 based this._day = checkAndCastDay(date.getDate(), _year, _month); this._hour = checkAndCastHour(date.getHours()); this._minute = checkAndCastMinute(date.getMinutes()); this._second = checkAndCastSecond(date.getSeconds()); // this is done because the y-m-d values are in the local timezone // so this adjusts the value back to zulu time (UTC) // Note that the sign on this is opposite of Ion (and Calendar) offset. // Example: PST = 480 here but Ion/Calendar use -480 = -08:00 = UTC-8 int offset = date.getTimezoneOffset(); this.apply_offset(-offset); }