Skip to content

Commit 2028962

Browse files
committed
utils: time format
1 parent aa22c37 commit 2028962

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/utils/time_format.cr

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,30 @@ module MoonScript
2323
MILLISECONDS_IN_SECOND = 1_000
2424
MICROSECONDS_IN_SECOND = 1_000_000
2525

26-
def self.auto(time: Time::Span) : String
27-
time_f = time.to_f
26+
def self.auto(time : Time::Span) : String
27+
time_f = time.to_f
2828

29-
return minutes(time_f) if time_f / SECONDS_IN_MINUTE >= 1
30-
return seconds(time_f) if time_f >= 1
31-
return milliseconds(time_f) if time_f * MILLISECONDS_IN_SECOND >= 1
29+
return minutes(time_f) if time_f / SECONDS_IN_MINUTE >= 1
30+
return seconds(time_f) if time_f >= 1
31+
return milliseconds(time_f) if time_f * MILLISECONDS_IN_SECOND >= 1
3232

33-
microseconds(time_f)
33+
microseconds(time_f)
3434
end
3535

36-
private def self.microseconds(time_f: Float) : String
37-
"#{(time_f * MICROSECONDS_IN_SECOND).to_i}us"
36+
private def self.minutes(time_f : Float) : String
37+
"#{(time_f / SECONDS_IN_MINUTE).round(2)}m"
38+
end
39+
40+
private def self.seconds(time_f : Float) : String
41+
"#{time_f.round(3)}s"
3842
end
39-
end
4043

41-
44+
private def self.milliseconds(time_f : Float) : String
45+
"#{(time_f * MILLISECONDS_IN_SECOND).round(3)}ms"
46+
end
47+
48+
private def self.microseconds(time_f : Float) : String
49+
"#{(time_f * MICROSECONDS_IN_SECOND).to_i}μs"
50+
end
51+
end
4252
end

0 commit comments

Comments
 (0)