简体   繁体   English

R中difftime的结果与excel和timeanddate.com不同

[英]Results with difftime in R different from excel and timeanddate.com

t1 <- strptime("2015-02-17 12:20:00", format = "%Y-%m-%d %H:%M:%S", tz = "America/Chicago")
t2 <- strptime("2015-03-13 15:00:00", format = "%Y-%m-%d %H:%M:%S", tz = "America/Chicago")
as.numeric(abs(difftime(t1, t2, units = "mins", tz = "America/Chicago")))

The above returns 34,660 while Excel and http://www.timeanddate.com/date/duration.html both return 34,720. 以上返回34,660,而Excel和http://www.timeanddate.com/date/duration.html均返回34,720。 R recognizes t1 as CST and t2 as CDT due to daylight savings on 3/8/15. 由于2015年3月8日的夏令时,R将t1识别为CST,t2识别为CDT。 I wanted to confirm that R is correct, while these other two sources are not. 我想确认R是正确的,而其他两个来源则没有。

Depends if you want to take into account local daylight savings, or if these times are two points in time irrelevant of local shifts. 取决于您是否要考虑当地夏令时,或者这些时间是两个与当地班次无关的时间点。 Excel and that site are just taking the exact difference between those two times, discarding daylight savings - eg R can get the same result by changing the timezones to UTC , which doesn't observe daylight saving: Excel和那个网站只是采用了这两次之间的确切差异,放弃了夏令时 - 例如R可以通过将时区更改为UTC来获得相同的结果,这不会观察到夏令时:

difftime(as.POSIXct("2015-03-13 15:00:00",tz="UTC"),
         as.POSIXct("2015-02-17 12:20:00",tz="UTC"), units="mins")
# Time difference of 34720 mins

If I'm sitting in Chicago with a stopwatch and counting the minutes difference between when a local clock in the pub says "2015-02-17 12:20:00" and then hits "2015-03-13 15:00:00" , I'd count 60 minutes less as the clock got wound forward an hour for daylight savings while I was counting. 如果我带着秒表坐在芝加哥,并计算酒吧当地时钟说"2015-02-17 12:20:00"然后点击"2015-03-13 15:00:00" "2015-02-17 12:20:00"之间的分钟差异"2015-03-13 15:00:00"我的数量减少了60分钟,因为时钟在一小时的时间里用于夏令时,而我在数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM