To effectively manage daylight saving time (DST) changes in BigQuery, follow these guidelines:
Store Timestamps in UTC:
Consistently store all timestamps in UTC using the TIMESTAMP data type. This approach ensures uniformity in time representation, irrespective of DST changes.
Apply Timezone Conversions During Data Retrieval:
Convert UTC timestamps to local timezones as needed during data retrieval. Utilize DATETIME(timestamp_column, timezone_region) for this conversion. This function automatically adjusts for DST in the specified timezone.
Accurately Calculate Time Differences:
Use TIMESTAMP_DIFF to compute the difference between timestamps. When both timestamps are converted to the same timezone, this function accurately accounts for any DST adjustments.
Carefully Handle DST Transitions:
Pay special attention to days where DST starts or ends, as these days might have 23 or 25 hours. While EXTRACT(HOUR FROM timestamp_column) can be used to extract the hour component of a timestamp, it does not directly indicate a DST shift.
Example Query:
Here’s an example SQL query to calculate the hour difference, considering DST changes:
SELECT TIMESTAMP_DIFF(
DATETIME(timestamp_column, "America/Los_Angeles"),
DATETIME(timestamp_column - INTERVAL 1 DAY, "America/Los_Angeles"),
HOUR
) AS hours_difference
FROM your_table
This query assumes timestamp_column is stored in UTC and converts it to a specific timezone, accounting for DST changes.
Key Considerations:
Always store timestamps in UTC to avoid inconsistencies due to local time changes.
Convert timestamps to local timezones during data retrieval, considering DST.
Use TIMESTAMP_DIFF for precise time difference calculations, especially around DST transitions.
Be cautious and aware of the peculiarities of DST transition days.