Created
April 11, 2014 22:09
-
-
Save bdmac/10505604 to your computer and use it in GitHub Desktop.
Problem with timezone query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TEMP TABLE reservations ( | |
reservation_id serial | |
, start_on timestamptz NOT NULL | |
, time_zone text); -- we don't need this | |
INSERT INTO reservations (start_on, time_zone) VALUES | |
('2014-04-11 00:00+10', 'Australia/Sydney') -- start date 4/11, use midnight in Sydney | |
, ('2014-04-12 00:00+10', 'Australia/Sydney') -- start date 4/12, use midnight in Sydney | |
, ('2014-04-11 00:00-07', 'America/Los_Angeles') -- start date 4/11, use midnight in SF | |
, ('2014-04-12 00:00-07', 'America/Los_Angeles'); -- start date 4/12, use midnight in SF | |
-- Now pretend current wall time just turned 4/12 00:00 PDT (-07) | |
-- We expect to get the reservation starting on 4/12 in Sydney | |
-- where it is currently 4/12 5PM and the reservation starting | |
-- on 4/12 in SF where it is midnight on 4/12. | |
SELECT start_on, time_zone | |
, start_on::timestamp AS local_ts | |
, start_on AT TIME ZONE time_zone AS ts_at_tz | |
, current_date::timestamptz AS lower_bound | |
, (current_date + 1)::timestamptz AS upper_bound | |
FROM reservations | |
WHERE start_on >= ('2014-04-12')::timestamptz | |
AND start_on < ('2014-04-13')::timestamptz; | |
-- We only find the reservation starting on 4/12 in SF though because they | |
-- condition of the query excludes the Sydney reservation which had its | |
-- start_on shifted to 2014-04-11 14:00+00 even though that day is not | |
-- actually over yet. | |
-- This is why I switched to using tsrange because then the start_on is | |
-- an actual range and you can check for overlaps. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment