Skip to content

Commit

Permalink
Permit constructing a microsoft.sql.DateTimeOffset instance from a ja…
Browse files Browse the repository at this point in the history
…va.time.OffsetDateTime value (#2340)
  • Loading branch information
arashi01 committed Mar 15, 2024
1 parent eae6d7b commit 26803d2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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

0 comments on commit 26803d2

Please sign in to comment.