我正在尝试做一些动态的日期/时间计算。我注意到我的约会/时间计算被推迟了12个小时。Sys.Date()
似乎忽略了系统时区,并且默认为UTC,尽管系统时区的设置是准确的。
Sys.Date()
的帮助声明如下。
详细信息 Sys.time返回一个绝对日期-时间值,该值可以转换为不同的时区,并可能返回不同的日期。 Sys.Date返回当前时区中的当前日期。
当我检查系统时区时,我可以看到它是正确设置的。
> Sys.timezone()
[1] "Pacific/Auckland"
此外,Sys.getlocale()
返回以下内容。这里似乎一切都很好。
> Sys.getlocale()
[1] "LC_COLLATE=English_New Zealand.1252;LC_CTYPE=English_New Zealand.1252;LC_MONETARY=English_New Zealand.1252;LC_NUMERIC=C;LC_TIME=English_New Zealand.1252"
使用Sys.Date()
获取lubridate::tz()
时区将返回以下内容。
> lubridate::tz(Sys.Date())
[1] "UTC"
并且,将Sys.Date()
转换为POSIXct
确认,尽管精确设置了系统时区,Sys.Date()
的原始时区仍然是UTC。
> as.POSIXct(Sys.Date())
[1] "2019-07-15 12:00:00 NZST"
最初,我认为Sys.Date()可能是被其他包覆盖了。重新启动R会话不会改变任何事情。这是我的sessionInfo()
。
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
Matrix products: default
locale:
[1] LC_COLLATE=English_New Zealand.1252 LC_CTYPE=English_New Zealand.1252
[3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C
[5] LC_TIME=English_New Zealand.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] elasticsearchr_0.3.0 viridis_0.5.1 viridisLite_0.3.0 forecast_8.7
[5] extrafont_0.17 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3
[9] purrr_0.3.2 readr_1.3.1 tidyr_0.8.3 tibble_2.1.3
[13] ggplot2_3.2.0 tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] zoo_1.8-6 tidyselect_0.2.5 urca_1.3-0 haven_2.1.1 lattice_0.20-38
[6] colorspace_1.4-1 generics_0.0.2 rlang_0.4.0 pillar_1.4.2 glue_1.3.1
[11] withr_2.1.2 modelr_0.1.4 TTR_0.23-4 readxl_1.3.1 quantmod_0.4-15
[16] timeDate_3043.102 munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0 rvest_0.3.4
[21] tseries_0.10-47 lmtest_0.9-37 curl_3.3 parallel_3.6.1 Rttf2pt1_1.3.7
[26] broom_0.5.2 xts_0.11-2 Rcpp_1.0.1 scales_1.0.0 backports_1.1.4
[31] jsonlite_1.6 gridExtra_2.3 fracdiff_1.4-2 hms_0.4.2 stringi_1.4.3
[36] grid_3.6.1 quadprog_1.5-7 cli_1.1.0 tools_3.6.1 magrittr_1.5
[41] lazyeval_0.2.2 crayon_1.3.4 extrafontdb_1.0 pkgconfig_2.0.2 xml2_1.2.0
[46] lubridate_1.7.4 assertthat_0.2.1 httr_1.4.0 rstudioapi_0.10 R6_2.4.0
[51] nnet_7.3-12 nlme_3.1-140 compiler_3.6.1
有人也经历过类似的事情吗?我觉得我好像错过了什么。
发布于 2019-07-14 23:55:03
Sys.Date
返回一个日期对象,该对象不包括时区属性:
> str(Sys.Date())
Date[1:1], format: "2019-07-14"
如果不存在时区属性,lubridate::tz
返回"UTC“:
function (x)
{
tzone <- attr(x, "tzone")[[1]]
if (is.null(tzone) && !is.POSIXt(x))
return("UTC")
if (is.character(tzone) && nzchar(tzone))
return(tzone)
tzone <- attr(as.POSIXlt(x[1]), "tzone")[[1]]
if (is.null(tzone))
return("UTC")
tzone
}
因此,Sys.Date
并不忽略系统时区;而是Date对象不包含任何时区信息,因此无法从Date对象恢复时区。
https://stackoverflow.com/questions/57031784
复制相似问题