-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow constructing a microsoft.sql.DateTimeOffset instance from a jav…
…a.time.OffsetDateTime value
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made | ||
* available under the terms of the MIT License. See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package microsoft.sql; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
|
||
public class DateTimeOffsetTest { | ||
|
||
@Test | ||
@DisplayName("DateTimeOffset.valueOf(offsetDateTime) instantiates correct DateTimeOffset instances.") | ||
void valueOfOffsetDateTime() { | ||
int nanos = 123456789; | ||
int hundredNanos = 1234568; // DateTimeOffset has a precision of 100 nanos of second | ||
OffsetDateTime offsetDateTime = OffsetDateTime.of( | ||
2024, | ||
2, | ||
25, | ||
23, | ||
55, | ||
6, | ||
nanos, | ||
ZoneOffset.ofHoursMinutes(1, 30) | ||
); | ||
Assertions | ||
.assertEquals( | ||
offsetDateTime.withNano(hundredNanos * 100), | ||
DateTimeOffset.valueOf(offsetDateTime).getOffsetDateTime() | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("DateTimeOffset.valueOf(offsetDateTime) correctly rounds up values within 50 nanoseconds of the next second.") | ||
void valueOfOffsetDateTimeRounding() { | ||
OffsetDateTime offsetDateTime = | ||
OffsetDateTime.now() | ||
.withSecond(58) | ||
.withNano(999999950); | ||
Assertions | ||
.assertEquals( | ||
offsetDateTime | ||
.withSecond(59) | ||
.withNano(0), | ||
DateTimeOffset.valueOf(offsetDateTime).getOffsetDateTime() | ||
); | ||
} | ||
} |