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

GH-35633: [R] R builds failing with error 'Invalid: Timestamps already have a timezone: 'UTC'. Cannot localize to 'UTC'' #35671

Merged
merged 1 commit into from
May 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions r/R/dplyr-funcs-datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ register_bindings_datetime_utility <- function() {
tz <- Sys.timezone()
}

# if a timestamp does not contain timezone information (i.e. it is
# If a timestamp does not contain timezone information (i.e. it is
# "timezone-naive") we can attach timezone information (i.e. convert it into
# a "timezone-aware" timestamp) with `assume_timezone`
# if we want to cast to a different timezone, we can only do it for
# timezone-aware timestamps, not for timezone-naive ones
if (!is.null(tz)) {
# timezone-aware timestamps, not for timezone-naive ones.
# strptime in Acero will return a timezone-aware timestamp if %z is
# part of the format string.
if (!is.null(tz) && !grepl("%z", format, fixed = TRUE)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how timezones work in R (and with POSIXlt and POSIXct), but with the above change, the tz keyword will just be ignored when the string includes an offset?

That doesn't match fully what base R does:

> strptime("2020-05-01T00:00+0100", format="%Y-%m-%dT%H:%M%z")
[1] "2020-05-01 01:00:00"
> strptime("2020-05-01T00:00+0100", format="%Y-%m-%dT%H:%M%z", tz="US/Eastern")
[1] "2020-04-30 19:00:00"

(but again, I don't really know how to interpret those returned values, and whether they are tz "aware" or "native" or even if those concepts even exist in R. I looked at the $zone and $gmtoff attributes of the above return values, but can't make sense of it)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a naive datetime type in R...everything is timezone aware, and tz = "" means "your local timezone" (but everything is UTC internally). What you've encountered here is a fun type called the POSIXlt which is sort of like a data frame that stores components in separate vectors (the POSIXct is a more normal version of it, which is seconds from the unix epoch as a double vector).

It's a good point that this doesn't match what base R does...I think we'd want to cast() if grepl("%z") and assume_timezone() otherwise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this helps with understanding dates/times in R, but the tz argument won't affect the underlying point in time (just the attribute of the output and, to great confusion of many, how it is printed):

unclass(as.POSIXct(strptime("2020-05-01T00:00+0100", format="%Y-%m-%dT%H:%M%z")))
#> [1] 1588287600
#> attr(,"tzone")
#> [1] ""
unclass(as.POSIXct(strptime("2020-05-01T00:00+0100", tz = "UTC", format="%Y-%m-%dT%H:%M%z")))
#> [1] 1588287600
#> attr(,"tzone")
#> [1] "UTC"
unclass(as.POSIXct(strptime("2020-05-01T00:00+0100", tz = "America/Halifax", format="%Y-%m-%dT%H:%M%z")))
#> [1] 1588287600
#> attr(,"tzone")
#> [1] "America/Halifax"

Created on 2023-05-24 with reprex v2.0.2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good point that this doesn't match what base R does...I think we'd want to cast() if grepl("%z") and assume_timezone() otherwise?

Yes, that sounds correct

output <- Expression$create(
"assume_timezone",
output,
Expand Down