tidyverse / lubridate

Make working with dates in R just that little bit easier
https://lubridate.tidyverse.org
GNU General Public License v3.0
724 stars 207 forks source link

Question: ISO 8601 representations with reduced accuracy #1142

Open ramiromagno opened 10 months ago

ramiromagno commented 10 months ago

ISO 8601 allows for "representations with reduced accuracy," meaning that you can omit certain components of the date and time if they are not relevant or available. Here are some examples:

Date Only:
    YYYY-MM-DD (complete)
    YYYY-MM (reduced accuracy, no day)
    YYYY (reduced accuracy, no month or day)

Time Only:
    HH:MM:SS (complete)
    HH:MM (reduced accuracy, no seconds)
    HH (reduced accuracy, no minutes or seconds)

Combined Date and Time:
    YYYY-MM-DDTHH:MM:SS (complete)
    YYYY-MM-DDTHH:MM (reduced accuracy, no seconds)
    YYYY-MM-DDTHH (reduced accuracy, no minutes or seconds)

Is this supported in lubridate? I mean, is there a way to generate ISO 8601 strings with missing components, and indicate how those missing components are represented, e.g. with an hyphen? Are lubridate functions' implementation, in one way or the other, always going through strftime() or strptime()? As far as I understand these functions always impute the missing values to 0. Is there any low-level parser that collects all date-time components into a list before missing elements are converted to default values?

Here are examples of ISO 8601 dates or date-times that illustrate what I would like to achieve:

From Precision To
December 15, 2003 13:14 Unknown seconds 2003-12-15T13:14
December 15, 2003 13 Unknown minutes and seconds 2003-12-15T13
December 15, 2003 Unknown time 2003-12-15
December, 2003 Unknown day and time 2003-12
2003 Unknown month, day, and time 2003
Between 10:00 and 10:30 on the morning of December 15, 2003 2003-12-15T10:00/2003-12-15T10:30
Between the first of this year (2003) until "now" (February 15, 2003) 2003-01-01/2003-02-15
December 15, 2003 ??:15 Unknown hour with known minutes 2003-12-15T-:15
December 15, 2003 13:??:17 Unknown minutes with known date, hours, and seconds 2003-12-15T13:-:17
The 15th of some month in 2003, time not collected Unknown month and time with known year and day 2003---15
December 15, but can't remember the year, time not collected Unknown year with known month and day --12-15
7:15 of some unknown date Unknown date with known hour and minute -----T07:15