簡體   English   中英

PostgreSQL:計算夏令時中一天中的小時數

[英]PostgreSQL: Compute number of hours in the day on daylight savings time

我正在嘗試提出一個查詢,該查詢將正確地計算出有25個小時的夏時制。 我的表具有一列名為timely_timestamp的timestampz類型的列。 到目前為止,我得到的錯誤答案如下所示:

select EXTRACT(epoch FROM tomorrow-today)/3600
from(
  select date_trunc('day', timezone('America/New_York', hourly_timestamp) as today ,  
         date_trunc('day', timezone('America/New_York', hourly_timestamp))) 
                                                        + '1 day'::interval as tomorrow
    )t;

在夏令時期間執行此查詢時,我仍然只能得到24小時,而不是25小時。有什么主意如何正確執行此操作?

您首先必須提取到紀元,然后進行計算:

WITH test AS (
    SELECT  '2014-10-26'::timestamptz at time zone 'America/New_York' AS today,
        '2014-10-27'::timestamptz at time zone 'America/New_York' AS tomorrow
)
SELECT  
    extract(epoch from tomorrow)  - extract(epoch from today)       AS seconds,  -- 90000
    (extract(epoch from tomorrow) - extract(epoch from today)) / 3600   AS hours -- 25
FROM    test;

小時數隨時鍾而變化。

with hours as (
  select (timestamp with time zone '2014-11-01 00:00:00 America/New_York' + (n || ' hour')::interval) as hourly_timestamp
  from generate_series(0, 72) n
)
select hourly_timestamp
     , hourly_timestamp + interval '1' day as one_day_later
     , hourly_timestamp + interval '1' day - hourly_timestamp as elapsed_time
from hours;
hourly_timestamp         one_day_later            elapsed_time
--
[snip]
2014-11-01 22:00:00-04   2014-11-02 22:00:00-05   1 day 01:00:00
2014-11-01 23:00:00-04   2014-11-02 23:00:00-05   1 day 01:00:00
2014-11-02 00:00:00-04   2014-11-03 00:00:00-05   1 day 01:00:00
2014-11-02 01:00:00-04   2014-11-03 01:00:00-05   1 day 01:00:00
2014-11-02 01:00:00-05   2014-11-03 01:00:00-05   1 day
2014-11-02 02:00:00-05   2014-11-03 02:00:00-05   1 day
2014-11-02 03:00:00-05   2014-11-03 03:00:00-05   1 day
2014-11-02 04:00:00-05   2014-11-03 04:00:00-05   1 day
[snip]

請注意,重復01:00,但偏移量不同。 夏令時結束於02:00,時鍾回退並在01:00到02:00之間重復一小時,但是由於夏令時結束,因此UTC與美國/紐約時區之間存在五個小時。

這個類似的查詢顯示日期,而不是時間戳。

with dates as (
  select (timestamp with time zone '2014-11-01 00:00:00 America/New_York' + (n || ' day')::interval) as daily_timestamp
  from generate_series(0, 2) n
)
select daily_timestamp::date
     , (daily_timestamp + interval '1' day)::date as one_day_later
     , daily_timestamp + interval '1' day - daily_timestamp as elapsed_time
from dates;
daily_timestamp  one_day_later  elapsed_time
--
2014-11-01       2014-11-02     1 day
2014-11-02       2014-11-03     1 day 01:00:00
2014-11-03       2014-11-04     1 day

你哪里出問題了? 通過計算截斷時間信息經過的時間。 (日期沒有與之關聯的時區。)如果我進行第二個查詢並將“ daily_timestamp”轉換為公用表表達式中的日期,那么我也可以獲得24小時。

with dates as (
  select (timestamp with time zone '2014-11-01 00:00:00 America/New_York' + (n || ' day')::interval)::date as daily_timestamp
  from generate_series(0, 2) n
)
select daily_timestamp::date
     , (daily_timestamp + interval '1' day)::date as one_day_later
     , daily_timestamp + interval '1' day - daily_timestamp as elapsed_time
from dates;
daily_timestamp  one_day_later  elapsed_time
--
2014-11-01       2014-11-02     1 day
2014-11-02       2014-11-03     1 day
2014-11-03       2014-11-04     1 day

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM