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;
/**
* Duplicate of java.sql.Timestamp which keeps our implementation constant in
* case the java.sql.Timestamp implementation changes incompatibly. This way
* we can write it to disk and not worry about upgrading the log file.
* Duplicate of java.sql.Timestamp
* This duplicate keeps our implementation constant in case the java.sql.Timestamp implementation
* 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 {
@ -24,135 +24,157 @@ public class Timestamp extends java.util.Date {
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) {
super((time / 1000) * 1000);
nanos = (int) ((time % 1000) * 1000000);
if (nanos < 0) {
nanos = 1000000000 + nanos;
super.setTime(((time / 1000) - 1) * 1000);
}
super((time / 1000) * 1000);
nanos = (int) ((time % 1000) * 1000000);
if (nanos < 0) {
nanos = 1000000000 + nanos;
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() {
long time = super.getTime();
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() {
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;
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;
if (year < 1000) {
/* Add leading zeros. */
yearString = "" + year;
yearString = yearZeros.substring(0, (4 - yearString.length())) +
yearString;
} else {
yearString = "" + year;
}
if (year < 1000) {
/* Add leading zeros. */
yearString = "" + year;
yearString = yearZeros.substring(0, (4 - yearString.length())) +
yearString;
} else {
yearString = "" + year;
}
if (month < 10) {
monthString = "0" + month;
} else {
monthString = Integer.toString(month);
}
if (month < 10) {
monthString = "0" + month;
} else {
monthString = Integer.toString(month);
}
if (day < 10) {
dayString = "0" + day;
} else {
dayString = Integer.toString(day);
}
if (day < 10) {
dayString = "0" + day;
} else {
dayString = Integer.toString(day);
}
if (hour < 10) {
hourString = "0" + hour;
} else {
hourString = Integer.toString(hour);
}
if (hour < 10) {
hourString = "0" + hour;
} else {
hourString = Integer.toString(hour);
}
if (minute < 10) {
minuteString = "0" + minute;
} else {
minuteString = Integer.toString(minute);
}
if (minute < 10) {
minuteString = "0" + minute;
} else {
minuteString = Integer.toString(minute);
}
if (second < 10) {
secondString = "0" + second;
} else {
secondString = Integer.toString(second);
}
if (second < 10) {
secondString = "0" + second;
} else {
secondString = Integer.toString(second);
}
if (nanos == 0) {
nanosString = "0";
} else {
nanosString = Integer.toString(nanos);
if (nanos == 0) {
nanosString = "0";
} else {
nanosString = Integer.toString(nanos);
/* Add leading zeros. */
nanosString = zeros.substring(0, (9 - nanosString.length())) +
nanosString;
/* Add leading zeros. */
nanosString = zeros.substring(0, (9 - nanosString.length())) +
nanosString;
/* Truncate trailing zeros. */
char[] nanosChar = new char[nanosString.length()];
nanosString.getChars(0, nanosString.length(), nanosChar, 0);
int truncIndex = 8;
while (nanosChar[truncIndex] == '0') {
truncIndex--;
}
/* Truncate trailing zeros. */
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);
}
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. */
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());
return (timestampBuf.toString());
}
public boolean equals(Timestamp ts) {
if (super.equals(ts)) {
if (nanos == ts.nanos) {
return true;
} else {
return false;
}
} else {
return false;
}
if (super.equals(ts)) {
if (nanos == ts.nanos) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public boolean equals(Object ts) {
if (ts instanceof Timestamp) {
return this.equals((Timestamp)ts);
} else {
return false;
}
if (ts instanceof Timestamp) {
return this.equals((Timestamp) ts);
} else {
return false;
}
}
}