forked from calogica/dbt-date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_timezone.sql
24 lines (21 loc) · 1.02 KB
/
convert_timezone.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{%- macro convert_timezone(column, target_tz=None, source_tz=None) -%}
{%- set source_tz = "UTC" if not source_tz else source_tz -%}
{%- set target_tz = var("dbt_date:time_zone") if not target_tz else target_tz -%}
{{ adapter.dispatch('convert_timezone', packages = dbt_date._get_utils_namespaces()) (column, target_tz, source_tz) }}
{%- endmacro -%}
{% macro default__convert_timezone(column, target_tz, source_tz) -%}
{%- if not source_tz -%}
cast(convert_timezone('{{ target_tz }}', {{ column }}) as {{ dbt_utils.type_timestamp() }})
{%- else -%}
cast(convert_timezone('{{ source_tz }}', '{{ target_tz }}', {{ column }}) as {{ dbt_utils.type_timestamp() }})
{%- endif -%}
{%- endmacro -%}
{%- macro bigquery__convert_timezone(column, target_tz, source_tz=None) -%}
timestamp(datetime({{ column }}, '{{ target_tz}}'))
{%- endmacro -%}
{%- macro spark__convert_timezone(column, target_tz, source_tz) -%}
from_utc_timestamp(
to_utc_timestamp({{ column }}, '{{ source_tz }}'),
'{{ target_tz }}'
)
{%- endmacro -%}