我想知道,为什么R没有得到正确的时区。我错过了什么吗?
my_str <- c("2016-01-01 00:04:52 CET", "2016-01-01 00:09:52 CET",
"2016-01-01 00:18:04 CET", "2016-01-01 00:18:49 CET")
my_t <- as.POSIXct(my_str)
attributes(my_t)
# $class
# [1] "POSIXct" "POSIXt"
#
# $tzone
# [1] ""
my_t <- lubridate::ymd_hms(my_str)
attributes(my_t)
# $tzone
# [1] "UTC"
#
# $class
# [1] "POSIXct" "POSIXt"
这"解决"了问题,但我想知道为什么我必须注意显而易见的问题:
my_t <- as.POSIXct(my_str, tz="CET")
attributes(my_t)
# $class
# [1] "POSIXct" "POSIXt"
#
# $tzone
# [1] "CET"
很棒的anytime
包解决了这个问题:
my_str <- c("2016-01-01 00:04:52 CET", "2016-01-01 00:09:52 CET",
"2016-01-01 00:18:04 CET", "2016-01-01 00:18:49 CET")
library(anytime)
my_t <- anytime(my_str)
attributes(my_t)
#> $class
#> [1] "POSIXct" "POSIXt"
#>
#> $tzone
#> [1] "Europe/Berlin"