-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
99 lines (74 loc) · 2.11 KB
/
time.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
package time
import (
"strconv"
"strings"
"time"
)
type ITime interface {
NowMilliseconds() int64
}
// https://www.golangprograms.com/add-n-number-of-year-month-day-hour-minute-second-millisecond-microsecond-and-nanosecond-to-current-date-time.html
// NowMilliseconds returns a unix timestamp (int64) in milliseconds
func NowMilliseconds() int64 {
return ToMilliseconds(time.Now())
}
// ToMilliseconds converts a time to milliseconds
func ToMilliseconds(t time.Time) int64 {
return t.UnixNano() / 1000000
}
// NowAddDateMilliseconds adds year, month, day to a date and returns the milliseconds
func NowAddDateMilliseconds(year int, month int, day int) int64 {
return ToMilliseconds(time.Now().AddDate(year, month, day))
}
// ParseRFC3339Offset parses an RFC3339 and returns its offset in seconds
func ParseRFC3339Offset(dateString string) (offsetSeconds int64, e error) {
offsetString := dateString[len(dateString)-6:]
offsetParts := strings.Split(offsetString, ":")
var offsetHour int64 = 0
var offsetMinute int64 = 0
offsetPlusMinus := offsetParts[0][0]
if offsetHour, e = strconv.ParseInt(offsetParts[0][1:], 10, 64); e != nil {
return
}
if offsetMinute, e = strconv.ParseInt(offsetParts[1], 10, 64); e != nil {
return
}
offsetSeconds = (offsetHour * 3600) + (offsetMinute * 60)
if offsetPlusMinus == '-' {
offsetSeconds = 0 - offsetSeconds
}
return
}
// type Timer struct {
// t *time.Time
// }
// func NewTimer() *Timer {
// now := time.Now()
// return &Timer{
// t: &now,
// }
// }
// func (t *Timer) AddYear(year int) *Timer {
// t.t.AddDate(year, 0, 0)
// return t
// }
// func (t *Timer) AddMonth(month int) *Timer {
// t.t.AddDate(0, month, 0)
// return t
// }
// func (t *Timer) AddDay(day int) *Timer {
// t.t.AddDate(0, 0, day)
// return t
// }
// func (t *Timer) AddHour(hour int) *Timer {
// t.t.Add(time.Duration(hour) * time.Hour)
// return t
// }
// func (t *Timer) AddMinute(minute int) *Timer {
// t.t.Add(time.Duration(minute) * time.Minute)
// return t
// }
// func (t *Timer) AddSecond(second int) *Timer {
// t.t.Add(time.Duration(second) * time.Second)
// return t
// }