Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OffsetDateTime conversion for pre-Gregorian dates #2568

Merged
merged 3 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/main/java/microsoft/sql/DateTimeOffset.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

package microsoft.sql;

import java.time.Instant;
machavan marked this conversation as resolved.
Show resolved Hide resolved
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
Expand Down Expand Up @@ -190,7 +193,6 @@ public String toString() {
.substring(2), // -> "123456"
formattedOffset);
}

return result;
}

Expand Down Expand Up @@ -257,12 +259,32 @@ public java.sql.Timestamp getTimestamp() {
* @return OffsetDateTime equivalent to this DateTimeOffset object.
*/
public java.time.OffsetDateTime getOffsetDateTime() {
java.time.ZoneOffset zoneOffset = java.time.ZoneOffset.ofTotalSeconds(60 * minutesOffset);
java.time.LocalDateTime localDateTime = java.time.LocalDateTime.ofEpochSecond(utcMillis / 1000, nanos,
zoneOffset);
return java.time.OffsetDateTime.of(localDateTime, zoneOffset);
// Format the offset as +hh:mm or -hh:mm. Zero offset is formatted as +00:00.
String formattedOffset = (minutesOffset < 0) ?
String.format(Locale.US, "-%1$02d:%2$02d", -minutesOffset / 60, -minutesOffset % 60) :
String.format(Locale.US, "+%1$02d:%2$02d", minutesOffset / 60, minutesOffset % 60);

// Create a Calendar instance with the time zone set to GMT plus the formatted offset
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT" + formattedOffset), Locale.US);
// Initialize the calendar with the UTC milliseconds value
calendar.setTimeInMillis(utcMillis);

// Extract the date and time components from the calendar
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // Calendar.MONTH is zero-based
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);

// Create the ZoneOffset from the minutesOffset
ZoneOffset offset = ZoneOffset.ofTotalSeconds(minutesOffset * 60);

// Create and return the OffsetDateTime
return OffsetDateTime.of(year, month, day, hour, minute, second, nanos, offset);
}



/**
* Returns this DateTimeOffset object's offset value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,35 @@ public void testDateTimeOffsetValueOfOffsetDateTime() throws Exception {
assertEquals(expected, DateTimeOffset.valueOf(roundUp).getOffsetDateTime());
assertEquals(expected, DateTimeOffset.valueOf(roundDown).getOffsetDateTime());
}

@Test
public void testPreGregorianDateTime() throws Exception {
try (Connection conn = getConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);) {

conn.setAutoCommit(false);
TestUtils.dropTableIfExists(escapedTableName, stmt);

stmt.executeUpdate("CREATE TABLE " + escapedTableName + " (dob datetimeoffset(7) null)");

stmt.executeUpdate("INSERT INTO " + escapedTableName + " VALUES ('1500-12-16 00:00:00.0000000+08:00')");
stmt.executeUpdate("INSERT INTO " + escapedTableName + " VALUES ('1400-09-27 09:30:00.0000000+08:00')");
stmt.executeUpdate("INSERT INTO " + escapedTableName + " VALUES ('2024-12-16 23:40:00.0000000+08:00')");

try (ResultSet rs = stmt.executeQuery("select dob from " + escapedTableName + " order by dob")) {
while (rs.next()) {
String strDateTimeOffset = rs.getString(1).substring(0, 10);
DateTimeOffset objDateTimeOffset = (DateTimeOffset) rs.getObject(1);
OffsetDateTime objOffsetDateTime = objDateTimeOffset.getOffsetDateTime();

String strOffsetDateTime = objOffsetDateTime.toString().substring(0, 10);
assertEquals(strDateTimeOffset, strOffsetDateTime, "Mismatch found in DateTimeOffset : "
+ objDateTimeOffset + " and OffsetDateTime : " + objOffsetDateTime);
}
}
TestUtils.dropTableIfExists(escapedTableName, stmt);
}
}

static LocalDateTime getUnstorableValue() throws Exception {
ZoneId systemTimezone = ZoneId.systemDefault();
Expand Down
Loading