dst()
調用的代碼基本上是:
c(NA, FALSE, TRUE)[as.POSIXlt(x)$isdst + 2]
as.POSIXlt
默認爲:
as.POSIXlt(x=, tz="")
...它會默認把你的系統時區。因此,鑑於你在湖人的位置,讓我們來看看:
as.POSIXlt(x, tz="America/Los_Angeles")
#[1] "2016-01-01 PST" "2016-06-01 PDT"
c(NA, FALSE, TRUE)[as.POSIXlt(x, tz="America/Los_Angeles")$isdst + 2]
#[1] FALSE TRUE
一切都很好。萬歲。現在,讓我們嘗試使用as.Date(x)
as.POSIXlt(as.Date(x))
#[1] "2016-01-01 UTC" "2016-06-01 UTC"
as.POSIXlt(as.Date(x), tz="America/Los_Angeles")
#[1] "2016-01-01 UTC" "2016-06-01 UTC"
哦。因此,as.POSIXlt
與Date
對象不搭配很好,並且始終返回UTC
而不是本地時區,並且看起來會忽略任何tz=
參數。由於UTC
不遵守任何夏令時,您總是會返回FALSE
。
看看R的源代碼,這似乎是這種情況。在https://svn.r-project.org/R/trunk/src/main/datetime.c中您可以找到:
# R call:
#> as.POSIXlt.Date
#function (x, ...)
#.Internal(Date2POSIXlt(x))
# source code:
#SEXP attribute_hidden do_D2POSIXlt(SEXP call, SEXP op, SEXP args, SEXP env)
#{
#...
setAttrib(ans, s_tzone, mkString("UTC"));
...作爲硬編碼字符串。
嗨pbaylis,你能告訴我們你期望的結果嗎? –
'dst(x)'爲我返回'c(FALSE,FALSE)'。你在什麼時區? 'dst(x)'調用as.POSIXlt(x)$ isdst',它使用系統時區。 – thelatemail
我期望'c(FALSE,TRUE)'和我在PST(相應編輯的問題)。 – pbaylis