gpt4 book ai didi

r - 是否有一种可靠的方法来检测表示由于 DST 不存在的时间的 POSIXlt 对象?

转载 作者:行者123 更新时间:2023-12-04 12:01:21 24 4
gpt4 key购买 nike

我有以下问题:我得到的数据中的日期列包含由于夏令时不存在的日期。 (例如2015-03-29 02:00在中欧时间不存在,因为DST在这一天生效,时钟直接设置为01:59到03:00)

是否有一种简单可靠的方法来确定日期是否相对于夏令时有效?

由于 datetime 类的属性,这并非微不足道。

# generating the invalid time as POSIXlt object
test <- strptime("2015-03-29 02:00", format="%Y-%m-%d %H:%M", tz="CET")

# the object seems to represent something at least partially reasonable, notice the missing timezone specification though
test
# [1] "2015-03-29 02:00:00"

# strangely enough this object is regarded as NA by is.na
is.na(test)
# [1] TRUE

# which is no surprise if you consider:
is.na.POSIXlt
# function (x)
# is.na(as.POSIXct(x))

as.POSIXct(test)
# [1] NA

# inspecting the interior of my POSIXlt object:
unlist(test)
# sec min hour mday mon year wday yday isdst zone gmtoff
# "0" "0" "2" "29" "2" "115" "0" "87" "-1" "" NA

所以我想到的最简单的方法就是查看 isdst领域 POSIXlt对象, POSIXt 的帮助描述该文件如下:

isdst
Daylight Saving Time flag. Positive if in force, zero if not, negative if unknown.



正在检查 isdst字段保存,因为该字段仅为 -1如果日期由于 dst 更改而无效,或者可以是 -1出于其他一些原因?

有关版本、平台和区域设置的信息
R.version
# _
# platform x86_64-w64-mingw32
# arch x86_64
# os mingw32
# system x86_64, mingw32
# status
# major 3
# minor 3.1
# year 2016
# month 06
# day 21
# svn rev 70800
# language R
# version.string R version 3.3.1 (2016-06-21)
# nickname Bug in Your Hair
Sys.getlocale()
# [1] "LC_COLLATE=German_Austria.1252;LC_CTYPE=German_Austria.1252;LC_MONETARY=German_Austria.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"

最佳答案

as.POSIXct(test)的值似乎依赖于平台,为获得可靠的方法增加了一层复杂性。在我的 Windows 机器上,(R 3.3.1),as.POSIXct(test)生产 NA ,正如 OP 所报道的那样。但是,在我的 Linux 平台(相同的 R 版本)上,我得到以下信息:

times = c ("2015-03-29 01:00",
"2015-03-29 02:00",
"2015-03-29 03:00")

test <- strptime(times, format="%Y-%m-%d %H:%M", tz="CET")

test
#[1] "2015-03-29 01:00:00 CET" "2015-03-29 02:00:00 CEST" "2015-03-29 03:00:00 CEST"
as.POSIXct(test)
#[1] "2015-03-29 01:00:00 CET" "2015-03-29 01:00:00 CET" "2015-03-29 03:00:00 CEST"
as.character(test)
#[1] "2015-03-29 01:00:00" "2015-03-29 02:00:00" "2015-03-29 03:00:00"
as.character(as.POSIXct(test))
#[1] "2015-03-29 01:00:00" "2015-03-29 01:00:00" "2015-03-29 03:00:00"

我们可以依赖的一件事不是 as.POSIXct(test)的实际值。 ,但它会与 test 不同当 test是无效的日期/时间:
(as.character(test) == as.character(as.POSIXct(test))) %in% TRUE
# TRUE FALSE TRUE

我不确定 as.character在这里是绝对必要的,但我包含它只是为了确保我们不会与 POSIX 对象的任何其他奇怪行为发生冲突。

关于r - 是否有一种可靠的方法来检测表示由于 DST 不存在的时间的 POSIXlt 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39526527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com