2013-03-05 71 views
6

此示例以基準日期爲基準,並增加了7½小時,1天7½小時,2天7½小時等等。爲什麼在8:30加入6天7½小時到午夜?

use Date::Manip; 
use DateTime; 
use DateTime::Format::DateManip; 

Date::Manip::Date_Init("TZ=America/New_York", "Language=English"); 

my $otime = DateTime->new(
    year  => 2013, 
    month  => 3, 
    day  => 4, 
    hour  => 0, 
    minute => 0, 
    second => 0, 
    time_zone => 'America/New_York', 
); 

my $t1 = UnixDate($otime, "%i:%M %p on %A, %B %e, %Y "); 
print "original $t1\n"; 

for (my $i = 0; $i <= 20; $i++) {    
    my $dtw = $otime->clone(); 
    $dtw->add(
     minutes => (15) * 30, 
     days => ($i), 
    ); 
    $t1 = UnixDate($dtw, "%i:%M %p on %A, %B %e, %Y "); 
    print "$i days $t1\n"; 
} 

當添加6天7½小時時,結果包含額外的小時。

original 12:00 AM on Monday, March 04, 2013 
0 days 07:30 AM on Monday, March 04, 2013 
1 days 07:30 AM on Tuesday, March 05, 2013 
2 days 07:30 AM on Wednesday, March 06, 2013 
3 days 07:30 AM on Thursday, March 07, 2013 
4 days 07:30 AM on Friday, March 08, 2013 
5 days 07:30 AM on Saturday, March 09, 2013 
6 days 08:30 AM on Sunday, March 10, 2013 # why 8:30 and not 7:30? 
7 days 07:30 AM on Monday, March 11, 2013 
8 days 07:30 AM on Tuesday, March 12, 2013 
9 days 07:30 AM on Wednesday, March 13, 2013 
10 days 07:30 AM on Thursday, March 14, 2013 
11 days 07:30 AM on Friday, March 15, 2013 
12 days 07:30 AM on Saturday, March 16, 2013 
13 days 07:30 AM on Sunday, March 17, 2013 
14 days 07:30 AM on Monday, March 18, 2013 
15 days 07:30 AM on Tuesday, March 19, 2013 
16 days 07:30 AM on Wednesday, March 20, 2013 
17 days 07:30 AM on Thursday, March 21, 2013 
18 days 07:30 AM on Friday, March 22, 2013 
19 days 07:30 AM on Saturday, March 23, 2013 
20 days 07:30 AM on Sunday, March 24, 2013
+5

夏令時? – TLP 2013-03-05 08:11:09

+0

如果您想忽略DST更改,請將時區切換爲「浮動」。 – ikegami 2017-08-08 17:01:38

回答

19

因爲夏令時開始於2013年3月10日美國/紐約時區。 DateTime首先添加$i天(3月10日午夜),然後增加450分鐘以獲得8:30 AM(因爲3月10日凌晨1點59分之後的分鐘是凌晨3點)。參數到add的順序沒有意義;請參閱Adding a Duration to a Datetime

因爲它會分別添加天&分鐘(並且首先處理日期),效果只發生在DST實際開始或結束的日期。如果你想要一個特定的時間,只需要set而不是使用add。或撥打電話add兩次,一次添加分鐘,然後再添加幾天。