You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postgres中current_date与current_timestamp时区处理差异疑问

问题解析:current_date与current_timestamp时区处理差异

你执行的SQL语句如下:

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(夏令时,UTC+1)执行时,两条语句返回不同日期的核心原因是current_date和current_timestamp的本质差异:

  • test 1逻辑:
    current_date仅返回当前本地日期(执行时为5月17日),无时间部分。当用at time zone 'utc'转换时,PostgreSQL会自动将该日期补为本地时间的00:00:00。
    英国夏令时比UTC快1小时,因此本地5月17日00:00转换为UTC后是5月16日23:00,经date_trunc('day')截断到天,结果为5月16日。

  • test 2逻辑:
    current_timestamp返回包含当前具体时间的带时区时间戳(执行时为英国本地14:20,即UTC+1的14:20)。转换为UTC后是当天的13:20,经date_trunc('day')截断后结果为5月17日。

简言之,current_date转换时区时默认使用本地午夜时间,导致跨天;而current_timestamp使用当前实际时间,转换后仍在当天范围内,因此日期结果不同。

内容的提问来源于stack exchange,提问作者ConanTheGerbil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 03:27:05