在 Ruby 中计算持续时间与毫秒之间的差异

发布于 2024-12-21 00:10:20 字数 1290 浏览 0 评论 0原文

TL;DR: 我需要了解 HH:MM:SS.msHH:MM:SS.ms 之间的区别为 HH:MM:SS:ms


我需要什么:

这是一个棘手的问题。我正在尝试计算两个时间戳之间的差异,如下所示:

In: 00:00:10.520
Out: 00:00:23.720

应交付:

Diff: 00:00:13.200

我认为我会将时间解析为实际的 Time 对象并使用其中的差异。这在前一种情况下效果很好,并返回 00:0:13.200

什么不起作用:

但是,对于某些人来说,这不起作用,因为 Ruby 使用 usec 而不是 msec

In: 00:2:22.760
Out: 00:2:31.520
Diff: 00:0:8.999760

显然,区别应该是 00 :00:8:760 而不是 00:00:8.999760我真的很想 tdiff.usec.to_s.gsub('999','') ……


到目前为止我的代码:

这是到目前为止我的代码(这些从输入字符串(如“0:00:10:520”)中解析。

tin_first, tin_second = ins.split(".")
tin_hours, tin_minutes, tin_seconds = tin_first.split(":")
tin_usec = tin_second * 1000
tin = Time.gm(0, 1, 1, tin_hours, tin_minutes, tin_seconds, tin_usec)

tout 也会发生同样的情况。然后:

tdiff = Time.at(tout-tin)

对于输出,我使用:

"00:#{tdiff.min}:#{tdiff.sec}.#{tdiff.usec}"

有没有更快的方法来做到这一点?请记住,我只想得到两个时间之间的差异。我缺少什么?

我现在使用的是 Ruby 1.9.3p6。

TL;DR: I need to get the difference between HH:MM:SS.ms and HH:MM:SS.ms as HH:MM:SS:ms


What I need:

Here's a tricky one. I'm trying to calculate the difference between two timestamps such as the following:

In: 00:00:10.520
Out: 00:00:23.720

Should deliver:

Diff: 00:00:13.200

I thought I'd parse the times into actual Time objects and use the difference there. This works great in the previous case, and returns 00:0:13.200.

What doesn't work:

However, for some, this doesn't work right, as Ruby uses usec instead of msec:

In: 00:2:22.760
Out: 00:2:31.520
Diff: 00:0:8.999760

Obviously, the difference should be 00:00:8:760 and not 00:00:8.999760. I'm really tempted to just tdiff.usec.to_s.gsub('999','') ……


My code so far:

Here's my code so far (these are parsed from the input strings like "0:00:10:520").

tin_first, tin_second = ins.split(".")
tin_hours, tin_minutes, tin_seconds = tin_first.split(":")
tin_usec = tin_second * 1000
tin = Time.gm(0, 1, 1, tin_hours, tin_minutes, tin_seconds, tin_usec)

The same happens for tout. Then:

tdiff = Time.at(tout-tin)

For the output, I use:

"00:#{tdiff.min}:#{tdiff.sec}.#{tdiff.usec}"

Is there any faster way to do this? Remember, I just want to have the difference between two times. What am I missing?

I'm using Ruby 1.9.3p6 at the moment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶舞 2024-12-28 00:10:20

使用 Time

require 'time' # Needed for Time.parse

def time_diff(time1_str, time2_str)
  t = Time.at( Time.parse(time2_str) - Time.parse(time1_str) )
  (t - t.gmt_offset).strftime("%H:%M:%S.%L")
end

out_time = "00:00:24.240"
in_time  = "00:00:14.520"

p time_diff(in_time, out_time)
#=> "00:00:09.720"

这是一个不依赖于 Time 的解决方案:

def slhck_diff( t1, t2 )
  ms_to_time( time_as_ms(t2) - time_as_ms(t1) )
end

# Converts "00:2:22.760" to 142760
def time_as_ms( time_str )
  re = /(\d+):(\d+):(\d+)(?:\.(\d+))?/
  parts = time_str.match(re).to_a.map(&:to_i)
  parts[4]+(parts[3]+(parts[2]+parts[1]*60)*60)*1000
end

# Converts 142760 to "00:02:22.760"
def ms_to_time(ms)
  m = ms.floor / 60000
  "%02i:%02i:%06.3f" % [ m/60, m%60, ms/1000.0 % 60 ]
end

t1 = "00:00:10.520"
t2 = "01:00:23.720"
p slhck_diff(t1,t2)
#=> "01:00:13.200"

t1 = "00:2:22.760"
t2 = "00:2:31.520"
p slhck_diff(t1,t2)
#=> "00:00:08.760"

Using Time:

require 'time' # Needed for Time.parse

def time_diff(time1_str, time2_str)
  t = Time.at( Time.parse(time2_str) - Time.parse(time1_str) )
  (t - t.gmt_offset).strftime("%H:%M:%S.%L")
end

out_time = "00:00:24.240"
in_time  = "00:00:14.520"

p time_diff(in_time, out_time)
#=> "00:00:09.720"

Here's a solution that doesn't rely on Time:

def slhck_diff( t1, t2 )
  ms_to_time( time_as_ms(t2) - time_as_ms(t1) )
end

# Converts "00:2:22.760" to 142760
def time_as_ms( time_str )
  re = /(\d+):(\d+):(\d+)(?:\.(\d+))?/
  parts = time_str.match(re).to_a.map(&:to_i)
  parts[4]+(parts[3]+(parts[2]+parts[1]*60)*60)*1000
end

# Converts 142760 to "00:02:22.760"
def ms_to_time(ms)
  m = ms.floor / 60000
  "%02i:%02i:%06.3f" % [ m/60, m%60, ms/1000.0 % 60 ]
end

t1 = "00:00:10.520"
t2 = "01:00:23.720"
p slhck_diff(t1,t2)
#=> "01:00:13.200"

t1 = "00:2:22.760"
t2 = "00:2:31.520"
p slhck_diff(t1,t2)
#=> "00:00:08.760"
黑色毁心梦 2024-12-28 00:10:20

我认为以下内容可行:

out_time = "00:00:24.240"
in_time = "00:00:14.520"
diff = Time.parse(out_time) - Time.parse(in_time)
Time.at(diff).strftime("%H:%M:%S.%L")
# => "01:00:09.720"

确实打印一小时的01,但我不太明白。

与此同时,我使用了:

Time.at(diff).strftime("00:%M:%S.%L")
# => "00:00:09.720"

当然,任何做得更好的答案都会得到赞成票或接受。

I figured the following could work:

out_time = "00:00:24.240"
in_time = "00:00:14.520"
diff = Time.parse(out_time) - Time.parse(in_time)
Time.at(diff).strftime("%H:%M:%S.%L")
# => "01:00:09.720"

It does print 01 for the hour, which I don't really understand.

In the meantime, I used:

Time.at(diff).strftime("00:%M:%S.%L")
# => "00:00:09.720"

Any answer that does this better will get an upvote or the accept, of course.

作业与我同在 2024-12-28 00:10:20
in_time = "00:02:22.760"
out_time = "00:02:31.520"
diff = (Time.parse(out_time) - Time.parse(in_time))*1000
puts diff

输出:

8760.0 millliseconds

Time.parse(out_time) - Time.parse(in_time) 给出以为单位的结果,乘以1000以转换为毫秒

在此处输入图像描述

in_time = "00:02:22.760"
out_time = "00:02:31.520"
diff = (Time.parse(out_time) - Time.parse(in_time))*1000
puts diff

OUTPUT:

8760.0 millliseconds

Time.parse(out_time) - Time.parse(in_time) gives the result in seconds so multiplied by 1000 to convert into milliseconds.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文