Skip to content

Commit e13c63b

Browse files
committed
yet another time zone test
1 parent 779d9c9 commit e13c63b

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.instant;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import org.hibernate.annotations.JdbcTypeCode;
10+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
11+
import org.hibernate.testing.orm.junit.Jpa;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.time.Instant;
15+
import java.time.LocalDateTime;
16+
import java.time.OffsetDateTime;
17+
import java.time.ZoneOffset;
18+
import java.time.temporal.ChronoUnit;
19+
20+
import static org.hibernate.type.SqlTypes.TIMESTAMP;
21+
import static org.hibernate.type.SqlTypes.TIMESTAMP_WITH_TIMEZONE;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
24+
@Jpa(annotatedClasses = InstantTest.Instants.class)
25+
public class InstantTest {
26+
@Test
27+
public void test(EntityManagerFactoryScope scope) {
28+
Instant now = Instant.now();
29+
ZoneOffset zone = ZoneOffset.ofHours(1);
30+
scope.getEntityManagerFactory()
31+
.runInTransaction( entityManager -> {
32+
Instants instants = new Instants();
33+
instants.instantInUtc = now;
34+
instants.instantInLocalTimeZone = now;
35+
instants.instantWithTimeZone = now;
36+
instants.localDateTime = LocalDateTime.ofInstant( now, zone );
37+
instants.offsetDateTime = OffsetDateTime.ofInstant( now, zone );
38+
instants.localDateTimeUtc = LocalDateTime.ofInstant( now, ZoneOffset.UTC );
39+
instants.offsetDateTimeUtc = OffsetDateTime.ofInstant( now, ZoneOffset.UTC );
40+
entityManager.persist( instants );
41+
} );
42+
scope.getEntityManagerFactory()
43+
.runInTransaction( entityManager -> {
44+
Instants instants = entityManager.find( Instants.class, 0 );
45+
assertEqualInstants( now, instants.instantInUtc );
46+
assertEqualInstants( now, instants.instantInLocalTimeZone );
47+
assertEqualInstants( now, instants.instantWithTimeZone );
48+
assertEqualInstants( now, instants.offsetDateTime.toInstant() );
49+
assertEqualInstants( now, instants.localDateTime.toInstant( zone ) );
50+
assertEqualInstants( now, instants.offsetDateTimeUtc.toInstant() );
51+
assertEqualInstants( now, instants.localDateTimeUtc.toInstant( ZoneOffset.UTC ) );
52+
} );
53+
}
54+
void assertEqualInstants(Instant x, Instant y) {
55+
assertEquals( x.truncatedTo( ChronoUnit.SECONDS ), y.truncatedTo( ChronoUnit.SECONDS ) );
56+
}
57+
@Entity(name="Instants")
58+
static class Instants {
59+
@Id long id;
60+
Instant instantInUtc;
61+
@JdbcTypeCode(TIMESTAMP) Instant instantInLocalTimeZone;
62+
@JdbcTypeCode(TIMESTAMP_WITH_TIMEZONE) Instant instantWithTimeZone;
63+
LocalDateTime localDateTime;
64+
OffsetDateTime offsetDateTime;
65+
LocalDateTime localDateTimeUtc;
66+
OffsetDateTime offsetDateTimeUtc;
67+
}
68+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.instant;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import org.hibernate.annotations.JdbcTypeCode;
10+
import org.hibernate.cfg.JdbcSettings;
11+
import org.hibernate.cfg.MappingSettings;
12+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
13+
import org.hibernate.testing.orm.junit.Jpa;
14+
import org.hibernate.testing.orm.junit.Setting;
15+
import org.junit.jupiter.api.Test;
16+
17+
import java.time.Instant;
18+
import java.time.LocalDateTime;
19+
import java.time.OffsetDateTime;
20+
import java.time.ZoneOffset;
21+
import java.time.temporal.ChronoUnit;
22+
23+
import static org.hibernate.type.SqlTypes.TIMESTAMP;
24+
import static org.hibernate.type.SqlTypes.TIMESTAMP_WITH_TIMEZONE;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
27+
@Jpa(annotatedClasses = InstantTest2.Instants.class,
28+
integrationSettings = {@Setting(name= MappingSettings.TIMEZONE_DEFAULT_STORAGE,
29+
value = "NORMALIZE"),
30+
@Setting(name= JdbcSettings.JDBC_TIME_ZONE, value = "+01:00")})
31+
public class InstantTest2 {
32+
@Test
33+
public void test(EntityManagerFactoryScope scope) {
34+
Instant now = Instant.now();
35+
ZoneOffset zone = ZoneOffset.of("+01:00");// ZoneOffset.ofHours(1);
36+
scope.getEntityManagerFactory()
37+
.runInTransaction( entityManager -> {
38+
Instants instants = new Instants();
39+
instants.instantInUtc = now;
40+
instants.instantInLocalTimeZone = now;
41+
instants.instantWithTimeZone = now;
42+
instants.localDateTime = LocalDateTime.ofInstant( now, zone );
43+
instants.offsetDateTime = OffsetDateTime.ofInstant( now, zone );
44+
instants.localDateTimeUtc = LocalDateTime.ofInstant( now, ZoneOffset.UTC );
45+
instants.offsetDateTimeUtc = OffsetDateTime.ofInstant( now, ZoneOffset.UTC );
46+
entityManager.persist( instants );
47+
} );
48+
scope.getEntityManagerFactory()
49+
.runInTransaction( entityManager -> {
50+
Instants instants = entityManager.find( Instants.class, 0 );
51+
assertEqualInstants( now, instants.instantInUtc );
52+
assertEqualInstants( now, instants.instantInLocalTimeZone );
53+
assertEqualInstants( now, instants.instantWithTimeZone );
54+
assertEqualInstants( now, instants.offsetDateTime.toInstant() );
55+
assertEqualInstants( now, instants.localDateTime.toInstant( zone ) );
56+
assertEqualInstants( now, instants.offsetDateTimeUtc.toInstant() );
57+
assertEqualInstants( now, instants.localDateTimeUtc.toInstant( ZoneOffset.UTC ) );
58+
} );
59+
}
60+
void assertEqualInstants(Instant x, Instant y) {
61+
assertEquals( x.truncatedTo( ChronoUnit.SECONDS ), y.truncatedTo( ChronoUnit.SECONDS ) );
62+
}
63+
64+
@Entity(name="Instants")
65+
static class Instants {
66+
@Id long id;
67+
Instant instantInUtc;
68+
@JdbcTypeCode(TIMESTAMP) Instant instantInLocalTimeZone;
69+
@JdbcTypeCode(TIMESTAMP_WITH_TIMEZONE) Instant instantWithTimeZone;
70+
LocalDateTime localDateTime;
71+
OffsetDateTime offsetDateTime;
72+
LocalDateTime localDateTimeUtc;
73+
OffsetDateTime offsetDateTimeUtc;
74+
}
75+
}

0 commit comments

Comments
 (0)