forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeperiod.go
102 lines (89 loc) · 3.17 KB
/
timeperiod.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
package techan
import (
"fmt"
"time"
)
// TimePeriod is a simple struct that describes a period of time with a Start and End time
type TimePeriod struct {
Start time.Time
End time.Time
}
// Constants representing basic, human-readable and writable date formats
const (
SimpleDateTimeFormat = "01/02/2006T15:04:05"
SimpleDateFormat = "01/02/2006"
)
// Parse takes a string in one of the following formats and returns a new TimePeriod, and optionally, an error
//
// Supported Formats:
// SimpleDateTimeFormat:SimpleDateTimeFormat
// SimpleDateTimeFormat: (to now)
// SimpleDateFormat:
// SimpleDateFormat:SimpleDateFormat
func Parse(timerange string) (tr TimePeriod, err error) {
var start, end, layout string
switch len(timerange) {
case len(SimpleDateTimeFormat)*2 + 1:
layout = SimpleDateTimeFormat
start = string(timerange[:len(SimpleDateTimeFormat)])
end = string(timerange[len(SimpleDateTimeFormat)+1:])
case len(SimpleDateTimeFormat) + 1:
layout = SimpleDateTimeFormat
start = string(timerange[:len(SimpleDateTimeFormat)])
end = ""
case len(SimpleDateFormat)*2 + 1:
layout = SimpleDateFormat
start = string(timerange[:len(SimpleDateFormat)])
end = string(timerange[len(SimpleDateFormat)+1:])
case len(SimpleDateFormat) + 1:
layout = SimpleDateFormat
start = string(timerange[:len(SimpleDateFormat)])
end = ""
default:
err = fmt.Errorf("could not parse timerange string %s", timerange)
return
}
if tr.Start, err = time.Parse(layout, start); err != nil {
err = fmt.Errorf("could not parse time string %s", start)
}
if end == "" {
tr.End = time.Now()
} else if tr.End, err = time.Parse(layout, end); err != nil {
err = fmt.Errorf("could not parse time string %s", end)
}
return
}
// Length returns the length of the period as a time.Duration value
func (tp TimePeriod) Length() time.Duration {
return tp.End.Sub(tp.Start)
}
// Since returns the amount of time elapsed since the end of another TimePeriod as a time.Duration value
func (tp TimePeriod) Since(other TimePeriod) time.Duration {
return tp.Start.Sub(other.End)
}
// Format returns the string representation of this timePeriod in the given format
func (tp TimePeriod) Format(layout string) string {
return fmt.Sprintf("%s -> %s", tp.Start.Format(layout), tp.End.Format(layout))
}
// Advance will return a new TimePeriod with the start and end periods moved forwards or backwards in time in accordance
// with the number of iterations given.
//
// Example:
// A timePeriod that is one hour long, starting at unix time 0 and ending at unix time 3600, and advanced by one,
// will return a time period starting at unix time 3600 and ending at unix time 7200
func (tp TimePeriod) Advance(iterations int) TimePeriod {
return TimePeriod{
Start: tp.Start.Add(tp.Length() * time.Duration(iterations)),
End: tp.End.Add(tp.Length() * time.Duration(iterations)),
}
}
func (tp TimePeriod) String() string {
return tp.Format(SimpleDateTimeFormat)
}
// NewTimePeriod returns a TimePeriod starting at the given time and ending at the given time plus the given duration
func NewTimePeriod(start time.Time, period time.Duration) TimePeriod {
return TimePeriod{
Start: start,
End: start.Add(period),
}
}