Skip to content

Instantly share code, notes, and snippets.

@adamhopkinson
Created February 20, 2019 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamhopkinson/d972230d48a9b368bc2536b1bf5c7e71 to your computer and use it in GitHub Desktop.
Save adamhopkinson/d972230d48a9b368bc2536b1bf5c7e71 to your computer and use it in GitHub Desktop.
BigQuery Standard SQL UDF to apply a timezone offset to a timestamp
-- 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