提交 d7aa67f5 authored 作者: Noel Grandin's avatar Noel Grandin

comment some of the TIMESTAMP code better

上级 2a871b0b
...@@ -10,7 +10,7 @@ import org.h2.util.DateTimeUtils; ...@@ -10,7 +10,7 @@ import org.h2.util.DateTimeUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
/** /**
* How we expose "DATETIME WITH TIMEZONE" in our ResultSets. * How we expose "TIMESTAMP WITH TIMEZONE" in our ResultSets.
*/ */
public class TimestampWithTimeZone implements Serializable, Cloneable { public class TimestampWithTimeZone implements Serializable, Cloneable {
...@@ -170,27 +170,29 @@ public class TimestampWithTimeZone implements Serializable, Cloneable { ...@@ -170,27 +170,29 @@ public class TimestampWithTimeZone implements Serializable, Cloneable {
@Override @Override
public int hashCode() { public int hashCode() {
return 31 * super.hashCode() + timeZoneOffsetMins; final int prime = 31;
int result = 1;
result = prime * result + (int) (dateValue ^ (dateValue >>> 32));
result = prime * result + (int) (timeNanos ^ (timeNanos >>> 32));
result = prime * result + timeZoneOffsetMins;
return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj)
return true; return true;
} if (obj == null)
if (getClass() != obj.getClass()) { return false;
if (getClass() != obj.getClass())
return false; return false;
}
TimestampWithTimeZone other = (TimestampWithTimeZone) obj; TimestampWithTimeZone other = (TimestampWithTimeZone) obj;
if (dateValue != other.dateValue) { if (dateValue != other.dateValue)
return false; return false;
} if (timeNanos != other.timeNanos)
if (timeNanos != other.timeNanos) {
return false; return false;
} if (timeZoneOffsetMins != other.timeZoneOffsetMins)
if (timeZoneOffsetMins != other.timeZoneOffsetMins) {
return false; return false;
}
return true; return true;
} }
......
/* /*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, and the
* and the EPL 1.0 (http://h2database.com/html/license.html). * EPL 1.0 (http://h2database.com/html/license.html). Initial Developer: H2
* Initial Developer: H2 Group * Group Iso8601: Initial Developer: Robert Rathsack (firstName dot lastName at
* Iso8601: * gmx dot de)
* Initial Developer: Robert Rathsack (firstName dot lastName at gmx dot de)
*/ */
package org.h2.util; package org.h2.util;
...@@ -26,8 +25,8 @@ import org.h2.value.ValueTimestamp; ...@@ -26,8 +25,8 @@ import org.h2.value.ValueTimestamp;
/** /**
* This utility class contains time conversion functions. * This utility class contains time conversion functions.
* <p> * <p>
* Date value: a bit field with bits for the year, month, and day. * Date value: a bit field with bits for the year, month, and day. Absolute day:
* Absolute day: the day number (0 means 1970-01-01). * the day number (0 means 1970-01-01).
*/ */
public class DateTimeUtils { public class DateTimeUtils {
...@@ -58,24 +57,23 @@ public class DateTimeUtils { ...@@ -58,24 +57,23 @@ public class DateTimeUtils {
* have that problem, and while it is still a small memory leak, it is not a * have that problem, and while it is still a small memory leak, it is not a
* class loader memory leak. * class loader memory leak.
*/ */
private static final ThreadLocal<Calendar> CACHED_CALENDAR = private static final ThreadLocal<Calendar> CACHED_CALENDAR = new ThreadLocal<Calendar>();
new ThreadLocal<Calendar>();
/** /**
* A cached instance of Calendar used when a timezone is specified. * A cached instance of Calendar used when a timezone is specified.
*/ */
private static final ThreadLocal<Calendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE = private static final ThreadLocal<Calendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE = new ThreadLocal<Calendar>();
new ThreadLocal<Calendar>();
/** /**
* Observed JVM behaviour is that if the timezone of the host computer is changed while the JVM is running, * Observed JVM behaviour is that if the timezone of the host computer is
* the zone offset does not change but keeps the initial value. So it is correct to measure this once and use * changed while the JVM is running, the zone offset does not change but
* this value throughout the JVM's lifecycle. * keeps the initial value. So it is correct to measure this once and use
* * this value throughout the JVM's lifecycle. In any case, it is safer to
* In any case, it is safer to use a fixed value throughout the duration of the JVM's life, rather than have * use a fixed value throughout the duration of the JVM's life, rather than
* this offset change, possibly midway through a long-running query. * have this offset change, possibly midway through a long-running query.
*/ */
private static int zoneOffsetMillis = Calendar.getInstance().get(Calendar.ZONE_OFFSET); private static int zoneOffsetMillis = Calendar.getInstance()
.get(Calendar.ZONE_OFFSET);
private DateTimeUtils() { private DateTimeUtils() {
// utility class // utility class
...@@ -138,9 +136,8 @@ public class DateTimeUtils { ...@@ -138,9 +136,8 @@ public class DateTimeUtils {
cal.setLenient(true); cal.setLenient(true);
long dateValue = d.getDateValue(); long dateValue = d.getDateValue();
setCalendarFields(cal, yearFromDateValue(dateValue), setCalendarFields(cal, yearFromDateValue(dateValue),
monthFromDateValue(dateValue), monthFromDateValue(dateValue), dayFromDateValue(dateValue), 0,
dayFromDateValue(dateValue), 0, 0, 0);
0, 0, 0, 0);
long ms = cal.getTimeInMillis(); long ms = cal.getTimeInMillis();
return new Date(ms); return new Date(ms);
} }
...@@ -169,8 +166,8 @@ public class DateTimeUtils { ...@@ -169,8 +166,8 @@ public class DateTimeUtils {
s -= m * 60; s -= m * 60;
long h = m / 60; long h = m / 60;
m -= h * 60; m -= h * 60;
setCalendarFields(cal, 1970, 1, 1, setCalendarFields(cal, 1970, 1, 1, (int) h, (int) m, (int) s,
(int) h, (int) m, (int) s, (int) millis); (int) millis);
long ms = cal.getTimeInMillis(); long ms = cal.getTimeInMillis();
return new Time(ms); return new Time(ms);
} }
...@@ -201,8 +198,7 @@ public class DateTimeUtils { ...@@ -201,8 +198,7 @@ public class DateTimeUtils {
long h = m / 60; long h = m / 60;
m -= h * 60; m -= h * 60;
setCalendarFields(cal, yearFromDateValue(dateValue), setCalendarFields(cal, yearFromDateValue(dateValue),
monthFromDateValue(dateValue), monthFromDateValue(dateValue), dayFromDateValue(dateValue),
dayFromDateValue(dateValue),
(int) h, (int) m, (int) s, (int) millis); (int) h, (int) m, (int) s, (int) millis);
long ms = cal.getTimeInMillis(); long ms = cal.getTimeInMillis();
Timestamp x = new Timestamp(ms); Timestamp x = new Timestamp(ms);
...@@ -211,7 +207,7 @@ public class DateTimeUtils { ...@@ -211,7 +207,7 @@ public class DateTimeUtils {
} }
/** /**
* Convert the date using the specified calendar. * Convert a java.util.Date using the specified calendar.
* *
* @param x the date * @param x the date
* @param calendar the calendar * @param calendar the calendar
...@@ -282,7 +278,8 @@ public class DateTimeUtils { ...@@ -282,7 +278,8 @@ public class DateTimeUtils {
* @param calendar the calendar * @param calendar the calendar
* @return the timestamp * @return the timestamp
*/ */
public static ValueTimestamp convertTimestamp(Timestamp x, Calendar calendar) { public static ValueTimestamp convertTimestamp(Timestamp x,
Calendar calendar) {
if (calendar == null) { if (calendar == null) {
throw DbException.getInvalidValueException("calendar", null); throw DbException.getInvalidValueException("calendar", null);
} }
...@@ -372,8 +369,8 @@ public class DateTimeUtils { ...@@ -372,8 +369,8 @@ public class DateTimeUtils {
String n = (s.substring(s3 + 1, end) + "000000000").substring(0, 9); String n = (s.substring(s3 + 1, end) + "000000000").substring(0, 9);
nanos = Integer.parseInt(n); nanos = Integer.parseInt(n);
} }
if (hour >= 2000000 || minute < 0 || if (hour >= 2000000 || minute < 0 || minute >= 60 || second < 0
minute >= 60 || second < 0 || second >= 60) { || second >= 60) {
throw new IllegalArgumentException(s); throw new IllegalArgumentException(s);
} }
if (timeOfDay && hour >= 24) { if (timeOfDay && hour >= 24) {
...@@ -387,8 +384,8 @@ public class DateTimeUtils { ...@@ -387,8 +384,8 @@ public class DateTimeUtils {
* Calculate the milliseconds since 1970-01-01 (UTC) for the given date and * Calculate the milliseconds since 1970-01-01 (UTC) for the given date and
* time (in the specified timezone). * time (in the specified timezone).
* *
* @param tz the timezone of the parameters, * @param tz the timezone of the parameters, or null for the default
* or null for the default timezone * timezone
* @param year the absolute year (positive or negative) * @param year the absolute year (positive or negative)
* @param month the month (1-12) * @param month the month (1-12)
* @param day the day (1-31) * @param day the day (1-31)
...@@ -401,7 +398,8 @@ public class DateTimeUtils { ...@@ -401,7 +398,8 @@ public class DateTimeUtils {
public static long getMillis(TimeZone tz, int year, int month, int day, public static long getMillis(TimeZone tz, int year, int month, int day,
int hour, int minute, int second, int millis) { int hour, int minute, int second, int millis) {
try { try {
return getTimeTry(false, tz, year, month, day, hour, minute, second, millis); return getTimeTry(false, tz, year, month, day, hour, minute, second,
millis);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// special case: if the time simply doesn't exist because of // special case: if the time simply doesn't exist because of
// daylight saving time changes, use the lenient version // daylight saving time changes, use the lenient version
...@@ -435,9 +433,8 @@ public class DateTimeUtils { ...@@ -435,9 +433,8 @@ public class DateTimeUtils {
} }
} }
private static long getTimeTry(boolean lenient, TimeZone tz, private static long getTimeTry(boolean lenient, TimeZone tz, int year,
int year, int month, int day, int hour, int minute, int second, int month, int day, int hour, int minute, int second, int millis) {
int millis) {
Calendar c; Calendar c;
if (tz == null) { if (tz == null) {
c = getCalendar(); c = getCalendar();
...@@ -529,7 +526,6 @@ public class DateTimeUtils { ...@@ -529,7 +526,6 @@ public class DateTimeUtils {
* starts at Monday. See also http://en.wikipedia.org/wiki/ISO_8601 * starts at Monday. See also http://en.wikipedia.org/wiki/ISO_8601
* *
* @author Robert Rathsack * @author Robert Rathsack
*
* @param date the date object which day of week should be calculated * @param date the date object which day of week should be calculated
* @return the day of the week, Monday as 1 to Sunday as 7 * @return the day of the week, Monday as 1 to Sunday as 7
*/ */
...@@ -566,7 +562,6 @@ public class DateTimeUtils { ...@@ -566,7 +562,6 @@ public class DateTimeUtils {
* Returns the year according to the ISO week definition. * Returns the year according to the ISO week definition.
* *
* @author Robert Rathsack * @author Robert Rathsack
*
* @param date the date object which year should be calculated * @param date the date object which year should be calculated
* @return the year * @return the year
*/ */
...@@ -680,35 +675,35 @@ public class DateTimeUtils { ...@@ -680,35 +675,35 @@ public class DateTimeUtils {
} }
/** /**
* Convert a date value to a date, using the default timezone. * Convert an encoded date value to a java.util.Date, using the default
* timezone.
* *
* @param dateValue the date value * @param dateValue the date value
* @return the date * @return the date
*/ */
public static Date convertDateValueToDate(long dateValue) { public static Date convertDateValueToDate(long dateValue) {
long millis = getMillis(null, long millis = getMillis(null, yearFromDateValue(dateValue),
yearFromDateValue(dateValue), monthFromDateValue(dateValue), dayFromDateValue(dateValue), 0,
monthFromDateValue(dateValue), 0, 0, 0);
dayFromDateValue(dateValue), 0, 0, 0, 0);
return new Date(millis); return new Date(millis);
} }
/** /**
* Convert a date value to millis, using the supplied timezone. * Convert an encoded date value to millis, using the supplied timezone.
* *
* @param tz the timezone * @param tz the timezone
* @param dateValue the date value * @param dateValue the date value
* @return the date * @return the date
*/ */
public static long convertDateValueToMillis(TimeZone tz, long dateValue) { public static long convertDateValueToMillis(TimeZone tz, long dateValue) {
return getMillis(tz, return getMillis(tz, yearFromDateValue(dateValue),
yearFromDateValue(dateValue), monthFromDateValue(dateValue), dayFromDateValue(dateValue), 0,
monthFromDateValue(dateValue), 0, 0, 0);
dayFromDateValue(dateValue), 0, 0, 0, 0);
} }
/** /**
* Convert a date value / time value to a timestamp, using the default * Convert an encoded date value / time value to a timestamp, using the
* timezone. * default timezone.
* *
* @param dateValue the date value * @param dateValue the date value
* @param timeNanos the nanoseconds since midnight * @param timeNanos the nanoseconds since midnight
...@@ -724,10 +719,8 @@ public class DateTimeUtils { ...@@ -724,10 +719,8 @@ public class DateTimeUtils {
s -= m * 60; s -= m * 60;
long h = m / 60; long h = m / 60;
m -= h * 60; m -= h * 60;
long ms = getMillis(null, long ms = getMillis(null, yearFromDateValue(dateValue),
yearFromDateValue(dateValue), monthFromDateValue(dateValue), dayFromDateValue(dateValue),
monthFromDateValue(dateValue),
dayFromDateValue(dateValue),
(int) h, (int) m, (int) s, 0); (int) h, (int) m, (int) s, 0);
Timestamp ts = new Timestamp(ms); Timestamp ts = new Timestamp(ms);
ts.setNanos((int) (timeNanos + millis * 1000000)); ts.setNanos((int) (timeNanos + millis * 1000000));
...@@ -735,22 +728,21 @@ public class DateTimeUtils { ...@@ -735,22 +728,21 @@ public class DateTimeUtils {
} }
/** /**
* Convert a time value to a time, using the default * Convert a time value to a time, using the default timezone.
* timezone.
* *
* @param nanos the nanoseconds since midnight * @param nanosSinceMidnight the nanoseconds since midnight
* @return the time * @return the time
*/ */
public static Time convertNanoToTime(long nanos) { public static Time convertNanoToTime(long nanosSinceMidnight) {
long millis = nanos / 1000000; long millis = nanosSinceMidnight / 1000000;
long s = millis / 1000; long s = millis / 1000;
millis -= s * 1000; millis -= s * 1000;
long m = s / 60; long m = s / 60;
s -= m * 60; s -= m * 60;
long h = m / 60; long h = m / 60;
m -= h * 60; m -= h * 60;
long ms = getMillis(null, long ms = getMillis(null, 1970, 1, 1, (int) (h % 24), (int) m, (int) s,
1970, 1, 1, (int) (h % 24), (int) m, (int) s, (int) millis); (int) millis);
return new Time(ms); return new Time(ms);
} }
...@@ -797,8 +789,8 @@ public class DateTimeUtils { ...@@ -797,8 +789,8 @@ public class DateTimeUtils {
} }
/** /**
* Calculate the date value (in the default timezone) from a given time in * Convert a UTC datetime in millis to an encoded date in the default
* milliseconds in UTC. * timezone.
* *
* @param ms the milliseconds * @param ms the milliseconds
* @return the date value * @return the date value
...@@ -810,7 +802,7 @@ public class DateTimeUtils { ...@@ -810,7 +802,7 @@ public class DateTimeUtils {
} }
/** /**
* Calculate the date value from a given calendar. * Calculate the encoded date value from a given calendar.
* *
* @param cal the calendar * @param cal the calendar
* @return the date value * @return the date value
...@@ -824,8 +816,8 @@ public class DateTimeUtils { ...@@ -824,8 +816,8 @@ public class DateTimeUtils {
} }
/** /**
* Calculate the nanoseconds since midnight (in the default timezone) from a * Convert a time in milliseconds in UTC to the nanoseconds since midnight
* given time in milliseconds in UTC. * (in the default timezone).
* *
* @param ms the milliseconds * @param ms the milliseconds
* @return the nanoseconds * @return the nanoseconds
...@@ -837,7 +829,7 @@ public class DateTimeUtils { ...@@ -837,7 +829,7 @@ public class DateTimeUtils {
} }
/** /**
* Calculate the nanoseconds since midnight from a given calendar. * Convert a java.util.Calendar to nanoseconds since midnight.
* *
* @param cal the calendar * @param cal the calendar
* @return the nanoseconds * @return the nanoseconds
...@@ -857,7 +849,8 @@ public class DateTimeUtils { ...@@ -857,7 +849,8 @@ public class DateTimeUtils {
* @param nanos the nanoseconds (may be negative or larger than one day) * @param nanos the nanoseconds (may be negative or larger than one day)
* @return the timestamp * @return the timestamp
*/ */
public static ValueTimestamp normalizeTimestamp(long absoluteDay, long nanos) { public static ValueTimestamp normalizeTimestamp(long absoluteDay,
long nanos) {
if (nanos > NANOS_PER_DAY || nanos < 0) { if (nanos > NANOS_PER_DAY || nanos < 0) {
long d; long d;
if (nanos > NANOS_PER_DAY) { if (nanos > NANOS_PER_DAY) {
...@@ -873,7 +866,7 @@ public class DateTimeUtils { ...@@ -873,7 +866,7 @@ public class DateTimeUtils {
} }
/** /**
* Calculate the absolute day from a date value. * Calculate the absolute day from an encoded date value.
* *
* @param dateValue the date value * @param dateValue the date value
* @return the absolute day * @return the absolute day
...@@ -898,7 +891,7 @@ public class DateTimeUtils { ...@@ -898,7 +891,7 @@ public class DateTimeUtils {
} }
/** /**
* Calculate the date value from an absolute day. * Calculate the encoded date value from an absolute day.
* *
* @param absoluteDay the absolute day * @param absoluteDay the absolute day
* @return the date value * @return the date value
...@@ -940,8 +933,7 @@ public class DateTimeUtils { ...@@ -940,8 +933,7 @@ public class DateTimeUtils {
/** /**
* Adds the number of months to the date. If the resulting month's number of * Adds the number of months to the date. If the resulting month's number of
* days is less than the original's day-of-month, the resulting * days is less than the original's day-of-month, the resulting
* day-of-months gets adjusted accordingly: * day-of-months gets adjusted accordingly: <br>
* <br>
* 30.04.2007 - 2 months = 28.02.2007 * 30.04.2007 - 2 months = 28.02.2007
* *
* @param refDate the original date * @param refDate the original date
......
...@@ -30,8 +30,14 @@ public class ValueTime extends Value { ...@@ -30,8 +30,14 @@ public class ValueTime extends Value {
*/ */
static final int DISPLAY_SIZE = 8; static final int DISPLAY_SIZE = 8;
/**
* Nanoseconds since midnight
*/
private final long nanos; private final long nanos;
/**
* @param nanos nanoseconds since midnight
*/
private ValueTime(long nanos) { private ValueTime(long nanos) {
this.nanos = nanos; this.nanos = nanos;
} }
...@@ -39,7 +45,7 @@ public class ValueTime extends Value { ...@@ -39,7 +45,7 @@ public class ValueTime extends Value {
/** /**
* Get or create a time value. * Get or create a time value.
* *
* @param nanos the nanoseconds * @param nanos the nanoseconds since midnight
* @return the value * @return the value
*/ */
public static ValueTime fromNanos(long nanos) { public static ValueTime fromNanos(long nanos) {
...@@ -73,7 +79,6 @@ public class ValueTime extends Value { ...@@ -73,7 +79,6 @@ public class ValueTime extends Value {
* @param s the string to parse * @param s the string to parse
* @return the time * @return the time
*/ */
public static ValueTime parse(String s) { public static ValueTime parse(String s) {
try { try {
return fromNanos(DateTimeUtils.parseTimeNanos(s, 0, s.length(), false)); return fromNanos(DateTimeUtils.parseTimeNanos(s, 0, s.length(), false));
...@@ -83,6 +88,9 @@ public class ValueTime extends Value { ...@@ -83,6 +88,9 @@ public class ValueTime extends Value {
} }
} }
/**
* @return nanoseconds since midnight
*/
public long getNanos() { public long getNanos() {
return nanos; return nanos;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论