-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
113 lines (105 loc) · 3.25 KB
/
parse.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/rs/zerolog/log"
)
func parseDuration(timeExpr string) int {
seconds := 0
times := strings.Split(timeExpr, " ")
if len(times) == 2 && len(times[1]) > 2 && times[1][:3] == "sec" {
seconds, _ = strconv.Atoi(times[0])
} else if len(times) == 2 && len(times[1]) > 2 && times[1][:3] == "min" {
minutes, _ := strconv.Atoi(times[0])
seconds = minutes * 60
} else if len(times) == 2 && len(times[1]) > 3 && times[1][:4] == "hour" {
minutes, _ := strconv.Atoi(times[0])
seconds = minutes * 3600
}
return seconds
}
func parseStart(startStr string) (time.Time, error) {
startTime := time.Now().Local().Add(time.Duration(int64(1000000000)))
var err error
if startStr != "" {
matchTime, _ := regexp.Match("^\\d{1,2}(:\\d{2}){1,2}$", []byte(startStr))
if matchTime {
startTime, err = time.Parse("15:04", startStr)
if err != nil {
startTime, err = time.Parse("15:04:05", startStr)
if err != nil {
return startTime, fmt.Errorf("Error: invalid time format: %s", startStr)
}
}
} else if strings.Contains(startStr, "sec") || strings.Contains(startStr, "min") || strings.Contains(startStr, "hour") {
seconds := parseDuration(startStr)
if seconds > 0 {
offset := time.Duration(int64(seconds) * int64(1000000000))
startTime = time.Now().Local().Add(offset)
} else {
return startTime, fmt.Errorf("Invalid duration: %s", startStr)
}
} else {
return startTime, fmt.Errorf("Invalid start time: %s", startStr)
}
}
return startTime, err
}
func parseInterval(intervalStr string, messages []string) (time.Duration, error) {
var err error
// default interval is 30 sec.
interval := time.Duration(int64(30) * int64(1000000000))
if intervalStr != "" {
seconds := parseDuration(intervalStr)
if seconds > 0 {
interval = time.Duration(int64(seconds) * int64(1000000000))
} else {
err = fmt.Errorf("Invalid time interval: %s", intervalStr)
}
} else {
if len(messages) > 1 {
log.Warn().Msg("Warning: no interval set, default interval is 30 seconds")
}
}
return interval, err
}
func parseUntil(untilStr string, startTime time.Time) (int, time.Time) {
until := 1
untilTime := time.Now().Local()
var err error
if untilStr != "" {
matchTime, _ := regexp.Match("^\\d{1,2}(:\\d{2}){1,2}$", []byte(untilStr))
matchTimes, _ := regexp.Match("^\\d*( time| times){0,1}$", []byte(untilStr))
if matchTime {
untilTime, err = time.Parse("15:04", untilStr)
if err != nil {
untilTime, err = time.Parse("15:04:05", untilStr)
if err != nil {
log.Error().Msgf("Error: 'until' invalid time format: %s", untilStr)
} else {
until = -1
}
} else {
until = -1
}
} else if strings.Contains(untilStr, "sec") || strings.Contains(untilStr, "min") || strings.Contains(untilStr, "hour") {
seconds := parseDuration(untilStr)
if seconds > 0 {
offset := time.Duration(int64(seconds) * int64(1000000000))
untilTime = startTime.Add(offset)
until = -1
} else {
log.Error().Msgf("Invalid 'until' duration: %s", untilStr)
}
} else if matchTimes {
times := strings.Split(untilStr, " ")
until, _ = strconv.Atoi(times[0])
} else {
log.Error().Msgf("Invalid 'until': %s", untilStr)
}
}
return until, untilTime
}