-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdatetime.go
240 lines (210 loc) · 6.07 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package goutils
import (
"strings"
"time"
)
const (
DEFAULT_DATE = "0001-01-01"
LAYOUT_DDMMYYYY = "02012006"
LAYOUT_DD_MM_YYYY = "02/01/2006"
LAYOUT_DD_MM_YYYY_HH_MM_SS = "02/01/2006 15:04:05"
LAYOUT_YYYY_MM_DD_HH_MM_SS = "2006-01-02 15:04:05"
LAYOUT_MM_DD_YYYY_HH_MM = "01-02-2006 15:04"
LAYOUT_YYYYMMDD = "20060102"
LAYOUT_YYMMDDHHMMSS = "060102150405"
LAYOUT_YYYYMMDDHHMMSS = "20060102150405"
LAYOUT_YYYYMM = "200601"
LAYOUT_YYYY_MM_DD = "2006-01-02"
LAYOUT_YYMMDD = "060102"
LAYOUT_HHMMSS = "150405"
LAYOUT_HH = "15"
LAYOUT_HH_MM_SS = "15:04:05"
LAYOUT_YYYY_MM_DDTHH_MM_SS_000Z = "2006-01-02T15:04:05.000Z"
LAYOUT_YYYY_MM_DDTHH_MM_SS = "2006-01-02 15:04:05"
LAYOUT_YYYY_MM_DDTHH_MM_SS_000 = "2006-01-02T15:04:05.000"
)
/*
ConvertStringToTimeLayoutDDMMYYYY
*/
func ConvertStringToTimeLayoutDDMMYYYY(value string) time.Time {
t, _ := time.Parse(LAYOUT_DDMMYYYY, value)
return t
}
/*
ConvertTimeToStringLayoutDD_MM_YYYY
*/
func ConvertTimeToStringLayoutDD_MM_YYYY(date time.Time) string {
return date.Format(LAYOUT_DD_MM_YYYY)
}
func ConvertTimeToStringLayoutMM_DD_YYYY_HH_MM(date time.Time) string {
return date.Format(LAYOUT_MM_DD_YYYY_HH_MM)
}
/*
ConvertStringToTimeLayoutDD_MM_YYYY
*/
func ConvertStringToTimeLayoutDD_MM_YYYY(value string) time.Time {
t, _ := time.Parse(LAYOUT_DD_MM_YYYY, value)
return t
}
/*
ConvertStringToTimeLayoutDD_MM_YYYY_HH_MM_SS
*/
func ConvertStringToTimeLayoutDD_MM_YYYY_HH_MM_SS(value string) time.Time {
t, _ := time.Parse(LAYOUT_DD_MM_YYYY_HH_MM_SS, value)
return t
}
/*
ConvertStringDD_MM_YYYY
*/
func ConvertStringDD_MM_YYYY(date string) time.Time {
t, _ := time.Parse(LAYOUT_DD_MM_YYYY, date)
return t
}
/*
ConverTimeToStrinLayoutYYYY_MM_DD
*/
func ConverTimeToStrinLayoutYYYY_MM_DD(data time.Time) string {
data.Format(LAYOUT_YYYY_MM_DD)
return strings.Split(data.String(), " ")[0]
}
/*
ConvertStringToTimeLayoutYYYYMMDD
*/
func ConvertStringToTimeLayoutYYYYMMDD(value string) time.Time {
t, _ := time.Parse(LAYOUT_YYYYMMDD, value)
return t
}
/*
ConvertStringToTimeLayoutYYYY_MM_DD
*/
func ConvertStringToTimeLayoutYYYY_MM_DD(value string) time.Time {
t, _ := time.Parse(LAYOUT_YYYY_MM_DD, value)
return t
}
/*
ConverTimeToStrinLayoutYYYYMMDD
*/
func ConverTimeToStrinLayoutYYYYMMDD(data time.Time) string {
data.Format(LAYOUT_YYYYMMDD)
return strings.ReplaceAll(strings.Split(data.String(), " ")[0], "-", "")
}
/*
ConvertStringToTimeLayoutYYMMDDHHMMSS
*/
func ConvertStringToTimeLayoutYYMMDDHHMMSS(value string) time.Time {
t, _ := time.Parse(LAYOUT_YYMMDDHHMMSS, value)
return t
}
/*
ConvertStringToTimeLayoutYYYYMMDDHHMMSS
*/
func ConvertStringToTimeLayoutYYYYMMDDHHMMSS(value string) time.Time {
t, _ := time.Parse(LAYOUT_YYYYMMDDHHMMSS, value)
return t
}
/*
ConvertStringToTimeLayout_YYYY_MM_DD_HH_MM_SS o antigo nome era: ConvertStringToTimeLAYOUT_YYYY_MM_DD_HH_MM_SS
*/
func ConvertStringToTimeLayout_YYYY_MM_DD_HH_MM_SS(date time.Time) string {
return date.Format(LAYOUT_YYYY_MM_DD_HH_MM_SS)
}
func ConvertToTimeLayout_YYYY_MM_DD_HH_MM_SS(value string) time.Time {
t, _ := time.Parse(LAYOUT_YYYY_MM_DD_HH_MM_SS, value)
return t
}
/*
ConvertStringToTimeLayout_YYYY_MM_DD o antigo nome era: ConvertStringToTimeLAYOUT_YYYY_MM_DD
*/
func ConvertTimeToStringLayout_YYYY_MM_DD(date time.Time) string {
return date.Format(LAYOUT_YYYY_MM_DD)
}
/*
ConvertStringToTimeLayoutYYYY_MM_DDTHH_MM_SS_000Z
*/
func ConvertStringToTimeLayoutYYYY_MM_DDTHH_MM_SS_000Z(value string) time.Time {
t, _ := time.Parse(LAYOUT_YYYY_MM_DDTHH_MM_SS_000Z, value)
return t
}
/*
ConvertStringToTimeLayoutYYYY_MM_DDTHH_MM_SS_000
*/
func ConvertStringToTimeLayoutYYYY_MM_DDTHH_MM_SS_000(value string) time.Time {
t, _ := time.Parse(LAYOUT_YYYY_MM_DDTHH_MM_SS_000, value)
return t
}
/*
ConvertStringToTimeLayoutYYYY_MM_DDTHH_MM_SS
*/
func ConvertStringToTimeLayoutYYYY_MM_DDTHH_MM_SS(value string) time.Time {
t, _ := time.Parse(LAYOUT_YYYY_MM_DDTHH_MM_SS, value)
return t
}
/*
ConvertTimeToStringLayoutYYYY_MM_DDTHH_MM_SS_000
*/
func ConvertTimeToStringLayoutYYYY_MM_DDTHH_MM_SS_000(date time.Time) string {
return date.Format(LAYOUT_YYYY_MM_DDTHH_MM_SS_000)
}
/*
ConvertStringToTimeLayoutHHMMSS
*/
func ConvertStringToTimeLayoutHHMMSS(value string) time.Time {
t, _ := time.Parse(LAYOUT_HHMMSS, value)
return t
}
/*
ConvertStringToTimeLayoutHH_MM_SS
*/
func ConvertStringToTimeLayoutHH_MM_SS(value string) time.Time {
t, _ := time.Parse(LAYOUT_HH_MM_SS, value)
return t
}
/*
ConvertStringToTimeLayoutDDMMYYYYHHMMSS
*/
func ConvertStringToTimeLayoutDDMMYYYYHHMMSS(d time.Time, h time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), h.Hour(), h.Minute(), h.Second(), 0, time.UTC)
}
/*
RangeDate returns a date range function over start date to end date inclusive.
After the end of the range, the range function returns a zero date, date.IsZero() is true.
*/
func RangeDate(end, start time.Time) func() time.Time {
y, m, d := start.Date()
start = time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
y, m, d = end.Date()
end = time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
return func() time.Time {
if start.After(end) {
return time.Time{}
}
date := start
start = start.AddDate(0, 0, 1)
return date
}
}
/*
DatePlusTime
*/
func DatePlusTime(date, timeOfDay time.Time) (time.Time, error) {
return time.Parse("2006-01-02 15:04:05", date.Format("2006-01-02")+" "+timeOfDay.Format("15:04:05"))
}
/*
Qual é o dia da Segunda-feira da data que passou
*/
func WeekStartDate(date time.Time) time.Time {
if int(time.Sunday) == 0 { // Caso seja Domingo jogar para segunda
date = date.Add(time.Hour * 24)
}
offset := (int(time.Monday) - int(date.Weekday()) - 7) % 7
result := date.Add(time.Duration(offset*24) * time.Hour)
return result
}
/*
Qual é o dia da Sexta-feira da data que passou
*/
func WeekEndDate(date time.Time) time.Time {
offset := int(time.Friday) - int(date.Weekday())
result := date.Add(time.Duration(offset*24) * time.Hour)
return result
}