-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (78 loc) · 1.53 KB
/
main.go
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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
var (
daysF, weeksF, versionF bool
version string
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of deployed: [VALUE] [UNIT days weeks]\n")
flag.PrintDefaults()
}
flag.BoolVar(&versionF, "v", false, "Display version information")
}
func main() {
flag.Parse()
if versionF {
fmt.Println(version)
os.Exit(0)
}
if flag.NArg() == 0 {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
if err := calculate(scanner.Text()); err != nil {
log.Fatal(err)
}
}
} else {
if err := calculate(flag.Arg(0) + " " + flag.Arg(1)); err != nil {
log.Fatal(err)
}
}
}
func calculate(inputStr string) (err error) {
input, err := parseInput(inputStr)
if err != nil {
return
}
var duration string
if daysF {
duration = strconv.FormatFloat(input * 24.0, 'f', -1, 64) + "h"
}
if weeksF {
duration = strconv.FormatFloat(input * 24.0 * 7.0, 'f', -1, 64) + "h"
}
answer, err := time.ParseDuration(duration)
if err != nil {
return
}
fmt.Println(time.Now().Add(answer * -1).UTC().Format("2006-01-02 T15:04 -0700"))
return nil
}
func parseInput(input string) (value float64, err error) {
comp := strings.Split(input, " ")
if len(comp) == 0 {
return 0.0, fmt.Errorf("no input\n")
}
value, err = strconv.ParseFloat(comp[0], 64)
if err != nil {
return
}
switch comp[1] {
case "day", "days":
daysF = true
case "week", "weeks":
weeksF = true
}
return
}