Postgres 日期和时间混乱

问题描述 投票:0回答:1

为什么这两个语句产生不同的日期?

select 'test 1', date_trunc('day', current_date at time zone 'utc' )
union
select 'test 2', date_trunc('day',current_timestamp  at time zone 'utc' )

当我输入时(英国当地时间下午 2:20),第一个语句告诉我今天是 5 月 16 日(昨天),第二个语句告诉我是 5 月 17 日。

current_date
current_timestamp
现在都应该提供值,我从文档中了解到,current基本上意味着local,对我来说,现在本地和UTC之间只有一小时的差异。

我错过了什么?

postgresql datetime-format
1个回答
0
投票

试试这个。 Current_date 为您提供没有时间的日期(一天的开始),如果您将其截断为“天”,那么今天还没有结束,我想所以它四舍五入到昨天。

https://mode.com/blog/date-trunc-sql-timestamp-function-count-on

select current_date, 'test 1', date_trunc('day', current_date at time zone 'utc-3' )
union
select current_timestamp, 'test 2', date_trunc('day', current_timestamp at time zone 'utc' )
© www.soinside.com 2019 - 2024. All rights reserved.