10
10
import java .time .ZoneId ;
11
11
import java .time .ZoneOffset ;
12
12
import java .time .ZonedDateTime ;
13
+ import java .time .temporal .ChronoField ;
14
+ import java .time .temporal .ChronoUnit ;
13
15
import java .util .Collection ;
14
16
import java .util .List ;
15
17
16
18
import org .hibernate .cfg .Configuration ;
19
+ import org .hibernate .dialect .Dialect ;
20
+ import org .hibernate .dialect .MySQLDialect ;
21
+ import org .hibernate .dialect .SybaseDialect ;
17
22
import org .hibernate .reactive .BaseReactiveTest ;
18
23
import org .hibernate .reactive .annotations .DisabledFor ;
19
24
31
36
import static org .hibernate .cfg .AvailableSettings .TIMEZONE_DEFAULT_STORAGE ;
32
37
import static org .hibernate .reactive .containers .DatabaseConfiguration .DBType .DB2 ;
33
38
import static org .hibernate .reactive .testing .ReactiveAssertions .assertWithTruncationThat ;
34
- import static org .hibernate .type .descriptor .DateTimeUtils .roundToDefaultPrecision ;
39
+ import static org .hibernate .type .descriptor .DateTimeUtils .adjustToDefaultPrecision ;
35
40
36
41
@ Timeout (value = 10 , timeUnit = MINUTES )
37
42
@ DisabledFor (value = DB2 , reason = "Exception: IllegalStateException: Needed to have 6 in buffer but only had 0" )
@@ -51,8 +56,24 @@ protected void setProperties(Configuration configuration) {
51
56
52
57
@ Test
53
58
public void test (VertxTestContext context ) {
54
- ZonedDateTime nowZoned = ZonedDateTime .now ().withZoneSameInstant ( ZoneId .of ( "CET" ) );
55
- OffsetDateTime nowOffset = OffsetDateTime .now ().withOffsetSameInstant ( ZoneOffset .ofHours ( 3 ) );
59
+ final ZonedDateTime nowZoned ;
60
+ final OffsetDateTime nowOffset ;
61
+ final Dialect dialect = getDialect ();
62
+ if ( dialect instanceof SybaseDialect || dialect instanceof MySQLDialect ) {
63
+ // Sybase has 1/300th sec precision
64
+ nowZoned = ZonedDateTime .now ().withZoneSameInstant ( ZoneId .of ("CET" ) )
65
+ .with ( ChronoField .NANO_OF_SECOND , 0L );
66
+ nowOffset = OffsetDateTime .now ().withOffsetSameInstant ( ZoneOffset .ofHours (3 ) )
67
+ .with ( ChronoField .NANO_OF_SECOND , 0L );
68
+ }
69
+ else if ( dialect .getDefaultTimestampPrecision () == 6 ) {
70
+ nowZoned = ZonedDateTime .now ().withZoneSameInstant ( ZoneId .of ("CET" ) ).truncatedTo ( ChronoUnit .MICROS );
71
+ nowOffset = OffsetDateTime .now ().withOffsetSameInstant ( ZoneOffset .ofHours (3 ) ).truncatedTo ( ChronoUnit .MICROS );
72
+ }
73
+ else {
74
+ nowZoned = ZonedDateTime .now ().withZoneSameInstant ( ZoneId .of ("CET" ) );
75
+ nowOffset = OffsetDateTime .now ().withOffsetSameInstant ( ZoneOffset .ofHours (3 ) );
76
+ }
56
77
test ( context , getSessionFactory ()
57
78
.withTransaction ( s -> {
58
79
Zoned z = new Zoned ();
@@ -63,11 +84,11 @@ public void test(VertxTestContext context) {
63
84
.thenCompose ( zid -> openSession ()
64
85
.thenCompose ( s -> s .find ( Zoned .class , zid )
65
86
.thenAccept ( z -> {
66
- assertWithTruncationThat ( roundToDefaultPrecision ( z .zonedDateTime .toInstant (), getDialect () ) )
67
- .isEqualTo ( roundToDefaultPrecision ( nowZoned .toInstant (), getDialect () ) );
87
+ assertWithTruncationThat ( adjustToDefaultPrecision ( z .zonedDateTime .toInstant (), getDialect () ) )
88
+ .isEqualTo ( adjustToDefaultPrecision ( nowZoned .toInstant (), getDialect () ) );
68
89
69
- assertWithTruncationThat ( roundToDefaultPrecision ( z .offsetDateTime .toInstant (), getDialect () ) )
70
- .isEqualTo ( roundToDefaultPrecision ( nowOffset .toInstant (), getDialect () ) );
90
+ assertWithTruncationThat ( adjustToDefaultPrecision ( z .offsetDateTime .toInstant (), getDialect () ) )
91
+ .isEqualTo ( adjustToDefaultPrecision ( nowOffset .toInstant (), getDialect () ) );
71
92
72
93
ZoneId systemZone = ZoneId .systemDefault ();
73
94
ZoneOffset systemOffset = systemZone .getRules ().getOffset ( Instant .now () );
0 commit comments