Unverified 提交 dde366b2 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #766 from katzyn/DateTimeUtils

Minor clean up of DateTimeUtils
...@@ -1856,8 +1856,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -1856,8 +1856,7 @@ public class Function extends Expression implements FunctionCall {
int nanos = d.getNanos() % 1000000; int nanos = d.getNanos() % 1000000;
calendar.setTime(d); calendar.setTime(d);
calendar.add(field, (int) count); calendar.add(field, (int) count);
long t = calendar.getTime().getTime(); Timestamp ts = new Timestamp(calendar.getTimeInMillis());
Timestamp ts = new Timestamp(t);
ts.setNanos(ts.getNanos() + nanos); ts.setNanos(ts.getNanos() + nanos);
return ts; return ts;
} }
...@@ -1926,7 +1925,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -1926,7 +1925,7 @@ public class Function extends Expression implements FunctionCall {
default: default:
break; break;
} }
calendar = DateTimeUtils.createGregorianCalendar(TimeZone.getTimeZone("UTC")); calendar = DateTimeUtils.createGregorianCalendar(DateTimeUtils.UTC);
calendar.setTimeInMillis(t1); calendar.setTimeInMillis(t1);
int year1 = calendar.get(Calendar.YEAR); int year1 = calendar.get(Calendar.YEAR);
int month1 = calendar.get(Calendar.MONTH); int month1 = calendar.get(Calendar.MONTH);
......
...@@ -31,6 +31,7 @@ import org.h2.message.DbException; ...@@ -31,6 +31,7 @@ import org.h2.message.DbException;
import org.h2.server.Service; import org.h2.server.Service;
import org.h2.server.ShutdownHandler; import org.h2.server.ShutdownHandler;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.util.DateTimeUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.NetUtils; import org.h2.util.NetUtils;
...@@ -257,7 +258,7 @@ public class WebServer implements Service { ...@@ -257,7 +258,7 @@ public class WebServer implements Service {
if (startDateTime == null) { if (startDateTime == null) {
SimpleDateFormat format = new SimpleDateFormat( SimpleDateFormat format = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss z", new Locale("en", "")); "EEE, d MMM yyyy HH:mm:ss z", new Locale("en", ""));
format.setTimeZone(TimeZone.getTimeZone("GMT")); format.setTimeZone(DateTimeUtils.UTC);
startDateTime = format.format(System.currentTimeMillis()); startDateTime = format.format(System.currentTimeMillis());
} }
return startDateTime; return startDateTime;
......
...@@ -35,6 +35,11 @@ public class DateTimeUtils { ...@@ -35,6 +35,11 @@ public class DateTimeUtils {
*/ */
public static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L; public static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;
/**
* UTC time zone.
*/
public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
private static final long NANOS_PER_DAY = MILLIS_PER_DAY * 1000000; private static final long NANOS_PER_DAY = MILLIS_PER_DAY * 1000000;
private static final int SHIFT_YEAR = 9; private static final int SHIFT_YEAR = 9;
...@@ -163,10 +168,9 @@ public class DateTimeUtils { ...@@ -163,10 +168,9 @@ public class DateTimeUtils {
cal.clear(); cal.clear();
cal.setLenient(true); cal.setLenient(true);
long dateValue = d.getDateValue(); long dateValue = d.getDateValue();
setCalendarFields(cal, yearFromDateValue(dateValue), long ms = convertToMillis(cal, yearFromDateValue(dateValue),
monthFromDateValue(dateValue), dayFromDateValue(dateValue), 0, monthFromDateValue(dateValue), dayFromDateValue(dateValue), 0,
0, 0, 0); 0, 0, 0);
long ms = cal.getTimeInMillis();
return new Date(ms); return new Date(ms);
} }
...@@ -194,10 +198,7 @@ public class DateTimeUtils { ...@@ -194,10 +198,7 @@ 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, (int) h, (int) m, (int) s, return new Time(convertToMillis(cal, 1970, 1, 1, (int) h, (int) m, (int) s, (int) millis));
(int) millis);
long ms = cal.getTimeInMillis();
return new Time(ms);
} }
/** /**
...@@ -225,10 +226,9 @@ public class DateTimeUtils { ...@@ -225,10 +226,9 @@ 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, yearFromDateValue(dateValue), long ms = convertToMillis(cal, yearFromDateValue(dateValue),
monthFromDateValue(dateValue), dayFromDateValue(dateValue), monthFromDateValue(dateValue), dayFromDateValue(dateValue),
(int) h, (int) m, (int) s, (int) millis); (int) h, (int) m, (int) s, (int) millis);
long ms = cal.getTimeInMillis();
Timestamp x = new Timestamp(ms); Timestamp x = new Timestamp(ms);
x.setNanos((int) (nanos + millis * 1000000)); x.setNanos((int) (nanos + millis * 1000000));
return x; return x;
...@@ -283,7 +283,7 @@ public class DateTimeUtils { ...@@ -283,7 +283,7 @@ public class DateTimeUtils {
Calendar local = DateTimeUtils.createGregorianCalendar(); Calendar local = DateTimeUtils.createGregorianCalendar();
local.setTime(x); local.setTime(x);
convertTime(local, target); convertTime(local, target);
return target.getTime().getTime(); return target.getTimeInMillis();
} }
private static void convertTime(Calendar from, Calendar to) { private static void convertTime(Calendar from, Calendar to) {
...@@ -468,12 +468,11 @@ public class DateTimeUtils { ...@@ -468,12 +468,11 @@ public class DateTimeUtils {
c = getCalendar(tz); c = getCalendar(tz);
} }
c.setLenient(lenient); c.setLenient(lenient);
setCalendarFields(c, year, month, day, hour, minute, second, millis); return convertToMillis(c, year, month, day, hour, minute, second, millis);
return c.getTime().getTime();
} }
private static void setCalendarFields(Calendar cal, int year, int month, private static long convertToMillis(Calendar cal, int year, int month, int day,
int day, int hour, int minute, int second, int millis) { int hour, int minute, int second, int millis) {
if (year <= 0) { if (year <= 0) {
cal.set(Calendar.ERA, GregorianCalendar.BC); cal.set(Calendar.ERA, GregorianCalendar.BC);
cal.set(Calendar.YEAR, 1 - year); cal.set(Calendar.YEAR, 1 - year);
...@@ -488,6 +487,7 @@ public class DateTimeUtils { ...@@ -488,6 +487,7 @@ public class DateTimeUtils {
cal.set(Calendar.MINUTE, minute); cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second); cal.set(Calendar.SECOND, second);
cal.set(Calendar.MILLISECOND, millis); cal.set(Calendar.MILLISECOND, millis);
return cal.getTimeInMillis();
} }
/** /**
......
...@@ -172,7 +172,7 @@ public class ValueTimestamp extends Value { ...@@ -172,7 +172,7 @@ public class ValueTimestamp extends Value {
int timeEnd = s.length(); int timeEnd = s.length();
TimeZone tz = null; TimeZone tz = null;
if (s.endsWith("Z")) { if (s.endsWith("Z")) {
tz = TimeZone.getTimeZone("UTC"); tz = DateTimeUtils.UTC;
timeEnd--; timeEnd--;
} else { } else {
int timeZoneStart = s.indexOf('+', dateEnd + 1); int timeZoneStart = s.indexOf('+', dateEnd + 1);
......
...@@ -40,8 +40,6 @@ public class ValueTimestampTimeZone extends Value { ...@@ -40,8 +40,6 @@ public class ValueTimestampTimeZone extends Value {
*/ */
static final int DEFAULT_SCALE = 10; static final int DEFAULT_SCALE = 10;
private static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
/** /**
* A bit field with bits for the year, month, and day (see DateTimeUtils for * A bit field with bits for the year, month, and day (see DateTimeUtils for
* encoding) * encoding)
...@@ -165,7 +163,7 @@ public class ValueTimestampTimeZone extends Value { ...@@ -165,7 +163,7 @@ public class ValueTimestampTimeZone extends Value {
} }
if (tz != null) { if (tz != null) {
long millis = DateTimeUtils long millis = DateTimeUtils
.convertDateValueToMillis(GMT_TIMEZONE, dateValue); .convertDateValueToMillis(DateTimeUtils.UTC, dateValue);
tzMinutes = (short) (tz.getOffset(millis) / 1000 / 60); tzMinutes = (short) (tz.getOffset(millis) / 1000 / 60);
} }
} }
...@@ -294,7 +292,7 @@ public class ValueTimestampTimeZone extends Value { ...@@ -294,7 +292,7 @@ public class ValueTimestampTimeZone extends Value {
// convert to minutes and add timezone offset // convert to minutes and add timezone offset
long a = DateTimeUtils.convertDateValueToMillis( long a = DateTimeUtils.convertDateValueToMillis(
TimeZone.getTimeZone("UTC"), dateValue) / DateTimeUtils.UTC, dateValue) /
(1000L * 60L); (1000L * 60L);
long ma = timeNanos / (1000L * 1000L * 1000L * 60L); long ma = timeNanos / (1000L * 1000L * 1000L * 60L);
a += ma; a += ma;
...@@ -302,7 +300,7 @@ public class ValueTimestampTimeZone extends Value { ...@@ -302,7 +300,7 @@ public class ValueTimestampTimeZone extends Value {
// convert to minutes and add timezone offset // convert to minutes and add timezone offset
long b = DateTimeUtils.convertDateValueToMillis( long b = DateTimeUtils.convertDateValueToMillis(
TimeZone.getTimeZone("UTC"), t.dateValue) / DateTimeUtils.UTC, t.dateValue) /
(1000L * 60L); (1000L * 60L);
long mb = t.timeNanos / (1000L * 1000L * 1000L * 60L); long mb = t.timeNanos / (1000L * 1000L * 1000L * 60L);
b += mb; b += mb;
......
...@@ -1309,7 +1309,7 @@ public class TestResultSet extends TestBase { ...@@ -1309,7 +1309,7 @@ public class TestResultSet extends TestBase {
java.sql.Timestamp.valueOf("2011-11-11 00:00:00.0").getTime()); java.sql.Timestamp.valueOf("2011-11-11 00:00:00.0").getTime());
o = rs.getObject(2, Calendar.class); o = rs.getObject(2, Calendar.class);
assertTrue(o instanceof Calendar); assertTrue(o instanceof Calendar);
assertEquals(((Calendar) o).getTime().getTime(), assertEquals(((Calendar) o).getTimeInMillis(),
java.sql.Timestamp.valueOf("2011-11-11 00:00:00.0").getTime()); java.sql.Timestamp.valueOf("2011-11-11 00:00:00.0").getTime());
rs.next(); rs.next();
......
...@@ -352,7 +352,7 @@ public class TestDate extends TestBase { ...@@ -352,7 +352,7 @@ public class TestDate extends TestBase {
} }
private void testValidDate() { private void testValidDate() {
Calendar c = DateTimeUtils.createGregorianCalendar(TimeZone.getTimeZone("UTC")); Calendar c = DateTimeUtils.createGregorianCalendar(DateTimeUtils.UTC);
c.setLenient(false); c.setLenient(false);
for (int y = -2000; y < 3000; y++) { for (int y = -2000; y < 3000; y++) {
for (int m = -3; m <= 14; m++) { for (int m = -3; m <= 14; m++) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论