as_datetime <- function(x) {
UseMethod("as_datetime")
}
as_datetime.default <- function(x) {
stop("Unknown date type. Use Date or POSIXct only.")
}
as_datetime.datetime.date <- function(x) {
x
}
as_datetime.Date <- function(x) {
number_date <- as.numeric(x) * 86400 * 1e9
pandas$Timestamp(number_date, tz = "UTC")
}
as_datetime.POSIXct <- function(x) {
tz <- as.POSIXlt(x)$zone
number_date <- as.numeric(x) * 1e9
tryCatch({
pandas$Timestamp(number_date, tz = tz)
},
error = function(e) {
msg <- paste0("R time zone, ", tz, ", missing or does not exist in pytz. Defaulting to UTC.")
warning(msg, call. = FALSE)
pandas$Timestamp(number_date, tz = "UTC")
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.