Skip to content

date_difference(), calendar_difference(), time_point_difference() #266

@DavisVaughan

Description

@DavisVaughan
date_difference(x, ...)
date_difference.Date(x, ..., precision = y/q/m/w/d)
date_difference.POSIXt(x, ..., precision = y/q/m/w/d/H/M/S)

calendar_difference(x, ...)
calendar_difference.year_month_day(x, ..., precision = y/q/m)

time_point_difference(x, ...)
time_point_difference.year_month_day(x, ..., precision = w/d/H/M/S/subsecond

General idea is that these result in a data frame containing columns like year, month, ... down through the most precise component. If you were to add the components back to from in order of largest to smallest (does order matter?) bypassing intermediate invalid dates and time zone issues (by working with calendars and time points) then you'd end up with to

This would make it easy to compute someone's "age" in years as that is just date_difference(a, b)$year https://stackoverflow.com/questions/3611314/calculate-ages-in-r/25450756#25450756

Possibly first pass way to compute the months:

(date1.Year - date2.Year) * 12 + date1.Month - date2.Month + (date1.Day >= date2.Day ? 0 : -1)

Need to be very careful about the precisions that are allowed for calendar vs time point. I think ideally they don't overlap? i.e. day goes with time point.

The precision is used for the "starting" precision. i.e. precision = "month" would result in a data frame where the first column is the total number of whole months between the dates (although, this might be unnecessary cause you can always compute that from the year*12?)

Possibly the calendar and time point versions return data frames of duration columns, while the date version returns data frames of integer columns

Possibly allow from > to, resulting in negative components. But this may be hard to define. It may "fall out" if we can define the algebraic property of the result. i.e. from + (components) = to

Non-year-month-day calendars would be useful too. like determining the number of iso weeks between dates

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions