Search code examples
rdatetimelubridate

How to change the year when using POSIX class with Lubridate functions?


I am trying to reassign the year but get an error for an invalid timezone for certain timezones when using POSIX classes.

When I run this code

x <- as.POSIXct("2000-01-01", tz = "America/Costa_Rica")
year(x)
year(x) <-  1972

I get this error

Error in (function (dt, year, month, yday, mday, wday, hour, minute, second,  : 
  CCTZ: Invalid timezone of the input vector: "America/Costa_Rica"

But when I run it with EST or UTC it works

x <- as.POSIXct("2000-01-01", tz = "EST")
year(x)
year(x) <-  1972
x

I get the output

"1972-01-01 EST"

But the only two timezones I could get to work are EST and UTC. I randomly tried several other timezones from OlsonNames() and they all gave me CCTZ: Invalid timezone error. Some others that I tried were Indian/Mayotte, GMT, Etc/GMT-8.

I have made sure my Lubridate is updated and even downloaded the dev version just to try but it gave me the same errors.

Specifics: MacOS 10.15.7; R version 4.0.3; the only library I have loaded is Lubridate


Solution

  • I found a thread on GitHub that answers this https://github.com/tidyverse/lubridate/issues/928

    It appears to be an issue with R 4.0.3 and macOS Catalina. I was able to solve my problem by using R 4.0.2 instead.