Skip to content

Commit 762effb

Browse files
feat: add round timestamp macro (#84)
* feat: add round timestamp macro * Remove refs to dbt_utils * Update integration test * update README Co-authored-by: Claus Herther <claus@calogica.com>
1 parent 65a206f commit 762effb

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ To run the tests:
9090
- [next_week](#next_weektznone)
9191
- [now](#nowtznone)
9292
- [periods_since](#periods_sincedate_col-period_nameday-tznone)
93+
- [round_timestamp](#round_timestamptimestamp)
9394
- [to_unixtimestamp](#to_unixtimestamptimestamp)
9495
- [today](#todaytznone)
9596
- [tomorrow](#tomorrowdatenone-tznone)
@@ -621,6 +622,38 @@ or, optionally, you can override the default timezone:
621622
{{ dbt_date.periods_since("my_timestamp_column", period_name="minute", tz="UTC" }}
622623
```
623624

625+
### [round_timestamp](macros/calendar_date/round_timestamp.sql)(`timestamp`)
626+
627+
Rounds the given timestamp or date to the nearest date (return type is `timestamp`).
628+
629+
```sql
630+
select
631+
{{ dbt_date.round_timestamp("timestamp_col") }} as nearest_date
632+
...
633+
```
634+
635+
A few examples:
636+
637+
```sql
638+
{{ dbt_date.round_timestamp("'2022-02-05 18:45:15'")}}
639+
-- results in 2022-02-06
640+
```
641+
642+
```sql
643+
{{ dbt_date.round_timestamp("'2022-02-05 11:45:15'")}}
644+
-- results in 2022-02-05
645+
```
646+
647+
```sql
648+
{{ dbt_date.round_timestamp("'2022-02-05 12:00:00'")}}
649+
-- results in 2022-02-06
650+
```
651+
652+
```sql
653+
{{ dbt_date.round_timestamp("'2022-02-05 00:00:00'")}}
654+
-- results in 2022-02-05
655+
```
656+
624657
### [to_unixtimestamp](macros/calendar_date/to_unixtimestamp.sql)(`timestamp`)
625658

626659
Gets Unix timestamp (epochs) based on provided timestamp.

integration_tests/macros/get_test_dates.sql

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ select
2020
'Nov' as month_name_short,
2121
1623076520 as unix_epoch,
2222
cast('{{ get_test_timestamps()[0] }}' as {{ type_timestamp() }}) as time_stamp,
23-
cast('{{ get_test_timestamps()[1] }}' as {{ type_timestamp() }}) as time_stamp_utc
23+
cast('{{ get_test_timestamps()[1] }}' as {{ type_timestamp() }}) as time_stamp_utc,
24+
cast('2021-06-07' as {{ type_timestamp() }}) as rounded_timestamp,
25+
cast('2021-06-08' as {{ type_timestamp() }}) as rounded_timestamp_utc
2426

2527
union all
2628

@@ -45,7 +47,9 @@ select
4547
{# 1623051320 as unix_epoch, #}
4648
1623076520 as unix_epoch,
4749
cast('{{ get_test_timestamps()[0] }}' as {{ type_timestamp() }}) as time_stamp,
48-
cast('{{ get_test_timestamps()[1] }}' as {{ type_timestamp() }}) as time_stamp_utc
50+
cast('{{ get_test_timestamps()[1] }}' as {{ type_timestamp() }}) as time_stamp_utc,
51+
cast('2021-06-07' as {{ type_timestamp() }}) as rounded_timestamp,
52+
cast('2021-06-08' as {{ type_timestamp() }}) as rounded_timestamp_utc
4953

5054
{%- endmacro %}
5155

integration_tests/models/test_dates.yml

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ models:
3737
expression: "unix_epoch = {{ dbt_date.to_unixtimestamp('time_stamp_utc') }}"
3838
- dbt_utils.expression_is_true:
3939
expression: "time_stamp = {{ dbt_date.convert_timezone('time_stamp_utc') }}"
40+
- dbt_utils.expression_is_true:
41+
expression: "rounded_timestamp = {{ dbt_date.round_timestamp('time_stamp') }}"
42+
- dbt_utils.expression_is_true:
43+
expression: "rounded_timestamp_utc = {{ dbt_date.round_timestamp('time_stamp_utc') }}"
4044

4145
columns:
4246
- name: date_day
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% macro round_timestamp(timestamp) %}
2+
{{ date_trunc("day", dateadd("hour", 12, timestamp)) }}
3+
{% endmacro %}

0 commit comments

Comments
 (0)