From eeb785c5b28a260c432f8d1176e00e46c9251905 Mon Sep 17 00:00:00 2001 From: Imanuel Costigan Date: Sun, 6 Mar 2016 19:17:37 +1100 Subject: [PATCH] Implement as_date See #355 --- R/coercion.r | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/R/coercion.r b/R/coercion.r index b315751f..623be61a 100644 --- a/R/coercion.r +++ b/R/coercion.r @@ -548,3 +548,28 @@ setMethod("as.character", signature(x = "Duration"), function(x, ...){ setMethod("as.character", signature(x = "Interval"), function(x, ...){ format(x) }) + + + +#' Coerce to a Date +#' +#' @param x a POSIXct, POSIXt date +#' +#' @return a \code{\link{Date}} object that corresponds to the day of the year +#' in the time zone of \code{x}. +#' @export +#' +#' @examples +#' dt1 <- as.POSIXct("2010-08-03 00:59:59.23") +#' as_date(dt1) +#' dt2 <- as.POSIXct("2010-08-03 00:59:59.23", tz="Europe/London") +#' as_date(dt2) +setGeneric(name = "as_date", def = function(x) standardGeneric("as_date")) + +#' @export +setMethod(f = "as_date", signature = "POSIXt", definition = function (x) { + as.Date(x, tz(x)) +}) + + +