英文:
JPA, Postgresql and SQL INSERT statement: UTC timestamp not working properly
问题
我编写了一个简单的Java实体,其中包含两个Instant字段:
@Entity
@Table(name = "tb_foo")
public class Foo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE")
private Instant date1;
@Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE")
private Instant date2;
...
然后我在项目的资源根目录中编写了一个简单的import.sql
来测试UTC/本地时间是否正常工作:
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22T20:50:01.12345Z', NOW());
由于我的本地时区是GMT-3,上述的20:50:01应该在客户端显示为17:50:01,并且NOW()
应该显示为我的本地时间(在我测试的时刻大约是0:25)。我在H2数据库中测试过,效果完美:
这是JPA自动生成的DDL和INSERT到H2的代码:
create table tb_foo (
id bigint generated by default as identity,
date1 TIMESTAMP WITHOUT TIME ZONE,
date2 TIMESTAMP WITHOUT TIME ZONE,
primary key (id)
)
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22T20:50:01.12345Z', NOW())
然而,当我在Postgres(版本12.x)上测试相同的Java和SQL代码时,它并没有按预期工作(20点的UTC显示为20点而不是17点):
这是JPA自动生成的DDL和INSERT到Postgresql的代码:
create table tb_foo (
id int8 generated by default as identity,
date1 TIMESTAMP WITHOUT TIME ZONE,
date2 TIMESTAMP WITHOUT TIME ZONE,
primary key (id)
)
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22T20:50:01.12345Z', NOW())
我错过了什么吗?
更新:
我进行了另一个测试:我通过Java编写了另一个数据库插入(而不是SQL INSERT):
Foo foo = new Foo(null, Instant.parse("2020-07-23T20:50:01Z"), Instant.now());
fooRepository.save(foo);
这次,字面的UTC时间戳20:50被正确地插入到了Postgresql中!在pgadmin客户端上显示为17:50!所以也许正确的问题是:
如何在SQL INSERT调用中正确指定UTC时间戳,以便将其存储在没有时区的时间戳中?
我尝试了两种方式:
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22T20:50:01.12345Z', NOW());
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22 20:50:01 +00', NOW());
两者都没有起作用(在GMT-03系统中,它们都显示为20:50,而不是17:50)。
英文:
I wrote a simple Java entity with two Instant fields:
@Entity
@Table(name = "tb_foo")
public class Foo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE")
private Instant date1;
@Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE")
private Instant date2;
...
Then I wrote a simple import.sql in the project's resources root folder to test if UTC/Local is working:
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22T20:50:01.12345Z', NOW());
As my local time zone is GMT-3, that 20:50:01 above should appear on client as 17:50:01, and that NOW() should appear equivalent to my local time (around 0:25 at the moment I tested). I tested it in H2 database and it worked perfectly:
This is the DDL & INSERT auto generated by JPA to H2:
create table tb_foo (
id bigint generated by default as identity,
date1 TIMESTAMP WITHOUT TIME ZONE,
date2 TIMESTAMP WITHOUT TIME ZONE,
primary key (id)
)
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22T20:50:01.12345Z', NOW())
However, when I tested the same Java and SQL codes on Postgres (12.x), it didn't work (20h UTC showed as 20 instead of 17):
This is the DDL & INSERT auto generated by JPA to Postgresql:
create table tb_foo (
id int8 generated by default as identity,
date1 TIMESTAMP WITHOUT TIME ZONE,
date2 TIMESTAMP WITHOUT TIME ZONE,
primary key (id)
)
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22T20:50:01.12345Z', NOW())
What did I miss?
Update:
I've made another test: I wrote another database insertion through Java (not SQL INSERT):
Foo foo = new Foo(null, Instant.parse("2020-07-23T20:50:01Z"), Instant.now());
fooRepository.save(foo);
And the literal UTC timestamp 20:50 was properly inserted into Postgresql this time! It appeared as 17:50 on pgadmin client! So maybe the right question is:
How to properly specify a literal UTC timestamp to Postgresql in a SQL INSERT call to store it in a timestamp WITHOUT time zone?
I've tried it in two ways:
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22T20:50:01.12345Z', NOW());
INSERT INTO tb_foo (date1, date2) VALUES ('2020-07-22 20:50:01 +00', NOW());
And both didn't work (they both show 20:50 instead of 17:50 on the pgadmin client in a GMT-03 system).
答案1
得分: 1
可能是因为:如果在没有时区的时间的输入中指定了一个时区,它会被静默忽略。它会忽略你的 'Z',并插入所呈现的日期时间。
链接:https://www.postgresql.org/docs/12/datatype-datetime.html
英文:
Probably because of: If a time zone is specified in the input for time without time zone, it is silently ignored. It ignores your 'Z' and inserts the datetime as presented.
答案2
得分: 0
明白了。要在 SQL 的 INSERT 语句中指定一个 UTC 时间戳字面值,以便在不带时区的 Postgresql 时间戳字段上工作,你必须首先声明 TIMESTAMP WITH TIME ZONE
:
INSERT INTO tb_foo (date1, date2) VALUES (TIMESTAMP WITH TIME ZONE '2020-07-23 20:50:01.12345+00', NOW());
或者
INSERT INTO tb_foo (date1, date2) VALUES (TIMESTAMP WITH TIME ZONE '2020-07-23T20:50:01.12345Z', NOW());
这对于 H2 数据库也有效。因此,这是一种可以同时在两个数据库中工作的 SQL 脚本提供方式。
英文:
Got it. To specify an UTC timestamp literal in a SQL INSERT to work on a Postgresql timestamp field WITHOUT time zone, you must declare TIMESTAMP WITH TIME ZONE
first:
INSERT INTO tb_foo (date1, date2) VALUES (TIMESTAMP WITH TIME ZONE '2020-07-23 20:50:01.12345+00', NOW());
or
INSERT INTO tb_foo (date1, date2) VALUES (TIMESTAMP WITH TIME ZONE '2020-07-23T20:50:01.12345Z', NOW());
That also worked for H2 database. So this is a way to provide a SQL script that works for both databases.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论