我有一个红宝石应用程序,我需要以天 - 小时 - 分钟格式获取日期 - 时间差。对于这个即时通讯使用下面的函数ROR中的日期时间差
def duration (from_time, to_time)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_seconds = ((to_time - from_time).abs).round
secs = distance_in_seconds.to_int
mins = secs/60
hours = mins/60
days = hours/24
if days > 0
"#{days}d #{hours % 24}h"
elsif hours > 0
"#{hours}h #{mins % 60}m"
elsif mins > 0
"#{mins}m"
end
end
上面这样调用从另一个功能
duration(aw_updated, Time.now)
但有些时候它给了我错误的结果,
当我显示上述数值
aw_updated is 2012-09-19 04:23:34 UTC
Time.now is 2012-09-19 16:33:09 +0530
Time.now.utc is 2012-09-19 11:03:09 UTC
And
Diff is 6h 26m
但是我的系统时间为2012-09-19 16点33分09秒
不知道在哪里在做错误的,有些UTC问题?
请指教
对我来说看起来很好。您的系统时间显示UTC + 5.30 – Sebi
差异不应超过12小时? –
04:23:34和11:03:09之间的区别是6h:39m:35s。尝试再次运行代码并发布输出。 – Sebi