简体   繁体   English

在夏令时转换期间区分时间

[英]Distinguishing between times during a daylight savings changeover

I was looking through some outputs that were presented from a UK timezone that crossed the daylight savings changeover (For any unaware, during the morning hours of the last Sunday of October, 1am is repeated to subtract an hour, and likewise on the last Sunday of March, it shifts forwards by 1 hour).我正在查看从英国时区提供的一些输出,这些输出跨越了夏令时转换(对于任何不知道的人,在 10 月的最后一个星期日的早上,重复凌晨 1 点以减去一个小时,同样在最后一个星期日3 月,它向前移动 1 小时)。 The output showed events that appeared to go backwards in time - obviously this is a result of the hour resetting, however without any time offset listings, the only logical way to ascertain the correct time was to see their order. output 显示出现在 go 的事件在时间上倒退 - 显然这是小时重置的结果,但是没有任何时间偏移列表,确定正确时间的唯一合乎逻辑的方法是查看它们的顺序。 I understand that Postgres stores timestamp with timezone internally at UTC, but when timezone is specified the offset is ommitted eg我知道 Postgres 在 UTC 内部存储带有时区的时间戳,但是当指定时区时,偏移量被省略,例如

select
    '2019-10-27 00:30:00 UTC'::timestamptz at time zone 'Europe/London' time1,
    '2019-10-27 01:30:00 UTC'::timestamptz at time zone 'Europe/London' time2,
    '2020-03-29 00:30:00 UTC'::timestamptz at time zone 'Europe/London' time3,
    '2020-03-29 01:30:00 UTC'::timestamptz at time zone 'Europe/London' time4;

Below you can see October changeovers will give the exact same time during the 00:30 and 01:30 period, but it does not display the timezone offset so there is no way to tell the difference between the two.您可以在下面看到 10 月的转换将在 00:30 和 01:30 期间给出完全相同的时间,但它不显示时区偏移量,因此无法区分两者之间的差异。

        time1        |        time2        |        time3        |        time4        
---------------------+---------------------+---------------------+---------------------
 2019-10-27 01:30:00 | 2019-10-27 01:30:00 | 2020-03-29 00:30:00 | 2020-03-29 02:30:00

This is obviously not just a problem that affects only Postgres, and the output was to a logfile that does not use offsets annoyingly so there would be no clear way to retrospectively cast the times.这显然不仅仅是一个仅影响 Postgres 的问题,而且 output 是一个不使用偏移量的日志文件,因此没有明确的方法来追溯时间。 I was lucky that the order does tell the difference, but supposing there was only a single event during such a 2 hour period what would be the best practice to handle this (reasonably irregular) issue in the future?我很幸运,订单确实说明了差异,但假设在这样的 2 小时内只有一个事件,那么将来处理这个(合理不规则的)问题的最佳做法是什么? Can offsets be shown only during these periods or is there some other solution?只能在这些期间显示偏移量还是有其他解决方案?

The solution is simple: don't log local timestamps.解决方案很简单:不要记录本地时间戳。

Either log the time zone information along with the timestamps or convert them to UTC before logging:将时区信息与时间戳一起记录或在记录之前将它们转换为 UTC:

SET timezone = 'Europe/London';

select                         
    '2019-10-27 00:30:00 UTC'::timestamptz time1,
    '2019-10-27 01:30:00 UTC'::timestamptz time2,
    '2020-03-29 00:30:00 UTC'::timestamptz time3,
    '2020-03-29 01:30:00 UTC'::timestamptz time4;

         time1          |         time2          |         time3          |         time4          
------------------------+------------------------+------------------------+------------------------
 2019-10-27 01:30:00+01 | 2019-10-27 01:30:00+00 | 2020-03-29 00:30:00+00 | 2020-03-29 02:30:00+01
(1 row)

select                         
    '2019-10-27 00:30:00 UTC'::timestamptz at time zone 'UTC' time1,
    '2019-10-27 01:30:00 UTC'::timestamptz at time zone 'UTC' time2,
    '2020-03-29 00:30:00 UTC'::timestamptz at time zone 'UTC' time3,
    '2020-03-29 01:30:00 UTC'::timestamptz at time zone 'UTC' time4;

        time1        |        time2        |        time3        |        time4        
---------------------+---------------------+---------------------+---------------------
 2019-10-27 00:30:00 | 2019-10-27 01:30:00 | 2020-03-29 00:30:00 | 2020-03-29 01:30:00
(1 row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM