【问题标题】:How to find summertime adjustment, for a given date and timezone, in R?如何在 R 中找到给定日期和时区的夏令时调整?
【发布时间】:2012-02-11 09:15:14
【问题描述】:

在 PHP(自 5.2.0 起)中,我可以编写以下代码:

//$t is timestamp of day I want to look at
$tz=new DateTimeZone('America/New_York');
$transition = $tz->getTransitions($t,$t);
if(!$transition || count($transition)==0)throw new Exception("Bad timezone")
$offset=$transition[0]['offset'];   //This is seconds ahead of GMT.
      //I.e. -14400 in summer, -18000 in winter.

当我发现这个类/成语时,我几乎高兴得哭了;但是有没有办法在 R 中做同样的事情?还是我必须求助于我以前在 PHP 中所做的事情,即为我需要考虑的每个时区硬编码一组夏季开始/结束日期?

(顺便说一句,有关 PHP 代码的更多信息,请参阅:How to tell if a timezone observes daylight saving at any time of the year?

【问题讨论】:

  • 感谢您的快速回复(他们是一样的;我只是因为我已经在使用 POSIXlt 就给 Oscar 打勾了)。我意识到我也可以一直在 PHP 中这样做!

标签: php r timezone dst


【解决方案1】:

使用as.POSIXctas.POSIXlt,您可以获得此信息。例如:

tz = 'Europe/Madrid'
d1 <- as.POSIXct('2010-01-01 12:00:00',  tz = tz)
as.POSIXlt(d1)$isdst ## 0 since it's not DST

如果您需要与 UTC 的差异:

d0 <- as.POSIXct(format(d1,  tz = 'UTC'),  tz = tz)
as.numeric(d1 - d0) ## 1 hour

另一天也一样:

d2 <- as.POSIXct('2010-07-01 12:00:00',  tz = tz)
as.POSIXlt(d2)$isdst ## 1 since it is DST
d0 <- as.POSIXct(format1(d2,  tz = 'UTC'),  tz = tz)
as.numeric(d2 - d0) ## 2 hour

【讨论】:

    【解决方案2】:

    您可以为相同的日期和时间构建两个时间对象,一个在所需时区,另一个在 GMT,然后查看它们的差异。

    # Winter: London uses GMT
    > ISOdatetime(2012, 01, 01, 00, 00, 00, "Europe/London") - 
      ISOdatetime(2012, 01, 01, 00, 00, 00, "GMT")
    Time difference of 0 secs
    
    # Summer: London uses GMT+1
    > ISOdatetime(2012, 08, 01, 00, 00, 00, "Europe/London") - 
      ISOdatetime(2012, 08, 01, 00, 00, 00, "GMT")
    Time difference of -1 hours
    

    【讨论】:

      猜你喜欢
      • 2014-07-03
      • 1970-01-01
      • 2010-09-07
      • 2012-04-07
      • 2017-03-02
      • 2013-11-16
      • 1970-01-01
      • 2012-09-06
      • 2015-05-03
      相关资源
      最近更新 更多