pg 的毫秒级时间戳转换 - V2EX
yustation

pg 的毫秒级时间戳转换

  •  
  •   yustation Sep 26, 2024 3059 views
    This topic created in 595 days ago, the information mentioned may be changed or developed.
    select to_timestamp('1672531200000'::bigint / 1000) AT TIME ZONE 'UTC-8'; 

    结果为 2023-01-01 08:00:00

    select to_timestamp('1672531200000'::bigint / 1000) AT TIME ZONE 'UTC+8'; 

    结果为 2022-12-31 16:00:00

    为什么'UTC-8'才是北京时间?

    wangee
        1
    wangee  
       Sep 26, 2024
    pg 当中区分 timestamptz 和 timestamp ,可以使用`SHOW timezone;`看看。
    sagaxu
        2
    sagaxu  
       Sep 26, 2024
    因为 select '2023-01-01 08:00:00+08' AT TIME ZONE 'UTC+8' 是'2022-12-31 16:00:00'
    yustation
        3
    yustation
    OP
       Sep 26, 2024
    @wangee Asia/Shanghai ; to_timestamp 返回值是 timestamptz ,是我误解为 timestamp 了,谢谢
    yustation
        4
    yustation  
    OP
       Sep 26, 2024
    @sagaxu 谢谢!点醒我了
    wangee
        5
    wangee  
       Sep 26, 2024
    我更正一下我上面的回复,这个问题来自于 PostgreSQL 中 Time Zone 的格式与 ISO-8601 以及其他 PostgreSQL 中显示的不一致:UTC+8 在 Time Zone 配置中代表 UTC 西边 8 小时,而不是常规的向东 8 小时。参考文档: https://www.postgresql.org/docs/current/datetime-posix-timezone-specs.html

    PostgreSQL 中,timestamp 和 timestamptz 的底层都是 UTC 时间,仅仅是输出形式不同。

    to_timestamp 函数会将 Unix 时间戳转换为 timestamptz ,等价于 timestamp with time zone '2023-01-01 08:00:00+08'。

    at time zone 函数在处理 timestamptz 的时候,输出的转换后时区的 timestamp 。在上面的例子中,将会转换到 UTC+8 时区之后,显示在 UTC+8 显示的时间。参考文档: https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT

    因此需要关注 PostgreSQL 中对于 Time Zone 的表示,一共有三种,参考文档: https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-TIMEZONES 。其中对于第三种,就是我开头第一段写的,和常用的不一致。

    将 OP 的查询语句中的时区改为 Asia/Shanghai ,就是 OP 想要的北京时间:2023-01-01 08:00:00 了。

    之前回复的牛头不对马嘴,抱歉
    yustation
        6
    yustation  
    OP
       Sep 26, 2024
    select to_timestamp('1672531200000'::bigint / 1000) AT TIME ZONE 'Asia/Shanghai'; -- 2023-01-01 08:00:00

    确实如此,受教了!
    yustation
        7
    yustation  
    OP
       Sep 26, 2024
    @wangee 谢谢大佬!
    About     Help     Advertise     Blog     API     FAQ     Solana     5389 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 42ms UTC 05:49 PVG 13:49 LAX 22:49 JFK 01:49
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86