问题描述
我想从POSIX日期中减去1天,并在DST的同一时间结束。
I want to subtract 1 day from a POSIX date and end up at the same time around DST.
例如,当我添加一天时:
For example, when I add a day:
> as.POSIXct('2009-03-08 23:00:00.000') + 86400
[1] "2009-03-09 23:00:00 EDT"
但是当我过去时,它会抵消:
But when I go past, it offsets:
> as.POSIXct('2009-03-08 23:00:00.000') - 86400
[1] "2009-03-07 22:00:00 EST"
处理DST周围绝对时差的最佳方法是什么?通常,我通过将时间转换为字符串并将其分开处理,从而不应用DST来解决这个问题。
What's the best way to deal with absolute time differences around DST? Usually I deal with this by converting the times into strings and dealing with them separately so that DST isn't applied.
推荐答案
您的代码完全按照您的要求执行操作,因为您没有要求它增加或减少一天,而是要求它添加或减去 24小时。 2009-03-08 23:00:00 EDT
是 2009-03-07之前的24小时(86400秒)美国东部时间22:00:00
。我不熟悉R库,但熟悉它们包装的POSIX函数。如果您使用POSIXct,请将其 day
属性减少1,然后通过重新铸造到 POSIXct
POSIXlt
(以确保例如2月-1日成为1月31日),那么您应该能够可靠地减去一天。
Your code is doing exactly what you asked it to do, because you didn't ask it to add or subtract one day, you asked it to add or subtract 24 hours. 24 hours (86400 seconds) before 2009-03-08 23:00:00 EDT
is 2009-03-07 22:00:00 EST
. I'm not familiar with the R libraries, but I am familiar with the POSIX functions that they wrap. If you take a POSIXct, decrease its day
property by 1, and then "re-cast" it to POSIXct
via POSIXlt
( to ensure that e.g. February -1st becomes January 31st) then you should be able to subtract one day reliably.
这篇关于DST的POSIXct时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!