[英]How to sum integers and convert this to time
我有一個具有has_many曲目的播放列表。 每個磁道的持續時間為整數(以毫秒為單位)。 我想計算播放列表的總播放時間。 實際上,我是通過播放列表模型制作的:
def total_tracks_duration
seconds = tracks.sum(:duration) / 1000
Time.at(seconds).strftime("%H:%M:%S")
end
播放清單規格:
it '#total_tracks_duration' do
play = build(:playlist)
3.times do
create(:track, playlist: play, duration: 60000)
end
expect(play.total_tracks_duration).to eq("00:03:00")
end
最后我遇到了這個失敗:
Failures:
1) Playlist #total_tracks_duration
Failure/Error: expect(play.total_tracks_duration).to eq("00:03:00")
expected: "00:03:00"
got: "01:00:00"
(compared using ==)
# ./spec/models/playlist_spec.rb:51:in `block (2 levels) in <top (required)>'
Finished in 0.69605 seconds (files took 9.83 seconds to load)
1 example, 1 failure
我在哪里犯錯?
它為您的時區(UTC + 1)添加了偏移量,因此只需
Time.at(seconds).utc.strftime("%H:%M:%S")
它應該工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.