Skip to content

Fix transform_time() because of lubridate incompatibility #489

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ndevln
Copy link

@ndevln ndevln commented Mar 17, 2025

Hi,

this change is really minor, but fixes a strange incompatibility with lubridate.

The bug:

library(scales)
library(lubridate)

fun <- transform_time("UTC")
fun$inverse(as_datetime("2024-01-05")) + days(1)
#> Error: Incompatible classes: <POSIXt> + <Period>

This bug occurs in the wild, since scales::transform_time() is the default transformation for ggplot2::scale_x_datetime().

The underlying problem is that lubridate only provides a method for POSIXct and not for POSIXt. And the order of the classes is therefore important.
https://github.com/tidyverse/lubridate/blob/fea9692686dcb0a968fa068d7f8dfdb41bbea581/R/ops-addition.r#L129

Also, the new order of the classes is the default order of as.POSIXct:

class(as.POSIXct("2024-01-05"))
#> [1] "POSIXct" "POSIXt" 

Fixing this problem in scales seemed the easiest, since the alternative would be adding a lot of new methods to lubridate.

I don't know if there is a reason for the inverse class order. But I hope this is an easy fix.

Greetings
Alex

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

Successfully merging this pull request may close these issues.

1 participant