Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up[Request] IDateTime should use the time zone of a POSIXct argument consistently on the date and time #977
Comments
|
For my project, I created a specialization of I like this approach because the user will have a what you see (in In particular, the example from as.IDate.POSIXct <- function(x,...) {
if( hasArg("tz") ) {
as.IDate( as.Date(x, ...) )
}
else if( "tzone" %in% names(attributes(x)) ) {
as.IDate( as.Date(x, tz = attr(x,"tzone"), ...) )
}
else {
attr(x,"tzone") <- Sys.timezone() # make system time zone explicit,
as.IDate( x, ...) # then call this again
}
}
# example from help(IDateTime) with explicit time zone
tz <- "US/Central"
datetime <- seq(as.POSIXct("2001-01-01",tz=tz), as.POSIXct("2001-01-03",tz=tz), by = "5 hour")
(af <- data.table(IDateTime(datetime), a = rep(1:2, 5), key = "a,idate,itime"))
af[, mean(a), by = list(wday = wday(idate))]
# Without redefining for POSIXct, this produced weekdays in UTC
# wday V1
#1: 2 1.5
#2: 3 1.4
#3: 4 2.0
# With specialization of IDate for POSIXct, the local weekdays are used
# wday V1
#1: 2 1.4
#2: 3 1.6 |
Edit: I changed the issue title based on my current understanding. Previously it read: Pass along
..inIDateTimeor remove from documentation. Here's the quick version. My two posts propose a solution, but it may violate some goals of the package.The
...inIDateTimeare not passed along toas.IDateandas.ITime. This would make it easier to handle time zones. If there are reasons not to do that, maybe it should be removed from the function and the documentation (I don't know enough about R to say one way or the other; it seems conventional to have...everywhere).I realize that
ITimedoes not really support time zones, and I'm not trying to suggest it should. I would just like a way to manage data when I do not have the luxury of forcing UTC (data must be delivered downstream in local time zones for Shiny app). I take my UTC data, convert it to the local time zone, then try to convert toIDateTimefor use in a data.table.As I hope my example shows, the issue can be worked around with
as.IDateandas.ITimeindividually. It took me a long time to realize that when I usedIDateTimewith atzargument, it wasn't getting used. Maybe this could save someone else from getting confused on this point.As a side note, the
help(IDateTime)includes the following example.If the user has not set her time zone to UTC, this will produce unexpected results (at least they are unexpected if you don't understand the details of how this works). For example, US time zones will not see '2001-01-01 20:00:00' because it mixes the UTC converted date '2001-01-02' with the local time '20:00:00'.