forked from jinzhu/now
-
Notifications
You must be signed in to change notification settings - Fork 0
/
now.go
182 lines (152 loc) · 4.31 KB
/
now.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package now
import (
"errors"
"regexp"
"time"
)
func (now *Now) BeginningOfMinute() time.Time {
return now.Truncate(time.Minute)
}
func (now *Now) BeginningOfHour() time.Time {
return now.Truncate(time.Hour)
}
func (now *Now) BeginningOfDay() time.Time {
d := time.Duration(-now.Hour()) * time.Hour
return now.BeginningOfHour().Add(d)
}
func (now *Now) BeginningOfWeek() time.Time {
t := now.BeginningOfDay()
weekday := int(t.Weekday())
if FirstDayMonday {
if weekday == 0 {
weekday = 7
}
weekday = weekday - 1
}
d := time.Duration(-weekday) * 24 * time.Hour
return t.Add(d)
}
func (now *Now) BeginningOfMonth() time.Time {
t := now.BeginningOfDay()
d := time.Duration(-int(t.Day())+1) * 24 * time.Hour
return t.Add(d)
}
func (now *Now) BeginningOfQuarter() time.Time {
month := now.BeginningOfMonth()
offset := (int(month.Month()) - 1) % 3
return month.AddDate(0, -offset, 0)
}
func (now *Now) BeginningOfYear() time.Time {
t := now.BeginningOfDay()
d := time.Duration(-int(t.YearDay())+1) * 24 * time.Hour
return t.Truncate(time.Hour).Add(d)
}
func (now *Now) EndOfMinute() time.Time {
return now.BeginningOfMinute().Add(time.Minute - time.Nanosecond)
}
func (now *Now) EndOfHour() time.Time {
return now.BeginningOfHour().Add(time.Hour - time.Nanosecond)
}
func (now *Now) EndOfDay() time.Time {
return now.BeginningOfDay().Add(24*time.Hour - time.Nanosecond)
}
func (now *Now) EndOfWeek() time.Time {
return now.BeginningOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond)
}
func (now *Now) EndOfMonth() time.Time {
return now.BeginningOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
}
func (now *Now) EndOfQuarter() time.Time {
return now.BeginningOfQuarter().AddDate(0, 3, 0).Add(-time.Nanosecond)
}
func (now *Now) EndOfYear() time.Time {
return now.BeginningOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond)
}
func (now *Now) Monday() time.Time {
t := now.BeginningOfDay()
weekday := int(t.Weekday())
if weekday == 0 {
weekday = 7
}
d := time.Duration(-weekday+1) * 24 * time.Hour
return t.Truncate(time.Hour).Add(d)
}
func (now *Now) Sunday() time.Time {
t := now.BeginningOfDay()
weekday := int(t.Weekday())
if weekday == 0 {
return t
} else {
d := time.Duration(7-weekday) * 24 * time.Hour
return t.Truncate(time.Hour).Add(d)
}
}
func (now *Now) EndOfSunday() time.Time {
return now.Sunday().Add(24*time.Hour - time.Nanosecond)
}
func parseWithFormat(str string) (t time.Time, err error) {
for _, format := range TimeFormats {
t, err = time.Parse(format, str)
if err == nil {
return
}
}
err = errors.New("Can't parse string as time: " + str)
return
}
func (now *Now) Parse(strs ...string) (t time.Time, err error) {
var setCurrentTime bool
parseTime := []int{}
currentTime := []int{now.Second(), now.Minute(), now.Hour(), now.Day(), int(now.Month()), now.Year()}
currentLocation := now.Location()
for _, str := range strs {
onlyTime := regexp.MustCompile(`^\s*\d+(:\d+)*\s*$`).MatchString(str) // match 15:04:05, 15
t, err = parseWithFormat(str)
location := t.Location()
if location.String() == "UTC" {
location = currentLocation
}
if err == nil {
parseTime = []int{t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
onlyTime = onlyTime && (parseTime[3] == 1) && (parseTime[4] == 1)
for i, v := range parseTime {
// Don't reset hour, minute, second if it is a time only string
if onlyTime && i <= 2 {
continue
}
// Fill up missed information with current time
if v == 0 {
if setCurrentTime {
parseTime[i] = currentTime[i]
}
} else {
setCurrentTime = true
}
// Default day and month is 1, fill up it if missing it
if onlyTime {
if i == 3 || i == 4 {
parseTime[i] = currentTime[i]
continue
}
}
}
}
if len(parseTime) > 0 {
t = time.Date(parseTime[5], time.Month(parseTime[4]), parseTime[3], parseTime[2], parseTime[1], parseTime[0], 0, location)
currentTime = []int{t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
}
}
return
}
func (now *Now) MustParse(strs ...string) (t time.Time) {
t, err := now.Parse(strs...)
if err != nil {
panic(err)
}
return t
}
func (now *Now) Between(time1, time2 string) bool {
restime := now.MustParse(time1)
restime2 := now.MustParse(time2)
return now.After(restime) && now.Before(restime2)
}