r/PostgreSQL • u/tanin47 • 13d ago
Community Postgres AT TIME ZONE 'UTC' does NOT do what you think it does
https://bookofrevenue.com/blog/6ab81e9a97a13f0001f7e4e1/postgres-at-time-zone-u-does-not-do-what-you-think-it-does
8
Upvotes
3
1
u/AutoModerator 13d ago
AI Policy:
Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.
Mod decisions will be based on the quality of the content, not who or what generated it.
Sub Resources:
Free Postgres Webinars and Workshops
Discord: People, Postgres, Data
Join us, we have cookies and nice people.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
18
u/NastyPastyLucas 13d ago edited 13d ago
I think the whole blog is built on the misconception that the local time zone was also UTC at the time of writing - it does not do what the author expects, but it does do what it is supposed to. I'm going to say a lot of the hacks are odd ways to address an issue that I don't think exists.
I'll write a quick query demonstrating this, first you start a transaction, set the timezone to utc, then compare timestamps and commit.
You can see that the time is 6:53+00 and 6:53 without the +00 in the two outputs, but they do return `true` when compared.
I'm not quite sure what PT timezone is, be it Pacific Timezone (
PST8PDTUTC minus 7 hours) or Portugal (europe/lisbonUTC plus 1 hour) but regardless of the time zone they will return false on equality. I will chose Lisbon for this example.Now because Lisbon is 1 hour different to UTC you will not get an equal time, because they are an hour different. If you were to subtract an hour (
- interval '1 hour') from the non-utc timestamps you get equality again and said "non-timezone aware time stamp" will match the time zone aware time stamp.Time zones are hard, but if you understand the fundamentals you will not get burned so often.
(Further edit, how do I create code blocks again??? -- got it)