Skip to content

Commit 94152bc

Browse files
committed
Allow constructing a microsoft.sql.DateTimeOffset instance from a java.time.OffsetDateTime value
1 parent cfb018a commit 94152bc

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/main/java/microsoft/sql/DateTimeOffset.java

+28
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ private DateTimeOffset(java.sql.Timestamp timestamp, int minutesOffset) {
7272
assert 0 == this.utcMillis % 1000L : "utcMillis: " + this.utcMillis;
7373
}
7474

75+
/**
76+
* Constructs a DateTimeOffset from an existing java.time.OffsetDateTime
77+
*
78+
* @param offsetDateTime A java.time.OffsetDateTime value
79+
* @apiNote DateTimeOffset represents values to 100ns precision. If the java.time.OffsetDateTime instance represents
80+
* a value that is more precise, values in excess of the 100ns precision are rounded to the nearest
81+
* multiple of 100ns.
82+
*/
83+
private DateTimeOffset(java.time.OffsetDateTime offsetDateTime) {
84+
int hundredNanos = ((offsetDateTime.getNano() + 50) / 100);
85+
this.utcMillis = offsetDateTime.toEpochSecond() * 1000;
86+
this.nanos = 100 * (hundredNanos % HUNDRED_NANOS_PER_SECOND);
87+
this.minutesOffset = offsetDateTime.getOffset().getTotalSeconds() / 60;
88+
}
89+
7590
/**
7691
* Converts a java.sql.Timestamp value with an integer offset to the equivalent DateTimeOffset value
7792
*
@@ -105,6 +120,19 @@ public static DateTimeOffset valueOf(java.sql.Timestamp timestamp, Calendar cale
105120
(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000));
106121
}
107122

123+
/**
124+
* Directly converts a {@link java.time.OffsetDateTime} value to an equivalent {@link DateTimeOffset} value
125+
*
126+
* @param offsetDateTime A java.time.OffsetDateTime value
127+
* @return The DateTimeOffset value of the input java.time.OffsetDateTime
128+
* @apiNote DateTimeOffset represents values to 100ns precision. If the java.time.OffsetDateTime instance represents
129+
* a value that is more precise, values in excess of the 100ns precision are rounded to the nearest
130+
* multiple of 100ns.
131+
*/
132+
public static DateTimeOffset valueOf(java.time.OffsetDateTime offsetDateTime) {
133+
return new DateTimeOffset(offsetDateTime);
134+
}
135+
108136
/** formatted value */
109137
private String formattedValue = null;
110138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
3+
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
4+
*/
5+
6+
package microsoft.sql;
7+
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.DisplayName;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.time.OffsetDateTime;
13+
import java.time.ZoneOffset;
14+
15+
public class DateTimeOffsetTest {
16+
17+
@Test
18+
@DisplayName("DateTimeOffset.valueOf(offsetDateTime) instantiates correct DateTimeOffset instances.")
19+
void valueOfOffsetDateTime() {
20+
int nanos = 123456789;
21+
int hundredNanos = 1234568; // DateTimeOffset has a precision of 100 nanos of second
22+
OffsetDateTime offsetDateTime = OffsetDateTime.of(
23+
2024,
24+
2,
25+
25,
26+
23,
27+
55,
28+
6,
29+
nanos,
30+
ZoneOffset.ofHoursMinutes(1, 30)
31+
);
32+
Assertions
33+
.assertEquals(
34+
offsetDateTime.withNano(hundredNanos * 100),
35+
DateTimeOffset.valueOf(offsetDateTime).getOffsetDateTime()
36+
);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)