Skip to content

Commit e255847

Browse files
committed
Show compound units in elapsed time display.
1 parent bd73aba commit e255847

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

lib/console/clock.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,28 @@ def self.formatted_duration(duration)
1616
return format("%.2fs", duration)
1717
end
1818

19+
seconds = duration % 60
1920
duration /= 60.0
2021

2122
if duration < 60.0
22-
return "#{duration.floor}m"
23+
return format("%dm%02ds", duration, seconds)
2324
end
2425

26+
minutes = duration % 60
2527
duration /= 60.0
2628

2729
if duration < 24.0
28-
return "#{duration.floor}h"
30+
return format("%dh%02dm", duration, minutes)
2931
end
3032

33+
hours = duration % 24
3134
duration /= 24.0
3235

33-
return "#{duration.floor}d"
36+
if duration < 100.0
37+
return format("%dd%02dh", duration, hours)
38+
end
39+
40+
return format("%dd", duration)
3441
end
3542

3643
# @returns [Time] The current monotonic time.

test/console/clock.rb

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,32 @@
1919
end
2020

2121
it "can format minutes" do
22-
expect(subject.formatted_duration(60)).to be == "1m"
23-
expect(subject.formatted_duration(61)).to be == "1m"
24-
expect(subject.formatted_duration(120)).to be == "2m"
25-
expect(subject.formatted_duration(600)).to be == "10m"
26-
expect(subject.formatted_duration(3599)).to be == "59m"
22+
expect(subject.formatted_duration(60)).to be == "1m00s"
23+
expect(subject.formatted_duration(61)).to be == "1m01s"
24+
expect(subject.formatted_duration(120)).to be == "2m00s"
25+
expect(subject.formatted_duration(600)).to be == "10m00s"
26+
expect(subject.formatted_duration(3599)).to be == "59m59s"
2727
end
2828

2929
it "can format hours" do
30-
expect(subject.formatted_duration(3600)).to be == "1h"
31-
expect(subject.formatted_duration(3601)).to be == "1h"
32-
expect(subject.formatted_duration(7200)).to be == "2h"
33-
expect(subject.formatted_duration(36000)).to be == "10h"
34-
expect(subject.formatted_duration(86399)).to be == "23h"
30+
expect(subject.formatted_duration(3600)).to be == "1h00m"
31+
expect(subject.formatted_duration(3601)).to be == "1h00m"
32+
expect(subject.formatted_duration(7200)).to be == "2h00m"
33+
expect(subject.formatted_duration(36000)).to be == "10h00m"
34+
expect(subject.formatted_duration(86399)).to be == "23h59m"
3535
end
3636

3737
it "can format days" do
38-
expect(subject.formatted_duration(86400)).to be == "1d"
39-
expect(subject.formatted_duration(86401)).to be == "1d"
40-
expect(subject.formatted_duration(172800)).to be == "2d"
41-
expect(subject.formatted_duration(604799)).to be == "6d"
42-
expect(subject.formatted_duration(864000)).to be == "10d"
38+
expect(subject.formatted_duration(86400)).to be == "1d00h"
39+
expect(subject.formatted_duration(86401)).to be == "1d00h"
40+
expect(subject.formatted_duration(172800)).to be == "2d00h"
41+
expect(subject.formatted_duration(604799)).to be == "6d23h"
42+
expect(subject.formatted_duration(864000)).to be == "10d00h"
43+
end
44+
45+
it "can format many days" do
46+
expect(subject.formatted_duration(8640000)).to be == "100d"
47+
expect(subject.formatted_duration(86400000)).to be == "1000d"
4348
end
4449
end
4550

0 commit comments

Comments
 (0)