mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Merge pull request #2708 from FOCONIS/fix-localtime
Part 1: Fix read/write/query LocalTime values
This commit is contained in:
@@ -13,11 +13,10 @@ public interface DataTimeZone {
|
||||
Calendar getTimeZone();
|
||||
|
||||
/**
|
||||
* Return the Calendar to use for Timezone information when reading/writing date.
|
||||
* A 'date' only value has normally no timezone information, but some platforms (like MySQL)
|
||||
* reqire this.
|
||||
* Return the Calendar to use for Timezone information when reading/writing a time component (date only/time only).
|
||||
* A time component has normally no timezone information, but some platforms (like MySQL) reqire this.
|
||||
*/
|
||||
default Calendar getDateTimeZone() {
|
||||
default Calendar getTimeComponentTimeZone() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ public class MySqlDataTimeZone implements DataTimeZone {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calendar getDateTimeZone() {
|
||||
public Calendar getTimeComponentTimeZone() {
|
||||
return zone;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ public class DataBind implements DataBinder {
|
||||
|
||||
@Override
|
||||
public final void setDate(java.sql.Date v) throws SQLException {
|
||||
Calendar timeZone = dataTimeZone.getDateTimeZone();
|
||||
Calendar timeZone = dataTimeZone.getTimeComponentTimeZone();
|
||||
if (timeZone != null) {
|
||||
pstmt.setDate(++pos, v, timeZone);
|
||||
} else {
|
||||
@@ -178,7 +178,7 @@ public class DataBind implements DataBinder {
|
||||
|
||||
@Override
|
||||
public final void setTime(Time v) throws SQLException {
|
||||
Calendar timeZone = dataTimeZone.getTimeZone();
|
||||
Calendar timeZone = dataTimeZone.getTimeComponentTimeZone();
|
||||
if (timeZone != null) {
|
||||
pstmt.setTime(++pos, v, timeZone);
|
||||
} else {
|
||||
|
||||
@@ -107,7 +107,7 @@ public class RsetDataReader implements DataReader {
|
||||
|
||||
@Override
|
||||
public final Date getDate() throws SQLException {
|
||||
Calendar cal = dataTimeZone.getDateTimeZone();
|
||||
Calendar cal = dataTimeZone.getTimeComponentTimeZone();
|
||||
if (cal != null) {
|
||||
return rset.getDate(pos(), cal);
|
||||
} else {
|
||||
@@ -156,7 +156,7 @@ public class RsetDataReader implements DataReader {
|
||||
|
||||
@Override
|
||||
public final Time getTime() throws SQLException {
|
||||
Calendar cal = dataTimeZone.getTimeZone();
|
||||
Calendar cal = dataTimeZone.getTimeComponentTimeZone();
|
||||
if (cal != null) {
|
||||
return rset.getTime(pos(), cal);
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.tests.timezone;
|
||||
|
||||
import io.ebean.Database;
|
||||
import io.ebean.DatabaseFactory;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
import java.time.LocalTime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class LocalTimeTest {
|
||||
|
||||
protected String platform="h2";
|
||||
protected Database db;
|
||||
|
||||
@BeforeAll
|
||||
public void startTest() {
|
||||
db = createServer("GMT"); // test uses GMT database
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public void shutdown() {
|
||||
if (db != null) {
|
||||
db.find(MLocalTime.class).delete();
|
||||
db.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The test checks the write and read of LocalTime values. The database is in GMT time zone.
|
||||
* In order to verify the test in different java time zones (where the application runs),
|
||||
* use the <code>-Duser.timezone</code> as JVM argument,
|
||||
* e.g. <code>-Duser.timezone="America/New_York"</code> or <code>-Duser.timezone="PST"</code>>
|
||||
* or any other timezone: <a href="https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/">https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/</a>.
|
||||
*/
|
||||
@Test
|
||||
public void testLocalTime() {
|
||||
LocalTime lt = LocalTime.of(5, 15, 15);
|
||||
assertThat(db.find(MLocalTime.class).findCount()).isEqualTo(0);
|
||||
db.sqlUpdate("insert into mlocal_time (id, local_time) values (1, '05:15:15')").execute();
|
||||
|
||||
int count = db.find(MLocalTime.class).where().eq("local_time", lt).findCount();
|
||||
assertThat(count).isEqualTo(1);
|
||||
|
||||
MLocalTime dbModel = db.find(MLocalTime.class).where().eq("local_time", lt).findOne();
|
||||
assertThat(dbModel.getLocalTime().toString()).isEqualTo(lt.toString());
|
||||
}
|
||||
|
||||
private Database createServer(String dbTimeZone) {
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setName(platform);
|
||||
config.loadFromProperties();
|
||||
config.setDdlExtra(false);
|
||||
config.setDefaultServer(false);
|
||||
config.setRegister(false);
|
||||
config.setChangeLogAsync(false);
|
||||
config.addClass(MLocalTime.class);
|
||||
|
||||
config.setDumpMetricsOnShutdown(false);
|
||||
config.setDataTimeZone(dbTimeZone);
|
||||
|
||||
return DatabaseFactory.create(config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.tests.timezone;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import java.time.LocalTime;
|
||||
|
||||
@Entity
|
||||
public class MLocalTime {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
@Nullable
|
||||
private LocalTime localTime;
|
||||
|
||||
@Nullable
|
||||
public LocalTime getLocalTime() {
|
||||
return localTime;
|
||||
}
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user