Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider "time diff" helpers #494

Open
brookslogan opened this issue Jul 26, 2024 · 0 comments
Open

Consider "time diff" helpers #494

brookslogan opened this issue Jul 26, 2024 · 0 comments

Comments

@brookslogan
Copy link
Contributor

brookslogan commented Jul 26, 2024

Context

Proposal

Consider implementing these helpers:

  • unit_time_diff(time_type): takes a time_type, outputs an object that, added to a time_value of kind time_type, will output the next possible time_value, and which is an acceptable before argument.
    • For "daily": either 1 or as.difftime(1, units = "days")
    • For "weekly": as.difftime(1, units = "weeks")
    • For yearmon: 1
    • etc.
    • This could be mostly S3 except for "daily" vs. "weekly" Dates.
    • (Maybe tack on a "time_diff" class so we "know" that we generated these results.)
  • n_periods_to_time_diff(x, time_type): outputs x * unit_time_diff(time_type) (note recycling rules should mean this works on length != 1 vectors)
  • time_diff_to_n_periods(x, time_type): reverse operation, but allowing for both the numeric and difftime as inputs for "daily", whereas in n_period_to_time_diff we'd just choose to output one or the other. (Or maybe be strict and have an as_time_diff convert to the strict format.)
  • {is,assert}_time_diff(x, time_type): validates whether x looks like it's an appropriate time difference. Like validate_slide_window_arg but maybe not allowing Inf
  • [time_diff(time_values, time_type) (like diff()), time_sub(time_values1, time_values2, time_type) (like -)? with time_sub maybe catching mismatched wdays for "weekly" --- again, this is almost but not quite just S3 due to "weekly" Dates, and not sure about tsibble/clock classes]

Use cases:

  • before, after validation in non-Inf case
  • maybe making working with guess_period, complete, etc. easier
  • avoiding slow difftime operations
  • [potentially supersede next_after]
  • [potentially help clarify what type lag should be when we calculate it]

Discussion

@dshemetov @dsweber2 do you see these simplifying life at all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant