Add time unit to date · dateadd

🟢 4 of 4 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc3 (9e1c81a6) · 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["Add time unit to date · dateadd<br/>🟢4"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds["dateadd(DS, n, p)"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_comp["calc dateadd(comp, n, p)"]:::ok
    end
    subgraph G_sc["Scalar"]
        direction LR
        v_sc_date["dateadd(date, n, p)"]:::ok
        v_sc_time_period["dateadd(time_period, n, p)"]:::ok
    end
    O --- G_ds
    O --- G_comp
    O --- G_sc
    style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
    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_ds href "#form-ds"
    click v_comp href "#form-comp"
    click v_sc_date href "#form-sc-date"
    click v_sc_time_period href "#form-sc-time-period"
    

Types

Takes integer, time, date, time_period, duration · Gives time, date, any type

Comes from · integer, time, date, time_period, duration — 29 operators in 6 blocks 🟢29
General purpose operators (2) 🟢2
String operators (2) 🟢2
Numeric operators (4) 🟢4
Time operators (16) 🟢16
Aggregate and Analytic operators (2) 🟢2
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 34 operators in 7 blocks 🟢34
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Clause operators (9) 🟢9

Goes to: whatever takes its input's type

Goes to · time, date — 12 operators in 3 blocks 🟢12
General purpose operators (1) 🟢1
Time operators (10) 🟢10
Definitions (VTL-DL) (1) 🟢1
Goes to · operators that take any scalar — 49 operators in 9 blocks 🟢49
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (10) 🟢10
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Definitions (VTL-DL) (1) 🟢1

Forms

dateadd(DS, n, p) 🟢 · time, integer, duration → time, any type

Shifts the time identifier 🟢 pandas ✅ · DuckDB ✅
Tests:

dateadd(DS_1, 1, "M"), the page’s valid syntax DS_r := dateadd(DS_1, 1, “M”), on a dataset whose one time component is the date identifier Id_2.

Manual:

Add time unit to date: the operand is dataset{ identifier < time > _ , identifier _* }, and “the result Data Set has the same Identifier, Measure and Attribute Components as the operand Data Set”. The type names no time measure, only the time identifier, so Id_2 moves and Me_1 is kept; the page does not say it in words, and DS_1 allows no other reading.

Expected:

One month later, keeping the day as “dateadd (2020-02-10, 1, “M”) gives 2020-03-10”: 2019-02-15 and 2020-03-10, every component keeping its type.

script.vtl
DS_r := dateadd(DS_1, 1, "M");
Input DS_1
Id_1Id_2Me_1
A2019-01-1510
B2020-02-1020
Expected DS_r
Id_1Id_2Me_1
A2019-02-1510
B2020-03-1020

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

calc dateadd(comp, n, p) 🟢 · date, time_period, integer, duration → date

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

dateadd(Me_1, 2, "M") inside calc: two months after each date.

Manual:

Add time unit to date, Example 1, output verbatim: “adding months to a given date returns the date plus integer months”, and “In the component version, that can be used in a calc clause, a new component of type date is added to the dataset.”

Expected:

Me_2 is 2019-03-01, 2019-09-01 and 2021-02-28, two months after 2020-12-31 being the last day of February 2021.

script.vtl
DS_r:= DS_1[calc Me_2 := dateadd(Me_1, 2, "M")];
Input DS_1
Id_1Me_1
G2019-01-01
H2019-07-01
T2020-12-31
Expected DS_r
Id_1Me_1Me_2
G2019-01-012019-03-01
H2019-07-012019-09-01
T2020-12-312021-02-28

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

dateadd(date, n, p) 🟢 · date, integer, duration → date

One month later 🟢 pandas ✅ · DuckDB ✅
Tests:

dateadd(SC_1, 1, "M") with SC_1 = 2020-02-10: the date is an input scalar, as VTL has no date literal, and the period indicator a string, as in Manual example 1.

Manual:

Add time unit to date: “adding months to a given date returns the date plus integer months”, and “dateadd (2020-02-10, 1, “M”) gives 2020-03-10”.

Expected:

DS_r = 2020-03-10, a Date.

script.vtl
DS_r := dateadd(SC_1, 1, "M");
Input scalars
SC_1
2020-02-10
Expected scalars

Name

Type

Value

DS_r

Date

"2020-03-10"

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

One year later 🟢 pandas ✅ · DuckDB ✅
Tests:

dateadd(SC_1, 1, "A") with SC_1 = 2020-02-10: one year added to a date.

Manual:

Add time unit to date: “adding years to a given date returns the date plus integer years; for years the “Y” is used”, and “dateadd (2020-02-10, 1, “Y”) gives 2021-02-10”.

Expected:

DS_r = 2021-02-10, a Date, the page’s result.

Adaptation:

The year is written A, not the page’s “Y”: VTL prescribes no representation of durations, and the chapter’s list of period indicators gives A for a year and says that “each VTL system can personalize the representation of time, date, time_period and duration as desired”. vtlengine’s docs/data_types.rst gives the same codes for Duration, and the manual’s default conversion from string to duration (Type conversion) reads A, S, Q, M, W or D.

script.vtl
DS_r := dateadd(SC_1, 1, "A");
Input scalars
SC_1
2020-02-10
Expected scalars

Name

Type

Value

DS_r

Date

"2021-02-10"

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

dateadd(time_period, n, p) 🟢 · time_period, integer, duration → date

From the last day of the quarter 🟢 pandas ✅ · DuckDB ✅
Tests:

dateadd(SC_1, 5, "M") on a scalar time period, SC_1 = 2022Q1, the page’s valid syntax dateadd (2022Q1, 5, “M”).

Manual:

Add time unit to date: “The last day of the time period is assumed as the starting date”, and “The scalar version of this operator takes as input one date or time_period value and returns a date”.

Expected:

DS_r = 2022-08-31, a Date: 2022Q1 ends on 2022-03-31, and “adding months to a given date returns the date plus integer months”: five months later is August 31, August having 31 days.

script.vtl
DS_r := dateadd(SC_1, 5, "M");
Input scalars
SC_1
2022Q1
Expected scalars

Name

Type

Value

DS_r

Date

"2022-08-31"

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