forked from sjauld/nemtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nemtime.go
49 lines (40 loc) · 1.35 KB
/
nemtime.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
package nemtime
import (
"time"
)
// According the Australian Energy Market Operator, everything happens in
// UTC+10:00
var (
NEMTZOffset = 10 * time.Hour
NEMTZCorrection = time.Duration(-NEMTZOffset)
NEMTimeZone = time.FixedZone("NEM", int(NEMTZOffset.Seconds()))
// We'll accept a reasonably standard date format as input
DateLayout = "2006-01-02"
)
// NEMTime represents a point in time as understood by a simple string, from the
// perspective of the Australian Energy Market Operator
type NEMTime struct {
time.Time
}
// FromStartDateString takes a simple date string as an input and returns a
// pointer to a NEMTime representing the start of that day
func FromStartDateString(s string) (*NEMTime, error) {
baseTime, err := time.Parse(DateLayout, s)
if err != nil {
return nil, err
}
return &NEMTime{baseTime.In(NEMTimeZone).Add(NEMTZCorrection)}, nil
}
// FromEndDateString takes a simple date string as an input and returns a
// pointer to a NEMTime representing the end of that day
func FromEndDateString(s string) (*NEMTime, error) {
baseTime, err := time.Parse(DateLayout, s)
if err != nil {
return nil, err
}
return &NEMTime{baseTime.In(NEMTimeZone).Add(NEMTZCorrection).Add(time.Duration(24 * time.Hour))}, nil
}
// String represents a NEMTime in an RFC3339 string
func (n *NEMTime) String() string {
return n.Format(time.RFC3339)
}