-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeutil
executable file
·115 lines (101 loc) · 2.44 KB
/
timeutil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env ruby
# author: eric
require 'optparse'
class Tiime #{{{1
attr_reader :time
def initialize(t)
if t.class == Fixnum
# 由整数构造对象
@time = t
else
# 由字符构造对象
# 用正则式抽取数字字符,
digits = t.scan /\d+/
# 根据数组的元素个数进行判断
# 只有一个则全为秒,两个则为mm:ss,三个则是hh:mm:ss
# 全部转换为秒单位
case digits.length
when 1
@time = digits[0].to_i
when 2
@time = digits[0].to_i*60 + digits[1].to_i
when 3
@time = digits[0].to_i*3600 + digits[1].to_i*60 + digits[2].to_i
end
end
end
def + other
# 与other相加,并返回新Tiime对象
Tiime.new(@time + other.time)
end
def - other
# 与other求差,得绝对值并返回新Tiime对象
Tiime.new((@time - other.time).abs)
end
def to_s
# 输出字符串,以秒形式
@time.to_s
end
def to_hms
# 转成hh:mm:ss格式的字符串
h, h_left = @time/3600, @time%3600
m, s = h_left/60, h_left%60
format "%02d:%02d:%02d\n", h, m, s
end
def to_ms
# 转成mm:ss格式的字符串
m, s = @time/60, @time%60
format "%02d:%02d", m, s
end
end
OptionParser.new do |opts| #{{{1
# 帮助文档的顶部条
opts.banner =<<-END
Time Utility...
Usage: timeutil.rb time [options]
END
$options = {}
# 设定开关参数的初始值
$options[:to] = "sec"
# 设定开关
opts.on('-t TIME_FORMAT', '--to',
'output format: sec|ms|hms, omit: sec') \
do |format|
$options[:to] = format
end
$options[:add] = 0
opts.on('-a TIME', '--add', 'add given time') \
do |time|
$options[:add] = time
end
$options[:minus] = 0
opts.on('-m TIME', '--minus', 'minus given time') \
do |time|
$options[:minus] = time
end
# 打印用法说明
opts.on('-h', '--help', 'display this screen') \
do
puts opts
exit
end
end.parse! # 执行分析,从入口参数组中去除开关及其参数,留下操作对象本身
# main {{{1
if ARGV.length != 0
# 计算加减结果
result = Tiime.new(ARGV[0]) +
Tiime.new($options[:add]) -
Tiime.new($options[:minus])
# 根据指定的输出格式输出
case $options[:to]
when "sec"
puts result.to_s
when "hms"
puts result.to_hms
when "ms"
puts result.to_ms
else
# 默认以秒单位输出
puts result.to_s
end
end