Update timestamp to fix some lingering issues by copying over code again from java.sql.Timestamp.

This commit is contained in:
Greg Burd 2019-11-21 21:25:33 -05:00
parent 262bba3a02
commit 048f8c65f0

View file

@ -14,9 +14,9 @@
package com.sleepycat.je.utilint; package com.sleepycat.je.utilint;
/** /**
* Duplicate of java.sql.Timestamp which keeps our implementation constant in * Duplicate of java.sql.Timestamp
* case the java.sql.Timestamp implementation changes incompatibly. This way * This duplicate keeps our implementation constant in case the java.sql.Timestamp implementation
* we can write it to disk and not worry about upgrading the log file. * changes incompatibly. This way we can write it to disk and not worry about upgrading the log file.
*/ */
public class Timestamp extends java.util.Date { public class Timestamp extends java.util.Date {
@ -24,135 +24,157 @@ public class Timestamp extends java.util.Date {
private int nanos; private int nanos;
/**
* Constructs a {@code Timestamp} object using milliseconds as the time value. Stores the integral seconds in the
* underlying date value; stores the fractional seconds in the {@code nanos} field of the {@code Timestamp} object.
*
* @param time milliseconds since January 1, 1970, 00:00:00 GMT.
* A negative number represents milliseconds before January 1, 1970, 00:00:00 GMT.
*/
public Timestamp(long time) { public Timestamp(long time) {
super((time / 1000) * 1000); super((time / 1000) * 1000);
nanos = (int) ((time % 1000) * 1000000); nanos = (int) ((time % 1000) * 1000000);
if (nanos < 0) { if (nanos < 0) {
nanos = 1000000000 + nanos; nanos = 1000000000 + nanos;
super.setTime(((time / 1000) - 1) * 1000); super.setTime(((time / 1000) - 1) * 1000);
} }
} }
/**
* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
* represented by this {@code Timestamp} object.
*
* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
* represented by this date.
* @see #setTime
*/
public long getTime() { public long getTime() {
long time = super.getTime(); long time = super.getTime();
return (time + (nanos / 1000000)); return (time + (nanos / 1000000));
} }
/**
* Formats a timestamp in timestamp escape format.
* {@code yyyy-mm-dd hh:mm:ss.fffffffff},
* where {@code fffffffff} indicates nanoseconds.
*
* @return a {@code String} object in
* {@code yyyy-mm-dd hh:mm:ss.fffffffff} format
*/
public String toString() { public String toString() {
int year = super.getYear() + 1900;
int month = super.getMonth() + 1;
int day = super.getDate();
int hour = super.getHours();
int minute = super.getMinutes();
int second = super.getSeconds();
String yearString;
String monthString;
String dayString;
String hourString;
String minuteString;
String secondString;
String nanosString;
String zeros = "000000000";
String yearZeros = "0000";
StringBuffer timestampBuf;
int year = super.getYear() + 1900; if (year < 1000) {
int month = super.getMonth() + 1; /* Add leading zeros. */
int day = super.getDate(); yearString = "" + year;
int hour = super.getHours(); yearString = yearZeros.substring(0, (4 - yearString.length())) +
int minute = super.getMinutes(); yearString;
int second = super.getSeconds(); } else {
String yearString; yearString = "" + year;
String monthString; }
String dayString;
String hourString;
String minuteString;
String secondString;
String nanosString;
String zeros = "000000000";
String yearZeros = "0000";
StringBuffer timestampBuf;
if (year < 1000) { if (month < 10) {
/* Add leading zeros. */ monthString = "0" + month;
yearString = "" + year; } else {
yearString = yearZeros.substring(0, (4 - yearString.length())) + monthString = Integer.toString(month);
yearString; }
} else {
yearString = "" + year;
}
if (month < 10) { if (day < 10) {
monthString = "0" + month; dayString = "0" + day;
} else { } else {
monthString = Integer.toString(month); dayString = Integer.toString(day);
} }
if (day < 10) { if (hour < 10) {
dayString = "0" + day; hourString = "0" + hour;
} else { } else {
dayString = Integer.toString(day); hourString = Integer.toString(hour);
} }
if (hour < 10) { if (minute < 10) {
hourString = "0" + hour; minuteString = "0" + minute;
} else { } else {
hourString = Integer.toString(hour); minuteString = Integer.toString(minute);
} }
if (minute < 10) { if (second < 10) {
minuteString = "0" + minute; secondString = "0" + second;
} else { } else {
minuteString = Integer.toString(minute); secondString = Integer.toString(second);
} }
if (second < 10) { if (nanos == 0) {
secondString = "0" + second; nanosString = "0";
} else { } else {
secondString = Integer.toString(second); nanosString = Integer.toString(nanos);
}
if (nanos == 0) { /* Add leading zeros. */
nanosString = "0"; nanosString = zeros.substring(0, (9 - nanosString.length())) +
} else { nanosString;
nanosString = Integer.toString(nanos);
/* Add leading zeros. */ /* Truncate trailing zeros. */
nanosString = zeros.substring(0, (9 - nanosString.length())) + char[] nanosChar = new char[nanosString.length()];
nanosString; nanosString.getChars(0, nanosString.length(), nanosChar, 0);
int truncIndex = 8;
while (nanosChar[truncIndex] == '0') {
truncIndex--;
}
/* Truncate trailing zeros. */ nanosString = new String(nanosChar, 0, truncIndex + 1);
char[] nanosChar = new char[nanosString.length()]; }
nanosString.getChars(0, nanosString.length(), nanosChar, 0);
int truncIndex = 8;
while (nanosChar[truncIndex] == '0') {
truncIndex--;
}
nanosString = new String(nanosChar, 0, truncIndex + 1); /* Do a string buffer here instead. */
} timestampBuf = new StringBuffer(20 + nanosString.length());
timestampBuf.append(yearString);
timestampBuf.append("-");
timestampBuf.append(monthString);
timestampBuf.append("-");
timestampBuf.append(dayString);
timestampBuf.append(" ");
timestampBuf.append(hourString);
timestampBuf.append(":");
timestampBuf.append(minuteString);
timestampBuf.append(":");
timestampBuf.append(secondString);
timestampBuf.append(".");
timestampBuf.append(nanosString);
/* Do a string buffer here instead. */ return (timestampBuf.toString());
timestampBuf = new StringBuffer(20 + nanosString.length());
timestampBuf.append(yearString);
timestampBuf.append("-");
timestampBuf.append(monthString);
timestampBuf.append("-");
timestampBuf.append(dayString);
timestampBuf.append(" ");
timestampBuf.append(hourString);
timestampBuf.append(":");
timestampBuf.append(minuteString);
timestampBuf.append(":");
timestampBuf.append(secondString);
timestampBuf.append(".");
timestampBuf.append(nanosString);
return (timestampBuf.toString());
} }
public boolean equals(Timestamp ts) { public boolean equals(Timestamp ts) {
if (super.equals(ts)) { if (super.equals(ts)) {
if (nanos == ts.nanos) { if (nanos == ts.nanos) {
return true; return true;
} else { } else {
return false; return false;
} }
} else { } else {
return false; return false;
} }
} }
public boolean equals(Object ts) { public boolean equals(Object ts) {
if (ts instanceof Timestamp) { if (ts instanceof Timestamp) {
return this.equals((Timestamp)ts); return this.equals((Timestamp) ts);
} else { } else {
return false; return false;
} }
} }
} }