Use toTimeZone() and related functions to safely shift DateTime and DateTime64 values between time zones in ClickHouse.
Reports built for global users require timestamps to appear in each user’s local zone.ClickHouse stores DateTime values in UTC by default, so explicit conversion prevents misleading dates and broken aggregations.
toTimeZone(datetime, 'Zone') shifts a DateTime into the target IANA zone.
toDateTime(datetime, 'Zone') parses a string as DateTime in the given zone.
toDateTime64(datetime64, 'Zone') behaves the same for sub-second precision.
Join Orders with Customers, then wrap order_date in toTimeZone(order_date, Customers.preferred_tz).This returns each order in the shopper’s local time for receipts or analytics.
SELECT id, toTimeZone(order_date, 'US/Eastern') AS order_date_estFROM OrdersLIMIT 5;
Yes. Combine toTimeZone() with formatDateTime(): formatDateTime(toTimeZone(order_date,'Europe/Berlin'),'%Y-%m-%d %H:%M')
outputs a ready-to-display string.
• Store source timestamps in UTC.
• Keep zone identifiers in a dimension table (e.g., Customers.preferred_tz).
• Avoid hard-coding offsets; use IANA names so DST is handled automatically.
Hard-coding offsets: ‘+02:00’ ignores DST.Fix by using ‘Europe/Berlin’.
Converting twice: Applying toTimeZone() on already shifted data returns wrong times. Track conversion state in the query.
toDateTime parses strings; toTimeZone shifts existing DateTime values. Use both when ingesting text then converting.
No. Conversions happen at query time. Store UTC in primary keys for optimal index usage.
.
Yes. Given the same timestamp and zone, it always returns the same value, considering historical DST rules shipped with ClickHouse.
Parse with toDateTime('2024-03-10 02:30:00','America/New_York')
. It stores UTC but interprets the string in the supplied zone.
Run SELECT name FROM system.time_zones ORDER BY name;
to see all IANA identifiers ClickHouse recognizes.