R use GMT for all dates to avoid daylight savings (#1501)

This commit is contained in:
Ben Letham 2021-03-03 16:53:39 -08:00
parent 47ce88f255
commit aaa89445a1
2 changed files with 11 additions and 12 deletions

View file

@ -243,8 +243,8 @@ validate_column_name <- function(
#' Convert date vector #' Convert date vector
#' #'
#' Convert the date to POSIXct object. Dates without a specified timezone will #' Convert the date to POSIXct object. Timezones are stripped and replaced
#' be given system time zone. Dates with specified timezone will keep it. #' with GMT.
#' #'
#' @param ds Date vector #' @param ds Date vector
#' #'
@ -263,17 +263,15 @@ set_date <- function(ds) {
# Type should be either character, or an object compatible with lubridate # Type should be either character, or an object compatible with lubridate
if (is.character(ds)) { 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") ds <- as.POSIXct(ds, format = "%Y-%m-%d", tz = "GMT")
} else { } else {
ds <- as.POSIXct(ds, format = "%Y-%m-%d %H:%M:%S") ds <- as.POSIXct(ds, format = "%Y-%m-%d %H:%M:%S", tz = "GMT")
} }
} else { } else {
if (is.null(attr(ds, "tzone"))) { # Strip timezone and replace with GMT
# Make sure we don't default to GMT timezone ds <- as.POSIXct(lubridate::force_tz(ds, "GMT"), tz = "GMT")
ds <- lubridate::force_tz(ds)
}
ds <- as.POSIXct(ds)
} }
attr(ds, "tzone") <- "GMT"
return(ds) return(ds)
} }
@ -1678,6 +1676,7 @@ 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[177, ], tolerance = 1e-6) expect_equal(true.values, mat[1, ], tolerance = 1e-6)
}) })
test_that("fourier_series_yearly", { test_that("fourier_series_yearly", {
true.values <- c(0.70112651, -0.71303690, -0.99985814, 0.01684324, 0.72474500, true.values <- c(0.7006152, -0.7135393, -0.9998330, 0.01827656, 0.7262249,
0.68901719) 0.6874572)
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)
}) })