A Brightlane regional warehouse logged an inventory event at 09:30 Indian Standard Time (UTC+05:30) on 2024-06-15. The engineering team needs the moment recorded as a timezone-aware timestamp.
Write a query to return the string '2024-06-15 09:30:00+05:30' cast as a timezone-aware timestamp.
Output:
- A single row with one column,
ist_event, typed as a timezone-aware timestamp.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
'2024-06-15 09:30:00+05:30'::TIMESTAMPTZ AS ist_event The shape
The +05:30 inside the literal tells PostgreSQL that the wall-clock reading is in Indian Standard Time, and the ::timestamptz cast triggers the UTC conversion at parse time. The stored value is the absolute moment that 09:30 IST corresponds to, which is 04:00 UTC.
Clause by clause
SELECT '2024-06-15 09:30:00+05:30'::timestamptz AS ist_eventevaluates the literal once and returns it as aTIMESTAMPTZ. PostgreSQL reads09:30as the wall clock and+05:30as the offset, subtracts the offset to land on UTC, and stores2024-06-15 04:00:00+00internally. The displayed value depends on the session zone; the stored value is fixed.- There is no
FROMbecause no table is being read.
Why the explicit ::timestamptz cast
The +05:30 in the string is what lets PostgreSQL compute the absolute moment, but the destination type still has to be declared. Without ::timestamptz, the literal resolves as text and the offset is preserved only as characters in a string. The downstream system would not know that 09:30+05:30 is the same moment as 04:00+00. The cast is what turns the offset from a visible character into a load-bearing fact about the value.
The trap
'2024-06-15 09:30:00'::timestamptz (no offset in the literal) would also produce a TIMESTAMPTZ, but PostgreSQL would assume the session's current time zone for the conversion. The same query would produce a different absolute moment when run from a session set to a different zone. The IST offset has to live inside the literal, where it is documented and load-bearing, not inferred from whatever session happens to run the query.
You practiced casting a non-UTC offset literal to TIMESTAMPTZ — PostgreSQL converts the value to UTC at write time using the literal's offset.