如何在 Ruby on Rails 中获取两个 DateTime 之间的秒数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/567636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the number of seconds between two DateTimes in Ruby on Rails
提问by Tilendor
I've got code that does time tracking for employees. It creates a counter to show the employee how long they have been clocked in for.
我有为员工进行时间跟踪的代码。它创建了一个计数器来向员工显示他们已经打卡了多长时间。
This is the current code:
这是当前的代码:
start_time = Time.parse(self.settings.first_clock_in)
total_seconds = Time.now - start_time
hours = (total_seconds/ 3600).to_i
minutes = ((total_seconds % 3600) / 60).to_i
seconds = ((total_seconds % 3600) % 60).to_i
This works fine. But because Time is limited to the range of 1970 - 2038 we are trying to replace all Time uses with DateTimes. I can't figure out how to get the number of seconds between two DateTimes. Subtracting them yields a Rational which I don't know how to interpret, whereas subtracting Times yields the difference in seconds.
这工作正常。但是因为时间限制在 1970 - 2038 的范围内,我们试图用日期时间替换所有时间使用。我不知道如何获得两个 DateTimes 之间的秒数。减去它们会产生一个我不知道如何解释的 Rational,而减去 Times 会产生以秒为单位的差异。
NOTE: Since Ruby 1.9.2, the hard limit of Time is removed. However, Time is optimized for values between 1823-11-12 and 2116-02-20.
注意:从 Ruby 1.9.2 开始,时间的硬限制被删除。但是,时间针对 1823-11-12 和 2116-02-20 之间的值进行了优化。
回答by David Ma
Subtracting two DateTimes returns the elapsed time in days, so you could just do:
减去两个 DateTimes 以天为单位返回经过的时间,所以你可以这样做:
elapsed_seconds = ((end_time - start_time) * 24 * 60 * 60).to_i
回答by Avram Dorfman
Or, more readably:
或者,更易读:
diff = datetime_1 - datetime_2
diff * 1.days # => difference in seconds; requires Ruby on Rails
Note, what you or some other searchers might really be looking for is this:
请注意,您或其他一些搜索者可能真正要寻找的是:
diff = datetime_1 - datetime_2
Date.day_fraction_to_time(diff) # => [h, m, s, frac_s]
回答by Luke
You can convert them to floats with to_f, though this will incur the usual loss of precision associated with floats. If you're just casting to an integer for whole seconds it shouldn't be big enough to be a worry.
您可以使用 将它们转换为浮点数to_f,但这会导致通常与浮点数相关的精度损失。如果您只是在整秒内转换为整数,那么它不应该大到足以令人担忧。
The results are in seconds:
结果以秒为单位:
>> end_time.to_f - start_time.to_f
=> 7.39954495429993
>> (end_time.to_f - start_time.to_f).to_i
=> 7
Otherwise, you could look at using to_formatted_son the DateTime object and seeing if you can coax the output into something the Decimalclass will accept, or just formatting it as plain Unix time as a string and calling to_ion that.
否则,您可以查看to_formatted_sDateTime 对象上的using并查看是否可以将输出哄骗为Decimal类将接受的内容,或者只是将其格式化为纯 Unix 时间作为字符串并调用to_i它。
回答by Amin Ariana
Others incorrectly rely on fractions or helper functions. It's much simpler than that. DateTime itself is integer underneath. Here's the Ruby way:
其他人错误地依赖分数或辅助函数。它比那简单得多。DateTime 本身是下面的整数。这是Ruby的方式:
stop.to_i - start.to_i
Example:
例子:
start = Time.now
=> 2016-06-21 14:55:36 -0700
stop = start + 5.seconds
=> 2016-06-21 14:55:41 -0700
stop.to_i - start.to_i
=> 5
回答by Som Poddar
回答by Francois
there's a method made for that:
有一个方法:
Time.now.minus_with_coercion(10.seconds.ago)
Time.now.minus_with_coercion(10.seconds.ago)
equals 10.
等于 10。
Source: http://apidock.com/rails/Time/minus_with_coercion
来源:http: //apidock.com/rails/Time/minus_with_coercion
Hope I helped.
希望我有所帮助。
回答by ramya
Define a Ruby function like this,
像这样定义一个 Ruby 函数,
def time_diff(start_time, end_time)
seconds_diff = (start_time - end_time).to_i.abs
days = seconds_diff / 86400
seconds_diff -= days * 86400
hours = seconds_diff / 3600
seconds_diff -= hours * 3600
minutes = seconds_diff / 60
seconds_diff -= minutes * 60
seconds = seconds_diff
"#{days} Days #{hours} Hrs #{minutes} Min #{seconds} Sec"
end
And Call this function,
并调用这个函数,
time_diff(Time.now, Time.now-4.days-2.hours-1.minutes-53.seconds)
回答by zee
why not use the built in "sec" . Here is an example :
为什么不使用内置的 "sec" 。这是一个例子:
t1 = Time.now.sec
elapsed_t = Time.now.sec - t1
puts "it took me : #{elapsed_t} seconds to do something that is useful \n"

