> time1 = strptime("2010-03-01 00:15:00", format = "%Y-%m-%d %H:%M:%S")
> time2a = strptime("2010-03-01", format = "%Y-%m-%d")
> time2b = as.Date(time2a)
> difftime(time1, time2a)
Time difference of 15 mins
> difftime(time1, time2b)
Time difference of 5.25 hours
在difftime的帮助页面中,接受日期对象(time2b)。为什么结果错了(5.25小时)?
谢谢。
答案 0 :(得分:5)
difftime
做的第一件事就是检查tz参数。如果缺少它使用:
if(missing(tx)) {
as.POSIXct(time1)
as.POSIXct(time2)
}
测试:
> as.POSIXct(time2b)
[1] "2010-02-28 16:00:00 PST"
> as.POSIXct(time2a)
[1] "2010-03-01 PST"
因此它将我的时区偏移应用于date
对象。
> difftime(time1,time2a)
Time difference of 15 mins
> difftime(time1,time2b,tz='GMT')
Time difference of 15 mins