rlubridatechron

Warning message when extract months from chron object


I'm maintaining some code that uses both the lubridate and chron packages and a new warning has cropped up.

Warning message: tz(): Don't know how to compute timezone for object of class dates/times; returning "UTC". This warning will become an error in the next major version of lubridate.

This arises when extracting months from an object made by chron.

library(lubridate)
library(chron)
xdate <- structure(c(23831, 23832, 23833, 23834, 23835, 23836), 
         format = "d/m/y", origin = c(month = 1, day = 1, year = 1900), class = c("dates", "times"))


lubridate::month(xdate)

I can get around this by wrapping xdate in as.Date lubridate::month(as.Date(xdate)). The dates produced are correct. Is this approach ok?

Edit: I see another way of getting around this is chron::month.day.year(xdate)$month


Solution

  • I would think that would be ok but to avoid problems it might be better not to use lubridate for this. chron has months and monday.day.year and we provide some other conversions as well:

    as.integer(months(xdate))  # uses chron:::months.default
    ## [1] 4 4 4 4 4 4
    
    c(months(xdate))
    ## [1] 4 4 4 4 4 4
    
    month.day.year(xdate)$month # uses chron::month.day.year
    ## [1] 4 4 4 4 4 4
    
    as.integer(format(as.Date(xdate), "%m"))
    ## [1] 4 4 4 4 4 4
    
    library(zoo)
    cycle(as.yearmon(xdate))
    ## [1] 4 4 4 4 4 4