Skip to content

Commit

Permalink
Adds spark support for convert_timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
clausherther committed Nov 27, 2020
1 parent 5f24102 commit dd2c5c2
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions macros/calendar_date/convert_timezone.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,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 -%}
{%- 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}}'))
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 -%}

0 comments on commit dd2c5c2

Please sign in to comment.