Days between two dates · datediff

🟢 4 of 4 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc2 (90ee9376) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
Dataset formsComponent formsScalar forms
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart TB
    classDef ok fill:#d4edda,stroke:#28a745,color:#000
    classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
    classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
    classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
    classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
    classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
    classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
    O["Days between two dates · datediff<br/>🟢4"]:::ok
    subgraph G_comp["Component"]
        direction LR
        v_comp_comp["calc datediff(comp, comp)"]:::ok
    end
    subgraph G_sc["Scalar"]
        direction LR
        v_sc_date["datediff(date, date)"]:::ok
        v_sc_time_period["datediff(time_period, time_period)"]:::ok
        v_sc_mixed["datediff(time_period, date)"]:::ok
    end
    O --- G_comp
    O --- G_sc
    style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
    style G_sc fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 2 3
    click v_comp_comp href "#form-comp-comp"
    click v_sc_date href "#form-sc-date"
    click v_sc_time_period href "#form-sc-time-period"
    click v_sc_mixed href "#form-sc-mixed"
    

Types

Takes date, time_period · Gives integer

Goes to · integer — 44 operators in 8 blocks 🟢44
General purpose operators (1) 🟢1
String operators (2) 🟢2
Numeric operators (18) 🟢18
Time operators (6) 🟢6
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (10) 🟢10
Data validation operators (2) 🟢2
Definitions (VTL-DL) (4) 🟢4
Goes to · operators that take any scalar — 46 operators in 8 blocks 🟢46
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (8) 🟢8
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9

Forms

calc datediff(comp, comp) 🟢 · date, time_period → integer

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

datediff(Id_2, Me_1) inside calc, from a date identifier to a time_period measure.

Manual:

Days between two dates, Example 1, output verbatim: “The operator datediff returns the number of days between two dates or time periods. The last day of the time period is assumed as the starting/ending date.”

Expected:

2020Q2 ends on 2020-06-30 and 2021Q1 on 2021-03-31, so Me_2 is 546, 639 and 90, Integers.

script.vtl
DS_r:= DS_1[calc Me_2 := datediff(Id_2, Me_1)];
Input DS_1
Id_1Id_2Me_1
G2019-01-012020Q2
G2019-07-012021Q1
T2020-12-312021Q1
Expected DS_r
Id_1Id_2Me_1Me_2
G2019-01-012020Q2546
G2019-07-012021Q1639
T2020-12-312021Q190

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

datediff(date, date) 🟢 · date → integer

Across the new year 🟢 pandas ✅ · DuckDB ✅
Tests:

datediff(SC_1, SC_2) of two scalar dates, SC_1 = 2020-12-14 and SC_2 = 2021-04-20, the page’s valid syntax datediff (2020-12-14,2021-04-20), with the dates given in the default YYYY-MM-DD form, as VTL has no date literal.

Manual:

Days between two dates: “The operator datediff returns the number of days between two dates or time periods.”

Expected:

DS_r = 127, an Integer: 17 days to the end of 2020, then 31, 28 (2021 is not a leap year), 31 and 20.

script.vtl
DS_r := datediff(SC_1, SC_2);
Input scalars
SC_1SC_2
2020-12-142021-04-20
Expected scalars

Name

Type

Value

DS_r

Integer

127

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗

datediff(time_period, time_period) 🟢 · time_period → integer

Last days of two quarters 🟢 pandas ✅ · DuckDB ✅
Tests:

datediff(SC_1, SC_2) of two scalar time periods, SC_1 = 2022Q1 and SC_2 = 2023Q2, the page’s valid syntax datediff (2022Q1, 2023Q2).

Manual:

Days between two dates: “The last day of the time period is assumed as the starting/ending date.”

Expected:

DS_r = 456, an Integer: from 2022-03-31, the end of 2022Q1, to 2023-03-31 is 365 days, with no February 29 in between, then 30, 31 and 30 days to 2023-06-30, the end of 2023Q2.

script.vtl
DS_r := datediff(SC_1, SC_2);
Input scalars
SC_1SC_2
2022Q12023Q2
Expected scalars

Name

Type

Value

DS_r

Integer

456

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗

datediff(time_period, date) 🟢 · time_period, date → integer

Quarter end to a date 🟢 pandas ✅ · DuckDB ✅
Tests:

datediff(SC_1, SC_2) of a time period and a date, SC_1 = 2021Q2 and SC_2 = 2021-11-04, the page’s valid syntax datediff (2021Q2, 2021-11-04).

Manual:

Days between two dates: “The last day of the time period is assumed as the starting/ending date”. The result is on the Extract time period page, whose Example 2, misplaced there, says that datediff (2021Q2, 2021-11-04); “returns 127”.

Expected:

DS_r = 127, an Integer: 2021Q2 ends on 2021-06-30, then come 31, 31, 30, 31 and 4 days.

script.vtl
DS_r := datediff(SC_1, SC_2);
Input scalars
SC_1SC_2
2021Q22021-11-04
Expected scalars

Name

Type

Value

DS_r

Integer

127

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗