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

Question: ISO 8601 representations with reduced accuracy #361

Closed
ramiromagno opened this issue Oct 15, 2023 · 2 comments
Closed

Question: ISO 8601 representations with reduced accuracy #361

ramiromagno opened this issue Oct 15, 2023 · 2 comments

Comments

@ramiromagno
Copy link

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 clock? 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 clock 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
@trevorld
Copy link
Contributor

There is two parts of your questions: 1) reduced precision and 2) missing (lower precision) components

  1. {clock} supports formatting at reduced precision. You can also explicitly set the precision of clock datetimes and the formatting will automatically adjust although I think you can also adjust the precision in the formatting methods with a custom format string:
library("clock")
Sys.time() |> as_year_month_day() |> calendar_narrow("hour")
<year_month_day<hour>[1]>
[1] "2023-10-14T19"
  1. Maybe something has changed recently but last I checked {clock} didn't have good support for unknown components when you have a known more precise component. If this is important for you then for now you may want to look at {parttime} and/or (my own) {datetimeoffset}. In particular {datetimeoffset} has some limited support for the Extended Date/Time Format (EDTF) extension of ISO 8601:
library("datetimeoffset")
# Unknown year with known month and day
as_datetimeoffset("XXXX-12-15") |> format_edtf()
[1] "XXXX-12-15"
# Unknown month with known year and day
as_datetimeoffset("2003-XX-15") |> format_edtf()
[1] "2003-XX-15"

@ramiromagno
Copy link
Author

Thank you Trevor, will look into these packages!

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

3 participants