Ruby-on-rails Rails - 获取以小时、分钟和秒为单位的时差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19595840/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:56:39  来源:igfitidea点击:

Rails - Get the time difference in hours, minutes and seconds

ruby-on-railsrubyruby-on-rails-3ruby-on-rails-4ruby-2.0

提问by Tumas

I'm looking for an idiomatic way to get the time passed since a given date in hours, minutes and seconds.

我正在寻找一种惯用的方法来以小时、分钟和秒为单位获取自给定日期以来经过的时间。

If the given date is 2013-10-25 23:55:00 and the current date is 2013-10-27 20:55:09, the returning value should be 45:03:09. The time_differenceand time_diffgems won't work with this requirement.

如果给定日期是 2013-10-25 23:55:00,当前日期是 2013-10-27 20:55:09,则返回值应该是 45:03:09。该TIME_DIFFERENCEtime_diff宝石不会与此要求的工作。

回答by MrYoshiji

You can try with this:

你可以试试这个:

def time_diff(start_time, end_time)
  seconds_diff = (start_time - end_time).to_i.abs

  hours = seconds_diff / 3600
  seconds_diff -= hours * 3600

  minutes = seconds_diff / 60
  seconds_diff -= minutes * 60

  seconds = seconds_diff

  "#{hours.to_s.rjust(2, '0')}:#{minutes.to_s.rjust(2, '0')}:#{seconds.to_s.rjust(2, '0')}"
  # or, as hagello suggested in the comments:
  # '%02d:%02d:%02d' % [hours, minutes, seconds]
end

And use it:

并使用它:

time_diff(Time.now, Time.now-2.days-3.hours-4.minutes-5.seconds)
# => "51:04:04"

回答by Tumas

time_diff = Time.now - 2.minutes.ago
Time.at(time_diff.to_i.abs).utc.strftime "%H:%M:%S"
=> "00:01:59"

Or if your app is picky about rounding, just replace to_i to round:

或者,如果您的应用对四舍五入很挑剔,只需将 to_i 替换为四舍五入:

  Time.at(time_diff.round.abs).utc.strftime "%H:%M:%S"
   => => "00:02:00"

Not sure about the idiomatic part though

虽然不确定惯用的部分

Update: If time difference is expected to be more than 24 hours then the above code is not correct. If such is the case, one could follow the answer of @MrYoshiji or adjust above solution to calculate hours from a datetime object manually:

更新:如果时差预计超过 24 小时,则上述代码不正确。如果是这种情况,可以按照@MrYoshiji 的回答或调整上述解决方案以手动计算日期时间对象的小时数:

def test_time time_diff
  time_diff = time_diff.round.abs
  hours = time_diff / 3600

  dt = DateTime.strptime(time_diff.to_s, '%s').utc
  "#{hours}:#{dt.strftime "%M:%S"}"
end

test_time Time.now - 28.hours.ago - 2.minutes - 12.seconds
=> "27:57:48" 
test_time Time.now - 8.hours.ago - 2.minutes - 12.seconds
=> "7:57:48" 
test_time Time.now - 24.hours.ago - 2.minutes - 12.seconds
=> "23:57:48" 
test_time Time.now - 25.hours.ago - 2.minutes - 12.seconds
=> "24:57:48" 

回答by Francois

There's a method for that:

有一个方法:

Time.now.minus_with_coercion(10.seconds.ago)

Time.now.minus_with_coercion(10.seconds.ago)

equals 10 (in fact, 9.99..., use .roundif you want a 10).

等于 10(实际上是 9.99...,.round如果您想要 10 ,请使用)。

Source: http://apidock.com/rails/Time/minus_with_coercion

来源:http: //apidock.com/rails/Time/minus_with_coercion

Hope I helped.

希望我有所帮助。

回答by Danny

Look at

看着

how to convert 270921sec into days + hours + minutes + sec ? (ruby)

如何将 270921sec 转换为天 + 小时 + 分钟 + 秒?(红宝石)

which is very similar to your question, just starting from the difference in seconds

这与您的问题非常相似,只是从几秒钟的差异开始

回答by Maged Makled

Once you get the time difference in seconds, you could do something like this

一旦你得到以秒为单位的时差,你可以做这样的事情

Time.at(t).utc.strftime("%H:%M:%S")

Where tis the time difference in seconds

t以秒为单位的时差在哪里