Skip to content

[C++] Support rolling backwards and forwards with temporal arithmetic #31334

@asfimport

Description

@asfimport

Original description in ARROW-11090:
"This should also cover the ability to do with and without rollback (so have the ability to do e.g. 2021-03-30 minus 1 month and either get a null back, or 2021-02-28), plus the ability to specify whether to rollback to the first or last, and whether to preserve or rest the time.)"

For example, in R, lubridate has the following functionality:

  • rollbackward() or rollback() which changes a date to the last day of the previous month or to the first day of the current month

  • rollforward() which rolls to the last day of the current month or to the first day of the next month.

  • all of the above also offer the option to preserve hms (hours, minutes and seconds) when rolling.

    This functionality underpins functions such as %m-% and %m+% which are used to add or subtract months to a date without exceeding the last day of the new month.

    {code:r}
    library(lubridate)

    jan <- ymd_hms("2010-01-31 03:04:05")
    jan + months(1:3) # Feb 31 and April 31 returned as NA
    #> [1] NA "2010-03-31 03:04:05 UTC"
    #> [3] NA

  1. NA "2010-03-31 03:04:05 UTC" NA
    jan %m+% months(1:3) # No rollover
    #> [1] "2010-02-28 03:04:05 UTC" "2010-03-31 03:04:05 UTC"
    #> [3] "2010-04-30 03:04:05 UTC"

leap <- ymd("2012-02-29")
"2012-02-29 UTC"
#> [1] "2012-02-29 UTC"
leap %m+% years(1)
#> [1] "2013-02-28"
leap %m+% years(-1)
#> [1] "2011-02-28"
leap %m-% years(1)
#> [1] "2011-02-28"

x <- ymd_hms("2019-01-29 01:02:03")
add_with_rollback(x, months(1))
#> [1] "2019-02-28 01:02:03 UTC"
add_with_rollback(x, months(1), preserve_hms = FALSE)
#> [1] "2019-02-28 UTC"
add_with_rollback(x, months(1), roll_to_first = TRUE)
#> [1] "2019-03-01 01:02:03 UTC"
add_with_rollback(x, months(1), roll_to_first = TRUE, preserve_hms = FALSE)
#> [1] "2019-03-01 UTC"
{code}

Reporter: Dragoș Moldovan-Grünfeld / @dragosmg
Assignee: Rok Mihevc / @rok
Watchers: Rok Mihevc / @rok

Related issues:

Note: This issue was originally created as ARROW-15904. Please see the migration documentation for further details.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions