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

Show UTC offset after parsing datetimes #1465

Closed
damianooldoni opened this issue Feb 1, 2023 · 1 comment
Closed

Show UTC offset after parsing datetimes #1465

damianooldoni opened this issue Feb 1, 2023 · 1 comment

Comments

@damianooldoni
Copy link

I like the way readr uses UTC datetimes consistently. I would like to know if there is a way to get the timezone offset specified in a ISO datetime string after reading it with readr. It seems to me that it gets lost. It would be nice to store it somewhere, maybe as an attribute? In example below, the offset I would like to retrieve is "-06:00" which is the ISO way to express offsets.

r <- readr::parse_datetime("2010/01/01 12:00 -0600", "%Y/%m/%d %H:%M %z")
r
#> [1] "2010-01-01 18:00:00 UTC"
attributes(r)
#> $class
#> [1] "POSIXct" "POSIXt" 
#> 
#> $tzone
#> [1] "UTC"
lubridate::tz(r)
#> [1] "UTC"

If the timezone is provided via locale arg, I can retrieve it by using the function tz_offset() from the non tidyverse package lutz, see example below. However, it seems to me more a workaround than a stable, elegant solution:

s <- readr::parse_datetime("2010/01/01 12:00 -0600", "%Y/%m/%d %H:%M %z", 
                    locale = readr::locale(tz = "US/Central"))
s
#> [1] "2010-01-01 12:00:00 CST"
attributes(s)
#> $class
#> [1] "POSIXct" "POSIXt" 
#> 
#> $tzone
#> [1] "US/Central"
lutz::tz_offset(s)
#>      tz_name           date_time zone is_dst utc_offset_h
#> 1 US/Central 2010-01-01 12:00:00  CST  FALSE           -6

Both examples created on 2023-02-01 with reprex v2.0.2

I am interested what you think about this. Thanks in advance.

@hadley
Copy link
Member

hadley commented Jul 31, 2023

I think this is unfortunately out of scope for readr since it doesn't have any control over the data structure that R uses for date times.

@hadley hadley closed this as completed Jul 31, 2023
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

2 participants