/
helper-time.go
640 lines (502 loc) · 13.9 KB
/
helper-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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package helper
/*
* Copyright 2020 Aldelo, LP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"strings"
"time"
"fmt"
)
// FormatDate will format the input date value to yyyy-mm-dd
func FormatDate(t time.Time) string {
return t.Format("2006-01-02")
}
// FormatTime will format the input date value to hh:mm:ss tt
func FormatTime(t time.Time) string {
return t.Format("03:04:05 PM")
}
// FormatDateTime will format the input date value to yyyy-mm-dd hh:mm:ss tt
func FormatDateTime(t time.Time) string {
return t.Format("2006-01-02 03:04:05 PM")
}
// DateFormatString returns the date format string constant (yyyy-mm-dd)
func DateFormatString() string {
return "2006-01-02"
}
// TimeFormatString returns the time format string constant (hh:mm:ss tt)
func TimeFormatString() string {
return "03:04:05 PM"
}
// DateTimeFormatString returns the date time format string constant (yyyy-mm-dd hh:mm:ss tt)
func DateTimeFormatString() string {
return "2006-01-02 03:04:05 PM"
}
// ParseDate will parse a date value in yyyy-mm-dd format into time.Time object,
// check time.IsZero() to verify if a zero time is returned indicating parser failure
func ParseDate(s string) time.Time {
t, err := time.Parse("2006-01-02", strings.TrimSpace(s))
if err != nil {
return time.Time{}
}
return t
}
// ParseTime will parse a time vaule in hh:mm:ss tt format into time.Time object,
// check time.IsZero() to verify if a zero time is returned indicating parser failure
func ParseTime(s string) time.Time {
t, err := time.Parse("03:04:05 PM", strings.TrimSpace(s))
if err != nil {
return time.Time{}
}
return t
}
// ParseDateTime will parse a date time value in yyyy-mm-dd hh:mm:ss tt format into time.Time object,
// check time.IsZero() to verify if a zero time is returned indicating parser failure
func ParseDateTime(s string) time.Time {
t, err := time.Parse("2006-01-02 03:04:05 PM", strings.TrimSpace(s))
if err != nil {
return time.Time{}
}
return t
}
// ParseFromExcelDate will handle integer value of excel date to convert to time.Time
func ParseFromExcelDate(s string, format string) time.Time {
i, _ := ParseInt32(s)
if i > 0 {
v := GetDate(1970, 1, 1)
return v.AddDate(0, 0, i-70*365-19)
} else {
return ParseDateTimeCustom(s, format)
}
}
// ParseDateTime24Hr will parse a date time value in yyyy-mm-dd HH:mm:ss format into time.Time object,
// check time.IsZero() to verify if a zero time is returned indicating parser failure
func ParseDateTime24Hr(s string) time.Time {
t, err := time.Parse("2006-01-02 15:04:05", strings.TrimSpace(s))
if err != nil {
return time.Time{}
}
return t
}
// ParseDateTimeCustom will parse a date time value in s string, based on the f format
// f format is 2006 01 02 15:04:05 / 03:04:05 PM
func ParseDateTimeCustom(s string, f string) time.Time {
t, err := time.Parse(f, strings.TrimSpace(s))
if err != nil {
return time.Time{}
}
return t
}
// ParseDateTimeFromYYYYMMDDhhmmss from string value
func ParseDateTimeFromYYYYMMDDhhmmss(s string) time.Time {
s = strings.TrimSpace(s)
if IsNumericIntOnly(s) == false {
return time.Time{}
}
if LenTrim(s) != 14 {
return time.Time{}
}
d := Left(s, 4) + "-" + Mid(s, 4, 2) + "-" + Mid(s, 6, 2)
t := Mid(s, 8, 2) + ":" + Mid(s, 10, 2) + ":" + Mid(s, 12, 2)
dv := d + " " + t
return ParseDateTime24Hr(dv)
}
// ParseDateTimeFromMMDDYYYYhhmmss from string value
func ParseDateTimeFromMMDDYYYYhhmmss(s string) time.Time {
s = strings.TrimSpace(s)
if IsNumericIntOnly(s) == false {
return time.Time{}
}
if LenTrim(s) != 14 {
return time.Time{}
}
d := Mid(s, 4, 4) + "-" + Left(s, 2) + "-" + Mid(s, 2, 2)
t := Mid(s, 8, 2) + ":" + Mid(s, 10, 2) + ":" + Mid(s, 12, 2)
dv := d + " " + t
return ParseDateTime24Hr(dv)
}
// ParseDateFromYYYYMMDD from string value
func ParseDateFromYYYYMMDD(s string) time.Time {
s = strings.TrimSpace(s)
if IsNumericIntOnly(s) == false {
return time.Time{}
}
if LenTrim(s) != 8 {
return time.Time{}
}
d := Left(s, 4) + "-" + Mid(s, 4, 2) + "-" + Mid(s, 6, 2)
return ParseDate(d)
}
// ParseDateFromDDMMYYYY from string value
func ParseDateFromDDMMYYYY(s string) time.Time {
s = strings.TrimSpace(s)
if IsNumericIntOnly(s) == false {
return time.Time{}
}
if LenTrim(s) != 8 {
return time.Time{}
}
d := Right(s, 4) + "-" + Mid(s, 2, 2) + "-" + Left(s, 2)
return ParseDate(d)
}
// ParseDateFromYYMMDD from string value
func ParseDateFromYYMMDD(s string) time.Time {
s = strings.TrimSpace(s)
if IsNumericIntOnly(s) == false {
return time.Time{}
}
if LenTrim(s) != 6 {
return time.Time{}
}
d := Left(s, 2) + "-" + Mid(s, 2, 2) + "-" + Mid(s, 4, 2)
return ParseDateTimeCustom(d, "06-01-02")
}
// ParseDateFromYYMM from string value
func ParseDateFromYYMM(s string) time.Time {
s = strings.TrimSpace(s)
if IsNumericIntOnly(s) == false {
return time.Time{}
}
if LenTrim(s) != 4 {
return time.Time{}
}
d := Left(s, 2) + "-" + Mid(s, 2, 2)
return ParseDateTimeCustom(d, "06-01")
}
// ParseDateFromMMYY from string value
func ParseDateFromMMYY(s string) time.Time {
s = strings.TrimSpace(s)
if IsNumericIntOnly(s) == false {
return time.Time{}
}
if LenTrim(s) != 4 {
return time.Time{}
}
d := Left(s, 2) + "-" + Mid(s, 2, 2)
return ParseDateTimeCustom(d, "01-06")
}
// ParseDateToLastDayOfMonth
func ParseDateToLastDayOfMonth(t time.Time) time.Time {
if t.IsZero() {
return t
}
newDate := t.AddDate(0, 1, 0)
y, m, _ := newDate.Date()
newDate = ParseDateFromYYYYMMDD(Padding(Itoa(y), 4, false, "0") + Padding(Itoa(int(m)), 2, false, "0") + "01")
newDate = newDate.AddDate(0, 0, -1)
return newDate
}
// ParseDateFromMMDD from string value
func ParseDateFromMMDD(s string) time.Time {
s = strings.TrimSpace(s)
if IsNumericIntOnly(s) == false {
return time.Time{}
}
if LenTrim(s) != 4 {
return time.Time{}
}
d := Left(s, 2) + "-" + Mid(s, 2, 2)
return ParseDateTimeCustom(d, "01-02")
}
// CurrentDate returns current date in yyyy-mm-dd format
func CurrentDate() string {
return time.Now().Format("2006-01-02")
}
// CurrentDateStruct returns current date in yyyy-mm-dd format via time.Time struct
func CurrentDateStruct() time.Time {
return ParseDate(CurrentDate())
}
// CurrentDateTime returns current date and time in yyyy-mm-dd hh:mm:ss tt format
func CurrentDateTime() string {
return time.Now().Format("2006-01-02 03:04:05 PM")
}
// CurrentDateTimeStruct returns current date and time in yyyy-mm-dd hh:mm:ss tt format via time.Time struct
func CurrentDateTimeStruct() time.Time {
return ParseDateTime(CurrentDateTime())
}
// CurrentTime returns current time in hh:mm:ss tt format
func CurrentTime() string {
s := time.Now().Format("2006-01-02 03:04:05 PM")
s = s[11:]
return s
}
// DaysDiff gets the days difference between from and to date
func DaysDiff(timeFrom time.Time, timeTo time.Time) int {
d := timeTo.Sub(timeFrom)
dv := d.Hours() / 24.0
days := int(dv)
return days
}
// HoursDiff gets the hours difference between from and to date
func HoursDiff(timeFrom time.Time, timeTo time.Time) int {
d := timeTo.Sub(timeFrom)
dv := d.Hours()
hr := int(dv)
return hr
}
// MinutesDiff gets the minutes difference between from and to date
func MinutesDiff(timeFrom time.Time, timeTo time.Time) int {
d := timeTo.Sub(timeFrom)
dv := d.Minutes()
mn := int(dv)
return mn
}
// SecondsDiff gets the seconds difference between from and to date
func SecondsDiff(timeFrom time.Time, timeTo time.Time) int {
d := timeTo.Sub(timeFrom)
dv := d.Seconds()
s := int(dv)
return s
}
// DateBefore checks if testDate is before the beforeDate
func DateBefore(testDate time.Time, beforeDate time.Time) bool {
if testDate.Before(beforeDate) {
return true
}
return false
}
// DateBeforeOrEqual checks if testDate is before or equal to the beforeEqualDate
func DateBeforeOrEqual(testDate time.Time, beforeEqualDate time.Time) bool {
if testDate.Equal(beforeEqualDate) {
return true
}
if testDate.Before(beforeEqualDate) {
return true
}
return false
}
// DateAfter checks if testDate is after the afterDate
func DateAfter(testDate time.Time, afterDate time.Time) bool {
if testDate.After(afterDate) {
return true
}
return false
}
// DateAfterOrEqual checks if testDate is after or equal to the afterEqualDate
func DateAfterOrEqual(testDate time.Time, afterEqualDate time.Time) bool {
if testDate.Equal(afterEqualDate) {
return true
}
if testDate.After(afterEqualDate) {
return true
}
return false
}
// DateBetween checks if testDate is within the fromDate and toDate,
// if doNotIncludeEqual = true, then testDate equals fromDate and toDate are skipped
func DateBetween(testDate time.Time, fromDate time.Time, toDate time.Time, doNotIncludeEqual bool) bool {
if doNotIncludeEqual == false {
if testDate.Equal(fromDate) {
return true
}
if testDate.Equal(toDate) {
return true
}
}
if testDate.After(fromDate) {
return true
}
if testDate.Before(toDate) {
return true
}
return false
}
// DateOutside checks if the testDate is outside of the fromDate and toDate
func DateOutside(testDate time.Time, fromDate time.Time, toDate time.Time) bool {
if testDate.Before(fromDate) {
return true
}
if testDate.After(toDate) {
return true
}
return false
}
// DateEqual checks if the testDate equals to the equalDate
func DateEqual(testDate time.Time, equalDate time.Time) bool {
if testDate.Equal(equalDate) {
return true
}
return false
}
// DateToUTC converts given time to utc
func DateToUTC(t time.Time) (time.Time, error) {
loc, err := time.LoadLocation("UTC")
if err != nil {
return time.Time{}, err
}
if loc == nil {
return time.Time{}, fmt.Errorf("DateToUTC Location Target is Not Retrieved")
}
return t.In(loc), nil
}
// DateToUTC2 returns utc value directly without error info
func DateToUTC2(t time.Time) time.Time {
v, _ := DateToUTC(t)
return v
}
// DateToLocal converts given time to local time
func DateToLocal(t time.Time) (time.Time, error) {
loc, err := time.LoadLocation("Local")
if err != nil {
return time.Time{}, err
}
if loc == nil {
return time.Time{}, fmt.Errorf("DateToLocal Location Targe is Not Retrieved")
}
return t.In(loc), nil
}
// DateToLocal2 returns local value directly without error info
func DateToLocal2(t time.Time) time.Time {
v, _ := DateToLocal(t)
return v
}
// IsLeapYear checks if the year input is leap year or not
func IsLeapYear(year int) bool {
if year % 100 == 0 {
// is century year, divisible by 400 is leap year
if year % 400 == 0 {
return true
} else {
return false
}
} else {
// not a century year, divisible by 4 is leap year
if year % 4 == 0 {
return true
} else {
return false
}
}
}
// IsDayOfMonthValid checks if the month day number is valid
func IsDayOfMonthValid(year int, month int, day int) bool {
switch month {
case 1:
fallthrough
case 3:
fallthrough
case 5:
fallthrough
case 7:
fallthrough
case 8:
fallthrough
case 10:
fallthrough
case 12:
if day < 1 || day > 31 {
return false
} else {
return true
}
case 4:
fallthrough
case 6:
fallthrough
case 9:
fallthrough
case 11:
if day < 1 || day > 30 {
return false
} else {
return true
}
case 2:
d := 28
if IsLeapYear(year) {
d = 29
}
if day < 1 || day > d {
return false
} else {
return true
}
default:
return false
}
}
// FormatDateTimeToYYYYMMDDhhmmss for the date time struct received
func FormatDateTimeToYYYYMMDDhhmmss(t time.Time) string {
return t.Format("20060102150405")
}
// FormatDateTimeToMMDDYYYYhhmmss for the date time struct received
func FormatDateTimeToMMDDYYYYhhmmss(t time.Time) string {
return t.Format("01022006150405")
}
// FormatDateToYYYYMMDD for the date time struct received
func FormatDateToYYYYMMDD(t time.Time) string {
return t.Format("20060102")
}
// FormatDateToDDMMYYYY for the date time struct received
func FormatDateToDDMMYYYY(t time.Time) string {
return t.Format("02012006")
}
// FormatDateToYYMMDD for the date time struct received
func FormatDateToYYMMDD(t time.Time) string {
return t.Format("060102")
}
// FormatDateToYYMM for the date time struct received
func FormatDateToYYMM(t time.Time) string {
return t.Format("0601")
}
// FormatDateToMMYY for the date time struct received
func FormatDateToMMYY(t time.Time) string {
return t.Format("0106")
}
// FormatDateToMMDD for the date time struct received
func FormatDateToMMDD(t time.Time) string {
return t.Format("0102")
}
// GetDate returns date based on given year month day,
// month max day is checked,
// leap year is checked
func GetDate(year int, month int, day int) time.Time {
if year < 1970 || year > 2199 {
return time.Time{}
}
if month < 1 || month > 12 {
return time.Time{}
}
if day < 1 || day > 31 {
return time.Time{}
}
x := []int{4, 6, 9, 11}
if IntSliceContains(&x, month) {
// 30
if day == 31 {
return time.Time{}
}
} else if month == 2 {
// either 28 or 29
ly := 28
if IsLeapYear(year) {
ly = 29
}
if day > ly {
return time.Time{}
}
}
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
}
// GetFirstDateOfMonth returns the given date's first date of month,
// for example, 8/21/2020 => 8/1/2020
func GetFirstDateOfMonth(t time.Time) time.Time {
return GetDate(t.Year(), int(t.Month()), 1)
}
// GetLastDateOfMonth returns the given date's last day of the month,
// for example, 8/21/2020 => 8/31/2020
func GetLastDateOfMonth(t time.Time) time.Time {
x := GetFirstDateOfMonth(t).AddDate(0, 1, 0)
return GetFirstDateOfMonth(x).AddDate(0, 0, -1)
}