-
-
Notifications
You must be signed in to change notification settings - Fork 234
/
main.go
200 lines (164 loc) · 5.06 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Package now is a time toolkit for golang.
//
// More details README here: https://github.com/jinzhu/now
//
// import "github.com/jinzhu/now"
//
// now.BeginningOfMinute() // 2013-11-18 17:51:00 Mon
// now.BeginningOfDay() // 2013-11-18 00:00:00 Mon
// now.EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
package now
import "time"
// WeekStartDay set week start day, default is sunday
var WeekStartDay = time.Sunday
// TimeFormats default time formats will be parsed as
var TimeFormats = []string{
"2006", "2006-1", "2006-1-2", "2006-1-2 15", "2006-1-2 15:4", "2006-1-2 15:4:5", "1-2",
"15:4:5", "15:4", "15",
"15:4:5 Jan 2, 2006 MST", "2006-01-02 15:04:05.999999999 -0700 MST", "2006-01-02T15:04:05Z0700", "2006-01-02T15:04:05Z07",
"2006.1.2", "2006.1.2 15:04:05", "2006.01.02", "2006.01.02 15:04:05", "2006.01.02 15:04:05.999999999",
"1/2/2006", "1/2/2006 15:4:5", "2006/01/02", "20060102", "2006/01/02 15:04:05",
time.ANSIC, time.UnixDate, time.RubyDate, time.RFC822, time.RFC822Z, time.RFC850,
time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC3339Nano,
time.Kitchen, time.Stamp, time.StampMilli, time.StampMicro, time.StampNano,
}
// Config configuration for now package
type Config struct {
WeekStartDay time.Weekday
TimeLocation *time.Location
TimeFormats []string
}
// DefaultConfig default config
var DefaultConfig *Config
// New initialize Now based on configuration
func (config *Config) With(t time.Time) *Now {
return &Now{Time: t, Config: config}
}
// Parse parse string to time based on configuration
func (config *Config) Parse(strs ...string) (time.Time, error) {
if config.TimeLocation == nil {
return config.With(time.Now()).Parse(strs...)
} else {
return config.With(time.Now().In(config.TimeLocation)).Parse(strs...)
}
}
// MustParse must parse string to time or will panic
func (config *Config) MustParse(strs ...string) time.Time {
if config.TimeLocation == nil {
return config.With(time.Now()).MustParse(strs...)
} else {
return config.With(time.Now().In(config.TimeLocation)).MustParse(strs...)
}
}
// Now now struct
type Now struct {
time.Time
*Config
}
// With initialize Now with time
func With(t time.Time) *Now {
config := DefaultConfig
if config == nil {
config = &Config{
WeekStartDay: WeekStartDay,
TimeFormats: TimeFormats,
}
}
return &Now{Time: t, Config: config}
}
// New initialize Now with time
func New(t time.Time) *Now {
return With(t)
}
// BeginningOfMinute beginning of minute
func BeginningOfMinute() time.Time {
return With(time.Now()).BeginningOfMinute()
}
// BeginningOfHour beginning of hour
func BeginningOfHour() time.Time {
return With(time.Now()).BeginningOfHour()
}
// BeginningOfDay beginning of day
func BeginningOfDay() time.Time {
return With(time.Now()).BeginningOfDay()
}
// BeginningOfWeek beginning of week
func BeginningOfWeek() time.Time {
return With(time.Now()).BeginningOfWeek()
}
// BeginningOfMonth beginning of month
func BeginningOfMonth() time.Time {
return With(time.Now()).BeginningOfMonth()
}
// BeginningOfQuarter beginning of quarter
func BeginningOfQuarter() time.Time {
return With(time.Now()).BeginningOfQuarter()
}
// BeginningOfYear beginning of year
func BeginningOfYear() time.Time {
return With(time.Now()).BeginningOfYear()
}
// EndOfMinute end of minute
func EndOfMinute() time.Time {
return With(time.Now()).EndOfMinute()
}
// EndOfHour end of hour
func EndOfHour() time.Time {
return With(time.Now()).EndOfHour()
}
// EndOfDay end of day
func EndOfDay() time.Time {
return With(time.Now()).EndOfDay()
}
// EndOfWeek end of week
func EndOfWeek() time.Time {
return With(time.Now()).EndOfWeek()
}
// EndOfMonth end of month
func EndOfMonth() time.Time {
return With(time.Now()).EndOfMonth()
}
// EndOfQuarter end of quarter
func EndOfQuarter() time.Time {
return With(time.Now()).EndOfQuarter()
}
// EndOfYear end of year
func EndOfYear() time.Time {
return With(time.Now()).EndOfYear()
}
// Monday monday
func Monday(strs ...string) time.Time {
return With(time.Now()).Monday(strs...)
}
// Sunday sunday
func Sunday(strs ...string) time.Time {
return With(time.Now()).Sunday(strs...)
}
// EndOfSunday end of sunday
func EndOfSunday() time.Time {
return With(time.Now()).EndOfSunday()
}
// Quarter returns the yearly quarter
func Quarter() uint {
return With(time.Now()).Quarter()
}
// Parse parse string to time
func Parse(strs ...string) (time.Time, error) {
return With(time.Now()).Parse(strs...)
}
// ParseInLocation parse string to time in location
func ParseInLocation(loc *time.Location, strs ...string) (time.Time, error) {
return With(time.Now().In(loc)).Parse(strs...)
}
// MustParse must parse string to time or will panic
func MustParse(strs ...string) time.Time {
return With(time.Now()).MustParse(strs...)
}
// MustParseInLocation must parse string to time in location or will panic
func MustParseInLocation(loc *time.Location, strs ...string) time.Time {
return With(time.Now().In(loc)).MustParse(strs...)
}
// Between check now between the begin, end time or not
func Between(time1, time2 string) bool {
return With(time.Now()).Between(time1, time2)
}