@@ -18,23 +18,39 @@ def elapsed_text(elapsed)
1818 " #{ (millis * 1000 ).round(2 ) } µs"
1919end
2020
21- def decode_length_seconds (string )
22- length_seconds = string.gsub(/[^0-9:] / , " " )
23- return 0 _i32 if length_seconds.empty?
21+ module TimeSpanConverter
22+ def self.to_yaml (value : Time ::Span , yaml : YAML ::Nodes ::Builder )
23+ return yaml.scalar recode_length_seconds(value.total_seconds.to_i32)
24+ end
2425
25- length_seconds = length_seconds.split(" :" ).map { |x | x.to_i? || 0 }
26- length_seconds = [0 ] * (3 - length_seconds.size) + length_seconds
26+ def self.from_yaml (ctx : YAML ::ParseContext , node : YAML ::Nodes ::Node ) : Time ::Span
27+ if node.is_a?(YAML ::Nodes ::Scalar )
28+ return decode_time_span(node.value)
29+ else
30+ node.raise " Expected scalar, not #{ node.class } "
31+ end
32+ end
33+ end
2734
28- length_seconds = Time ::Span .new(
29- hours: length_seconds[0 ],
30- minutes: length_seconds[1 ],
31- seconds: length_seconds[2 ]
32- ).total_seconds.to_i32
35+ def decode_time_span (string : String ) : Time ::Span
36+ time_span = string.gsub(/[^0-9:] / , " " )
37+ return Time ::Span .new(seconds: 0 ) if time_span.empty?
38+
39+ time_span = time_span.split(" :" ).map { |x | x.to_i? || 0 }
40+ time_span = [0 ] * (3 - time_span.size) + time_span
41+
42+ return Time ::Span .new(
43+ hours: time_span[0 ],
44+ minutes: time_span[1 ],
45+ seconds: time_span[2 ]
46+ )
47+ end
3348
34- return length_seconds
49+ def decode_length_seconds (string : String ) : Int32
50+ return decode_time_span(string).total_seconds.to_i32
3551end
3652
37- def recode_length_seconds (time )
53+ def recode_length_seconds (time : Int32 ) : String
3854 if time <= 0
3955 return " "
4056 else
0 commit comments