-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinterval.go
95 lines (81 loc) · 2.2 KB
/
interval.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
// Copyright (c) 2023 Tim van der Molen <[email protected]>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"errors"
"fmt"
"strings"
"time"
"github.com/tbvdm/sigtop/signal"
)
func parseInterval(str string) (signal.Interval, error) {
minStr, maxStr, found := strings.Cut(str, ",")
if !found {
maxStr = minStr
}
min, err := parseTime(minStr, false)
if err != nil {
return signal.Interval{}, err
}
max, err := parseTime(maxStr, true)
if err != nil {
return signal.Interval{}, err
}
return signal.Interval{min, max}, nil
}
func parseTime(str string, max bool) (time.Time, error) {
if str == "" {
return time.Time{}, nil
}
year, month, day := 0, 0, 0
nsec := time.Duration(0)
switch len(str) {
case len("yyyy"):
year = 1
case len("yyyy-mm"):
month = 1
case len("yyyy-mm-dd"):
day = 1
case len("yyyy-mm-ddThh"):
nsec = time.Hour
case len("yyyy-mm-ddThh:mm"):
nsec = time.Minute
case len("yyyy-mm-ddThh:mm:ss"):
nsec = time.Second
default:
return time.Time{}, invalidTimeError(str)
}
layout := "2006-01-02T15:04:05"
t, err := time.ParseInLocation(layout[:len(str)], str, time.Local)
if err != nil {
var perr *time.ParseError
if errors.As(err, &perr) {
if perr.Message == "" {
err = invalidTimeError(str)
} else {
err = fmt.Errorf("%s%s", str, perr.Message)
}
}
return t, err
}
if max {
t = t.AddDate(year, month, day)
t = t.Add(nsec)
t = t.Add(-1)
}
return t, err
}
func invalidTimeError(str string) error {
return fmt.Errorf("%s: invalid time", str)
}