每三个小时的时间戳序列

时间:2016-07-24 17:59:17

标签: r time timezone sequence

我希望创建一个时间戳序列,每三个小时开始和结束一个特定日期。我尝试过的方式运作良好,但都有同样的问题。在序列中间的某个地方,PST与PDT之间存在时间转换,我不知道为什么。以下是我尝试过的代码,类似于此网站上提出的问题(Question1Question2):

 TIME1 <- as.POSIXct( "2000-01-01 02:00:00", tz = "America/Los_Angeles")
 TIME2 <- as.POSIXct( "2016-02-29 23:00:00" , tz = "America/Los_Angeles")
 temp <- seq(from = TIME1, to = TIME2, by = "3 hours")

当我查看temp的输出时,除了查看下面的指定间隔外,它看起来很不错

 temp[720:745] 

[1] "2000-03-30 23:00:00 PST" "2000-03-31 02:00:00 PST" "2000-03-31 05:00:00 PST" "2000-03-31 08:00:00 PST" "2000-03-31 11:00:00 PST" "2000-03-31 14:00:00 PST"
[7] "2000-03-31 17:00:00 PST" "2000-03-31 20:00:00 PST" "2000-03-31 23:00:00 PST" "2000-04-01 02:00:00 PST" "2000-04-01 05:00:00 PST" "2000-04-01 08:00:00 PST"
[13] "2000-04-01 11:00:00 PST" "2000-04-01 14:00:00 PST" "2000-04-01 17:00:00 PST" "2000-04-01 20:00:00 PST" "2000-04-01 23:00:00 PST" "2000-04-02 03:00:00 PDT"
[19] "2000-04-02 06:00:00 PDT" "2000-04-02 09:00:00 PDT" "2000-04-02 12:00:00 PDT" "2000-04-02 15:00:00 PDT" "2000-04-02 18:00:00 PDT" "2000-04-02 21:00:00 PDT"
[25] "2000-04-03 00:00:00 PDT" "2000-04-03 03:00:00 PDT"

在输出的[1]行注意,它从“2000-03-30 23:00:00 PST”到“2000-03-31 02:00:00 PST”,这是3小时,但是在[13]行,它从“2000-04-01 23:00:00 PST”变为“2000-04-02 03:00:00 PDT”。注意那里的时区变化。这在整个序列中都会发生。任何想法如何避免这样的结果?我相信它会自动为我转换夏令时,这是我不想要的。如果我能把它保持为LST(当地标准时间),那就太棒了。

更新:我最近找到this suggestion,我们只是转换为时区“UTC”以消除夏令时。它工作,只是将所有时间戳标记为UTC。但是,很高兴看到是否还有办法将其保持为LST。

2 个答案:

答案 0 :(得分:4)

如果您想要太平洋标准时间(PST)作为时区并且不想切换到太平洋夏令时(PDT),为什么不指定? "America/Los Angeles"表示按照定义在特定时间在洛杉矶生效的时区(包括DST切换)......

TIME1 <- as.POSIXct( "2000-01-01 02:00:00", tz = "PST")
TIME2 <- as.POSIXct( "2016-02-29 23:00:00" , tz = "PST")
temp <- seq(from = TIME1, to = TIME2, by = "3 hours") 
temp[720:745]
 [1] "2000-03-30 23:00:00 PST" "2000-03-31 02:00:00 PST"
 [3] "2000-03-31 05:00:00 PST" "2000-03-31 08:00:00 PST"
 [5] "2000-03-31 11:00:00 PST" "2000-03-31 14:00:00 PST"
 [7] "2000-03-31 17:00:00 PST" "2000-03-31 20:00:00 PST"
 [9] "2000-03-31 23:00:00 PST" "2000-04-01 02:00:00 PST"
[11] "2000-04-01 05:00:00 PST" "2000-04-01 08:00:00 PST"
[13] "2000-04-01 11:00:00 PST" "2000-04-01 14:00:00 PST"
[15] "2000-04-01 17:00:00 PST" "2000-04-01 20:00:00 PST"
[17] "2000-04-01 23:00:00 PST" "2000-04-02 02:00:00 PST"
[19] "2000-04-02 05:00:00 PST" "2000-04-02 08:00:00 PST"
[21] "2000-04-02 11:00:00 PST" "2000-04-02 14:00:00 PST"
[23] "2000-04-02 17:00:00 PST" "2000-04-02 20:00:00 PST"
[25] "2000-04-02 23:00:00 PST" "2000-04-03 02:00:00 PST"

建议使用UTC的referenced question注释三个字母的时区代码可能不明确(例如澳大利亚和北美都有东部标准时间,尽管this reference表明澳大利亚时区代码是编码为AEST)。你应该小心谨慎地检查你的结果,因为这样的时区编码很有可能是特定于操作系统的......

答案 1 :(得分:1)

  

在输出的[1]行注意,它来自&#34; 2000-03-30 23:00:00 PST&#34;到&#34; 2000-03-31 02:00:00太平洋标准时间&#34;这是3个小时,但在线[13]它来自&#34; 2000-04-01 23:00:00 PST&#34;到&#34; 2000-04-02 03:00:00 PDT&#34;。

请注意&#34; 2000-04-01 23:00:00 PST&#34;到&#34; 2000-04-02 03:00:00 PDT&#34;是 3个小时。

如果您希望时区不会观察夏令时,但与GMT的偏差与当前当地时间相同,那么您可以尝试时区名称中的建议 ?timezone的部分:

  

大多数平台都支持“GMT + n”和“GMT-n”形式的时区,它假定与UTC的固定偏移量(因此没有DST)。

R> TIME1 <- as.POSIXct( "2000-01-01 02:00:00", tz = "GMT-8")
R> TIME2 <- as.POSIXct( "2016-02-29 23:00:00", tz = "GMT-8")
R> temp <- seq(from = TIME1, to = TIME2, by = "3 hours")
R> temp[720:745] 
 [1] "2000-03-30 23:00:00 GMT" "2000-03-31 02:00:00 GMT"
 [3] "2000-03-31 05:00:00 GMT" "2000-03-31 08:00:00 GMT"
 [5] "2000-03-31 11:00:00 GMT" "2000-03-31 14:00:00 GMT"
 [7] "2000-03-31 17:00:00 GMT" "2000-03-31 20:00:00 GMT"
 [9] "2000-03-31 23:00:00 GMT" "2000-04-01 02:00:00 GMT"
[11] "2000-04-01 05:00:00 GMT" "2000-04-01 08:00:00 GMT"
[13] "2000-04-01 11:00:00 GMT" "2000-04-01 14:00:00 GMT"
[15] "2000-04-01 17:00:00 GMT" "2000-04-01 20:00:00 GMT"
[17] "2000-04-01 23:00:00 GMT" "2000-04-02 02:00:00 GMT"
[19] "2000-04-02 05:00:00 GMT" "2000-04-02 08:00:00 GMT"
[21] "2000-04-02 11:00:00 GMT" "2000-04-02 14:00:00 GMT"
[23] "2000-04-02 17:00:00 GMT" "2000-04-02 20:00:00 GMT"
[25] "2000-04-02 23:00:00 GMT" "2000-04-03 02:00:00 GMT"
R> attr(temp, "tzone")
[1] "GMT-8"