Skip to content

Commit

Permalink
adding date() and datetime() short hand macros (#112)
Browse files Browse the repository at this point in the history
* adding date() and datetime() short hand macros

* adding test for date() and datetime() macros

* Rename macro file

* Add tz default handling

* Update tests

* outputing datetime object and casting in tests

* updating README with date and datetime marco instructions

---------

Co-authored-by: Claus Herther <[email protected]>
  • Loading branch information
gregology and clausherther authored Oct 24, 2023
1 parent 2169d5c commit 7a7cd80
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ For example, use `America/New_York` for East Coast Time.

* [get_fiscal_periods](#get_fiscal_periodsdates-year_end_month-week_start_day-shift_year1)

## Utils

* [date](#dateyear-month-day)
* [datetime](#datetimeyear-month-day-hour0-minute0-second0-microsecond0-tznone)

## Documentation

### [get_base_dates](macros/get_base_dates.sql)(`start_date=None, end_date=None, n_dateparts=None, datepart="day"`)
Expand Down Expand Up @@ -804,6 +809,30 @@ or, optionally, you can override the default timezone:
{{ dbt_date.yesterday(tz="America/New_York") }} as date_yesterday
```

### [date](macros/_utils/modules_datetime.sql)(`year`, `month`, `day`)

Reduces the boilerplate syntax required to produce a `date` object. This is not converted to a string to allow pythonic manipulation.

Usage:
```sql
{% set date_object = dbt_date.date(1997, 9, 29) %}
```

### [datetime](macros/_utils/modules_datetime.sql)(`year`, `month`, `day`, `hour=0`, `minute=0`, `second=0`, `microsecond=0`, `tz=None`)

Reduces the boilerplate syntax required to produce a `datetime` object. This is not converted to a string to allow pythonic manipulation.

Usage:
```sql
{% set datetime_object = dbt_date.datetime(1997, 9, 29, 6, 14) %}
```

or, optionally, you can override the default timezone:

```sql
{% set datetime_object = dbt_date.datetime(1997, 9, 29, 6, 14, tz='America/New_York') %}
```

## Integration Tests (Developers Only)

This project contains integration tests for all test macros in a separate `integration_tests` dbt project contained in this repo.
Expand Down
8 changes: 6 additions & 2 deletions integration_tests/macros/get_test_dates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ select
{{ dbt_date.last_month_name(short=True) }} as last_month_name_short,
{{ dbt_date.next_month_number() }} as next_month_number,
{{ dbt_date.next_month_name(short=False) }} as next_month_name,
{{ dbt_date.next_month_name(short=True) }} as next_month_name_short
{{ dbt_date.next_month_name(short=True) }} as next_month_name_short,
cast('{{ modules.datetime.date(1997, 9, 29) }}' as date) as datetime_date,
cast('{{ modules.datetime.datetime(1997, 9, 29, 6, 14, 0, tzinfo=modules.pytz.timezone(var("dbt_date:time_zone"))) }}' as {{ dbt.type_timestamp() }}) as datetime_datetime

union all

Expand Down Expand Up @@ -64,7 +66,9 @@ select
{{ dbt_date.last_month_name(short=True) }} as last_month_name_short,
{{ dbt_date.next_month_number() }} as next_month_number,
{{ dbt_date.next_month_name(short=False) }} as next_month_name,
{{ dbt_date.next_month_name(short=True) }} as next_month_name_short
{{ dbt_date.next_month_name(short=True) }} as next_month_name_short,
cast('{{ modules.datetime.date(1997, 9, 29) }}' as date) as datetime_date,
cast('{{ modules.datetime.datetime(1997, 9, 29, 6, 14, 0, tzinfo=modules.pytz.timezone(var("dbt_date:time_zone"))) }}' as {{ dbt.type_timestamp() }}) as datetime_datetime

{%- endmacro %}

Expand Down
4 changes: 4 additions & 0 deletions integration_tests/models/test_dates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ models:
expression: "next_month_name = {{ dbt_date.next_month_name(short=False) }}"
- expression_is_true:
expression: "next_month_name_short = {{ dbt_date.next_month_name(short=True) }}"
- expression_is_true:
expression: "datetime_date = cast('{{ dbt_date.date(1997, 9, 29) }}' as date)"
- expression_is_true:
expression: "datetime_datetime = cast('{{ dbt_date.datetime(1997, 9, 29, 6, 14) }}' as {{ dbt.type_timestamp() }})"


columns:
Expand Down
14 changes: 14 additions & 0 deletions macros/_utils/modules_datetime.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% macro date(year, month, day) %}
{{ return(modules.datetime.date(year, month, day)) }}
{% endmacro %}

{% macro datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tz=None) %}
{% set tz = tz if tz else var("dbt_date:time_zone") %}
{{ return(
modules.datetime.datetime(
year=year, month=month, day=day, hour=hour,
minute=minute, second=second, microsecond=microsecond,
tzinfo=modules.pytz.timezone(tz)
)
) }}
{% endmacro %}

0 comments on commit 7a7cd80

Please sign in to comment.