2012-01-16 32 views
0

在PHP(因爲5.2.0)我可以寫這樣的代碼:如何在R中查找給定日期和時區的夏令時調整?

//$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?

+0

感謝您的快速回復(他們是一樣的;我給就像我已經在使用POSIXlt一樣,奧斯卡的勾號)。我意識到我也可以一直在PHP這樣做! – 2012-01-17 00:26:09

回答

3

隨着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 
3

可以在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