Repository navigation
Expand file tree
/
Copy pathapp.go
More file actions
108 lines (90 loc) · 1.82 KB
/
Copy pathapp.go
File metadata and controls
108 lines (90 loc) · 1.82 KB
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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
type App struct {
Args []string
Config Config
}
func (app App) Usage() {
str := `Usage:
timeu [-s|-v] [time]
timeu [-s|-v] [time1] [+ | -] [time2]
Examples:
$ timeu 56
00:00:56
$ timeu -s 42:56
2576
$ timeu 11:42:56 + 2:30:59
14:13:55
$ timeu -s 11:42:56 + 2:30:59
51235
$ timeu 11:42:56 - 2:30:59
09:11:57
`
fmt.Fprintln(os.Stderr, str)
}
func (app App) FormatString(t int) string {
if app.Config.Second {
return fmt.Sprintf("%d", t)
}
var h, m, s int
h = t / 3600
m = t / 60 % 60
s = t % 60
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
func (app App) ParseTime(s string) int {
array := strings.Split(s, ":")
switch len(array) {
case 1:
i, _ := strconv.Atoi(array[0])
return i
case 2:
m, _ := strconv.Atoi(array[0])
s, _ := strconv.Atoi(array[1])
return (m * 60) + s
case 3:
h, _ := strconv.Atoi(array[0])
m, _ := strconv.Atoi(array[1])
s, _ := strconv.Atoi(array[2])
return (h * 60 * 60) + (m * 60) + s
}
fmt.Fprintf(os.Stderr, "Invalid time format: %v | array: %v\n", s, array)
return 0
}
func (app App) Run() int {
args := app.Args
switch len(args) {
case 0:
app.Usage()
return ExitCodeError
case 1:
v := app.ParseTime(args[0])
fmt.Fprintln(os.Stdout, app.FormatString(v))
return ExitCodeOK
case 3:
v1 := app.ParseTime(args[0])
op := args[1]
v2 := app.ParseTime(args[2])
switch op {
case "+":
fmt.Fprintln(os.Stdout, app.FormatString(v1+v2))
case "-":
fmt.Fprintln(os.Stdout, app.FormatString(v1-v2))
default:
fmt.Fprintln(os.Stderr, "Unknown operator:", op)
fmt.Fprintln(os.Stderr, "timeu supports + and -.")
return ExitCodeError
}
return ExitCodeOK
}
app.Usage()
return ExitCodeError
}
func NewApp(args []string, config Config) App {
return App{Args: args, Config: config}
}