-
Notifications
You must be signed in to change notification settings - Fork 99
/
datetime.go
100 lines (90 loc) · 2.25 KB
/
datetime.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
package datetime
import (
"time"
)
// Interval is the aggregation of each chart bar.
type Interval string
const (
// OneMin interval of 1 minute.
OneMin Interval = "1m"
// TwoMins interval of 2 minutes.
TwoMins Interval = "2m"
// FiveMins interval of 5 minutes.
FiveMins Interval = "5m"
// FifteenMins interval of 15 minutes.
FifteenMins Interval = "15m"
// ThirtyMins interval of thirty minutes.
ThirtyMins Interval = "30m"
// SixtyMins interval of 60 minutes.
SixtyMins Interval = "60m"
// NinetyMins interval of 90 minutes.
NinetyMins Interval = "90m"
// OneHour interval of 1 hour.
OneHour Interval = "1h"
// OneDay interval of 1 day.
OneDay Interval = "1d"
// FiveDay interval of 5 days.
FiveDay Interval = "5d"
// OneMonth interval of 1 month.
OneMonth Interval = "1mo"
// ThreeMonth interval of 3 months.
ThreeMonth Interval = "3mo"
// SixMonth interval of 6 months.
SixMonth Interval = "6mo"
// OneYear interval of 1 year.
OneYear Interval = "1y"
// TwoYear interval of 2 years.
TwoYear Interval = "2y"
// FiveYear interval of 5 years.
FiveYear Interval = "5y"
// TenYear interval of 10 years.
TenYear Interval = "10y"
// YTD interval of year-to-date.
YTD Interval = "ytd"
// Max interval.
Max Interval = "max"
)
// Datetime is a simple time construct,
// that is either the start point or the end point
// for a chart time-series.
type Datetime struct {
Day int
Month int
Year int
t *time.Time
}
// New creates a new instance of Datetime from a go time struct.
func New(t *time.Time) *Datetime {
year, month, day := t.Date()
return &Datetime{
Month: int(month),
Day: day,
Year: year,
t: t,
}
}
// FromUnix returns a new instance of Datetime from a unix timestamp.
func FromUnix(timestamp int) *Datetime {
t := time.Unix(int64(timestamp), 0)
return New(&t)
}
// Time returns a go time struct from a datetime.
func (d *Datetime) Time() *time.Time {
if d.t != nil {
return d.t
}
d.calculateTime()
return d.Time()
}
// Unix returns a valid unix timestamp from Datetime fields.
func (d *Datetime) Unix() int {
if d.t != nil {
return int(d.t.Unix())
}
d.calculateTime()
return d.Unix()
}
func (d *Datetime) calculateTime() {
t := time.Date(d.Year, time.Month(d.Month), d.Day, 9, 30, 0, 0, time.Local)
d.t = &t
}