Created
February 20, 2019 10:11
-
-
Save adamhopkinson/d972230d48a9b368bc2536b1bf5c7e71 to your computer and use it in GitHub Desktop.
BigQuery Standard SQL UDF to apply a timezone offset to a timestamp
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
-- Takes a timestamp (in seconds) and a timezone offset (eg +0730) and returns a timestamp | |
CREATE TEMPORARY FUNCTION applyTZOffsetToUnixTimestamp(ts INT64, tzOffset STRING) | |
RETURNS INT64 AS ( | |
CASE | |
WHEN SUBSTR(tzOffset, 1, 1) = '+' THEN ts + (CAST(SUBSTR(tzOffset, 2, 2) AS INT64) * 60 * 60) + (CAST(SUBSTR(tzOffset, 4, 2) AS INT64) * 60) | |
WHEN SUBSTR(tzOffset, 1, 1) = '-' THEN ts - (CAST(SUBSTR(tzOffset, 2, 2) AS INT64) * 60 * 60) - (CAST(SUBSTR(tzOffset, 4, 2) AS INT64) * 60) | |
ELSE NULL | |
END | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment