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

Allow constructing a microsoft.sql.DateTimeOffset instance from a java.time.OffsetDateTime value #2340

Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions src/main/java/microsoft/sql/DateTimeOffset.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ private DateTimeOffset(java.sql.Timestamp timestamp, int minutesOffset) {
assert 0 == this.utcMillis % 1000L : "utcMillis: " + this.utcMillis;
}

/**
* Constructs a DateTimeOffset from an existing java.time.OffsetDateTime
*
* @param offsetDateTime
* A java.time.OffsetDateTime value
* @apiNote DateTimeOffset represents values to 100 nanosecond precision. If the java.time.OffsetDateTime instance
* represents a value that is more precise, the value is rounded to the nearest multiple of 100 nanoseconds. Values
* within 50 nanoseconds of the next second are rounded up to the next second.
*/
private DateTimeOffset(java.time.OffsetDateTime offsetDateTime) {
int hundredNanos = ((offsetDateTime.getNano() + 50) / 100);
this.utcMillis = (offsetDateTime.toEpochSecond() * 1000) + (hundredNanos / HUNDRED_NANOS_PER_SECOND * 1000);
this.nanos = 100 * (hundredNanos % HUNDRED_NANOS_PER_SECOND);
this.minutesOffset = offsetDateTime.getOffset().getTotalSeconds() / 60;
}

/**
* Converts a java.sql.Timestamp value with an integer offset to the equivalent DateTimeOffset value
*
Expand Down Expand Up @@ -105,6 +121,20 @@ public static DateTimeOffset valueOf(java.sql.Timestamp timestamp, Calendar cale
(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000));
}

/**
* Directly converts a {@link java.time.OffsetDateTime} value to an equivalent {@link DateTimeOffset} value
*
* @param offsetDateTime
* A java.time.OffsetDateTime value
* @return The DateTimeOffset value of the input java.time.OffsetDateTime
* @apiNote DateTimeOffset represents values to 100 nanosecond precision. If the java.time.OffsetDateTime instance
* represents a value that is more precise, the value is rounded to the nearest multiple of 100 nanoseconds. Values
* within 50 nanoseconds of the next second are rounded up to the next second.
*/
public static DateTimeOffset valueOf(java.time.OffsetDateTime offsetDateTime) {
return new DateTimeOffset(offsetDateTime);
}

/** formatted value */
private String formattedValue = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,17 @@ public void testGetLocalDateTimeUnstorable() throws Exception {
}
}

@Test
public void testDateTimeOffsetValueOfOffsetDateTime() throws Exception {
OffsetDateTime expected = OffsetDateTime.now().withSecond(58).withNano(0);
OffsetDateTime roundUp = expected.withSecond(57).withNano(999999950);
OffsetDateTime roundDown = expected.withSecond(58).withNano(49);

assertEquals(expected, DateTimeOffset.valueOf(expected).getOffsetDateTime());
assertEquals(expected, DateTimeOffset.valueOf(roundUp).getOffsetDateTime());
assertEquals(expected, DateTimeOffset.valueOf(roundDown).getOffsetDateTime());
}

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