-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As of R-3.5.1, converting from POSIXct to Date creates an intermediate POSIXlt vector. This uses a non-trivial amount of memory. Directly converting from POSIXct to Date uses less memory and is roughly twice as fast. This solution is a combination of asPOSIXlt and POSIXlt2D from RCore. The functions are essentially concatenated, with the extraneous parts removed. The function name 'asDatePOSIXct' differs from the other rapi* names because it's not a RCore function. The name isn't as.Date.POSIXct() to avoid clashes with the current (or future) S3 method.
- Loading branch information
1 parent
84aa1b0
commit 6bc9320
Showing
6 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
##' Conversion from POSIXct to Date | ||
##' | ||
##' This function provides a direct conversion from POSIXct to Date. As of | ||
##' R-3.5.1, conversion from POSIXct to Date creates an intermediate POSIXlt | ||
##' object. This intermediate POSIXlt object uses a non-trivial amount of | ||
##' memory. The direct conversion is more memory efficient, and therefore | ||
##' approximately twice as fast as the current solution in base R. | ||
##' | ||
##' @title Conversion from POSIXct to Date | ||
##' @param x A POSIXct vector | ||
##' @param tz An optional timezone string | ||
##' @return A vector of \code{Date} objects | ||
##' @author Joshua Ulrich | ||
##' @examples | ||
##' p <- .POSIXct(1540181413, "America/Chicago") | ||
##' as.Date(p) # Using UTC timezone | ||
##' as.Date(p, "America/Chicago") # Using local timezone | ||
##' asDatePOSIXct(p) # Direct, using local timezone | ||
asDatePOSIXct <- function(x, tz="") { | ||
stopifnot(inherits(x, "POSIXct")) | ||
tzone <- attr(x, "tzone") | ||
if (missing(tz) && !is.null(tzone)) { | ||
tz <- tzone | ||
} | ||
res <- .Call("POSIXct2D", x, tz, PACKAGE="RApiDatetime") | ||
res | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters