Fix bug in timezone handling in R (#1501)

This commit is contained in:
Ben Letham 2021-03-03 14:35:36 -08:00
parent fae1ac1f9a
commit 8f1c1d5a27
3 changed files with 20 additions and 12 deletions

View file

@ -24,6 +24,7 @@ Imports:
extraDistr, extraDistr,
ggplot2, ggplot2,
grid, grid,
lubridate,
methods, methods,
RcppParallel (>= 5.0.1), RcppParallel (>= 5.0.1),
rstan (>= 2.18.1), rstan (>= 2.18.1),

View file

@ -240,15 +240,15 @@ validate_column_name <- function(
#' Convert date vector #' Convert date vector
#' #'
#' Convert the date to POSIXct object #' Convert the date to POSIXct object. Dates without a specified timezone will
#' be given system time zone. Dates with specified timezone will keep it.
#' #'
#' @param ds Date vector, can be consisted of characters #' @param ds Date vector
#' @param tz string time zone
#' #'
#' @return vector of POSIXct object converted from date #' @return vector of POSIXct object converted from date
#' #'
#' @keywords internal #' @keywords internal
set_date <- function(ds = NULL, tz = "GMT") { set_date <- function(ds) {
if (length(ds) == 0) { if (length(ds) == 0) {
return(NULL) return(NULL)
} }
@ -257,12 +257,20 @@ set_date <- function(ds = NULL, tz = "GMT") {
ds <- as.character(ds) ds <- as.character(ds)
} }
# Type should be either character, or an object compatible with lubridate
if (is.character(ds)) {
if (min(nchar(ds), na.rm=TRUE) < 12) { if (min(nchar(ds), na.rm=TRUE) < 12) {
ds <- as.POSIXct(ds, format = "%Y-%m-%d", tz = tz) ds <- as.POSIXct(ds, format = "%Y-%m-%d")
} else { } else {
ds <- as.POSIXct(ds, format = "%Y-%m-%d %H:%M:%S", tz = tz) ds <- as.POSIXct(ds, format = "%Y-%m-%d %H:%M:%S")
}
} else {
if (is.null(attr(ds, "tzone"))) {
# Make sure we don't default to GMT timezone
ds <- lubridate::force_tz(ds)
}
ds <- as.POSIXct(ds)
} }
attr(ds, "tzone") <- tz
return(ds) return(ds)
} }
@ -1667,7 +1675,6 @@ make_future_dataframe <- function(m, periods, freq = 'day',
dates <- dates[2:(periods + 1)] # Drop the first, which is max(history$ds) dates <- dates[2:(periods + 1)] # Drop the first, which is max(history$ds)
if (include_history) { if (include_history) {
dates <- c(m$history.dates, dates) dates <- c(m$history.dates, dates)
attr(dates, "tzone") <- "GMT"
} }
return(data.frame(ds = dates)) return(data.frame(ds = dates))
} }

View file

@ -213,12 +213,12 @@ test_that("fourier_series_weekly", {
true.values <- c(0.7818315, 0.6234898, 0.9749279, -0.2225209, 0.4338837, true.values <- c(0.7818315, 0.6234898, 0.9749279, -0.2225209, 0.4338837,
-0.9009689) -0.9009689)
mat <- prophet:::fourier_series(DATA$ds, 7, 3) mat <- prophet:::fourier_series(DATA$ds, 7, 3)
expect_equal(true.values, mat[1, ], tolerance = 1e-6) expect_equal(true.values, mat[177, ], tolerance = 1e-6)
}) })
test_that("fourier_series_yearly", { test_that("fourier_series_yearly", {
true.values <- c(0.7006152, -0.7135393, -0.9998330, 0.01827656, 0.7262249, true.values <- c(0.70112651, -0.71303690, -0.99985814, 0.01684324, 0.72474500,
0.6874572) 0.68901719)
mat <- prophet:::fourier_series(DATA$ds, 365.25, 3) mat <- prophet:::fourier_series(DATA$ds, 365.25, 3)
expect_equal(true.values, mat[1, ], tolerance = 1e-6) expect_equal(true.values, mat[1, ], tolerance = 1e-6)
}) })