2

嗨,我有一个rr长度为几百万的字符向量 ( ),它%Y-%m-%d %H:%M:%S以澳大利亚/悉尼记录的格式表示时间和日期戳。

如何(快速)获得一个代表这个的 POSIXct 对象。

fastPOSIXctfasttime包中找到了,但为了准确起见,它要求原始字符串为 GMT/UTC,(我的不是),然后使用tz争论转换回正确的时区......

> head(rr)
[1] "2009-05-01 10:01:00" "2009-05-01 10:02:00" "2009-05-01 10:03:00" "2009-05-01 10:04:00"
[5] "2009-05-01 10:05:00" "2009-05-01 10:06:00"

> as.POSIXct(head(rr),tz="Australia/Sydney")
[1] "2009-05-01 10:01:00 EST" "2009-05-01 10:02:00 EST" "2009-05-01 10:03:00 EST"
[4] "2009-05-01 10:04:00 EST" "2009-05-01 10:05:00 EST" "2009-05-01 10:06:00 EST"

如果对全套数据进行上述操作,则需要很长时间……因此,我们将不胜感激任何速度改进。谢谢。

4

2 回答 2

2

受到 Dirk 对此 qn 的回答的启发,我制作了这个包装器来处理一年中的一大堆日期:

    fastPOSIXct_generic <- function(x, mytz = "America/New_York")
{
    # Caution, read: ?DateTimeClasses
    stopifnot(is.character(x))
    times_UTC <- fastPOSIXct(x, tz='UTC')
    num_times <- as.numeric(times_UTC)
    t1 <- as.POSIXct(x[1], tz = mytz)
    t2 <- as.POSIXct(x[1], tz = "UTC")
    offset <- as.numeric(difftime(t1, t2, units = "secs"))
    daylightoffset <- as.POSIXlt(t1)$isdst
    # For this first 'time' in t1 and t2, remove possible impact of losing one hour by setting clocks one hour forward during summer months:
    offset <- offset + daylightoffset * 3600
    num_times <- num_times + offset
    new_num_times <- as.POSIXct(num_times, tz = mytz, origin = '1970-01-01')
    new_num_times2 <- new_num_times - as.POSIXlt(new_num_times)$isdst * 3600
    return(new_num_times2)
}

# Test Sydney time

mm <- as.POSIXct(c("2015-03-15 15:00:00", "2015-4-10 15:00:00", "2014-10-01 15:00:00", "2015-10-15 15:00:00"), tz = "Australia/Sydney")
# "2015-03-15 15:00:00 AEDT" "2015-04-10 15:00:00 AEST" "2014-10-01 15:00:00 AEST" "2015-10-15 15:00:00 AEDT"
aus_stamps <- as.character(mm)
aus_back <- fastPOSIXct_generic(x = aus_stamps, mytz = "Australia/Sydney")
#"2015-03-15 15:00:00 AEDT" "2015-04-10 15:00:00 AEST" "2014-10-01 15:00:00 AEST" "2015-10-15 15:00:00 AEDT"
identical(mm, aus_back)
# TRUE

我的用例几乎总是 UTC 到 America/New_York,到目前为止它似乎运行良好。我不知道它是否适用于其他时区;只是 dst 有时间的情况会提前一个小时。

于 2015-10-15T22:49:35.890 回答
1

这是一种方法:

i) 撒谎fasttime()并假装数据是 UTC,用于将数据解析为向量x

ii) 使用您的第一个数据点计算与 UTC 的偏移量:

R> d1 <- "2009-05-01 10:01:01"   ## or use `head(rr,1)`
R> t1 <- as.POSIXct(d1,tz="Australia/Sydney")
R> t2 <- as.POSIXct(d1,tz="UTC")
R> offset <- as.numeric(difftime(t2, t1, units="secs"))
R> offset
[1] 36000

iii) 将该offset值应用于您的数据——这是一种快速添加,因为它POSIXct实际上是以(小数)秒(自纪元以来)为单位的数字类型。

于 2013-08-30T17:04:29.893 回答