-
Notifications
You must be signed in to change notification settings - Fork 6
/
datetime.go
681 lines (603 loc) · 17.6 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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
package gostradamus
import (
"fmt"
"time"
)
// WeekInDays is the integer value how many days a week has
const WeekInDays = 7
// DateTime reflects time.Time and adds functionality of this library
type DateTime time.Time
// DateTimeFromTime returns a DateTime with given time.Time
func DateTimeFromTime(time time.Time) DateTime {
return DateTime(time)
}
// NewDateTime returns a new DateTime in the timezone given
func NewDateTime(
year int,
month int,
day int,
hour int,
minute int,
second int,
nanosecond int,
timezone Timezone,
) DateTime {
return DateTimeFromTime(
time.Date(
year,
time.Month(month),
day,
hour,
minute,
second,
nanosecond,
timezone.Location(),
),
)
}
// NewUTCDateTime returns a new DateTime with timezone in UTC
func NewUTCDateTime(
year int,
month int,
day int,
hour int,
minute int,
second int,
nanosecond int,
) DateTime {
return NewDateTime(
year,
month,
day,
hour,
minute,
second,
nanosecond,
UTC,
)
}
// NewLocalDateTime creates a new DateTime in Local (system) timezone
func NewLocalDateTime(
year int,
month int,
day int,
hour int,
minute int,
second int,
nanosecond int,
) DateTime {
return NewDateTime(
year,
month,
day,
hour,
minute,
second,
nanosecond,
Local(),
)
}
// FromUnixTimestamp gets the DateTime from given unix timestamp.
// The returned Datetime has the UTC timezone
func FromUnixTimestamp(timestamp int64) DateTime {
return DateTime(time.Unix(timestamp, 0).UTC())
}
// Now returns the current local DateTime
func Now() DateTime {
return DateTimeFromTime(time.Now())
}
// UTCNow returns the current DateTime in UTC timezone
func UTCNow() DateTime {
return Now().InTimezone(UTC)
}
// NowInTimezone returns the current DateTime in given timezone
func NowInTimezone(timezone Timezone) DateTime {
return Now().InTimezone(timezone)
}
// Year of current DateTime as int
func (dt DateTime) Year() int {
return dt.Time().Year()
}
// Month of current DateTime as int
func (dt DateTime) Month() int {
return int(dt.Time().Month())
}
// Day of current DateTime as int
func (dt DateTime) Day() int {
return dt.Time().Day()
}
// Hour of current DateTime as int
func (dt DateTime) Hour() int {
return dt.Time().Hour()
}
// Minute of current DateTime as int
func (dt DateTime) Minute() int {
return dt.Time().Minute()
}
// Second of current DateTime as int
func (dt DateTime) Second() int {
return dt.Time().Second()
}
// Nanosecond of current DateTime as int
func (dt DateTime) Nanosecond() int {
return dt.Time().Nanosecond()
}
// Time returns the underlying time.Time of DateTime
func (dt DateTime) Time() time.Time {
return time.Time(dt)
}
// IsoFormat the current DateTime into ISO-8601 standard
//
// For Example:
//
// 2017-07-14T02:40:00.000000
//
func (dt DateTime) IsoFormat() string {
return dt.Format(Iso8601)
}
// IsoFormatTZ the current DateTime into ISO-8601 standard and current timezone info
//
// For Example:
//
// 2017-07-14T02:40:00.000000+0200
//
func (dt DateTime) IsoFormatTZ() string {
return dt.Format(Iso8601TZ)
}
// CTimeFormat the current DateTime to ctime standard
//
// Example: Sat Feb 15 12:12:12 2020
//
func (dt DateTime) CTimeFormat() string {
return dt.Format(CTime)
}
// String of the DateTime
// it will use the IsoFormat
// It defines the ``native'' format for that value.
// The String method is used to print values passed as an operand
// to any format that accepts a string or to an unformatted printer
// such as Print.
func (dt DateTime) String() string {
return dt.IsoFormatTZ()
}
// GoString of the DateTime
// it will use the IsoFormat
// The GoString method is used to print values passed as an operand to a %#v format.
func (dt DateTime) GoString() string {
return dt.IsoFormatTZ()
}
// Copy the current DateTime to a new DateTime
func (dt DateTime) Copy() DateTime {
return NewDateTime(
dt.Year(),
dt.Month(),
dt.Day(),
dt.Hour(),
dt.Minute(),
dt.Second(),
dt.Nanosecond(),
dt.Timezone(),
)
}
// Format the current DateTime with given format to a string
func (dt DateTime) Format(format string) string {
return formatFromTime(dt.Time(), format)
}
// Parse a string value with given format into a new DateTime
func Parse(value string, format string) (DateTime, error) {
parsedTime, err := parseToTime(value, format, UTC)
return DateTimeFromTime(parsedTime), err
}
// ParseInTimezone a string value with given format into a new DateTime in given timezone
func ParseInTimezone(value string, format string, timezone Timezone) (DateTime, error) {
parsedTime, err := parseToTime(value, format, timezone)
return DateTimeFromTime(parsedTime), err
}
// InTimezone sets the current DateTime in the given Timezone and returns a new DateTime
func (dt DateTime) InTimezone(timezone Timezone) DateTime {
return DateTimeFromTime(dt.Time().In(timezone.Location()))
}
// IsoCalendar returns three int values with (year, month, day)
func (dt DateTime) IsoCalendar() (int, int, int) {
return dt.Year(), dt.Month(), dt.Day()
}
// Timezone returns the Timezone of current DateTime object
func (dt DateTime) Timezone() Timezone {
return Timezone(dt.Time().Location().String())
}
// UnixTimestamp returns the unix timestamp as int64
func (dt DateTime) UnixTimestamp() int64 {
return dt.Time().Unix()
}
// ShiftYears adds or subtracts years in current DateTime and returns a new DateTime
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftYears(years int) DateTime {
return DateTimeFromTime(dt.Time().AddDate(years, 0, 0))
}
// ShiftMonths adds or subtracts months
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftMonths(months int) DateTime {
return DateTimeFromTime(dt.Time().AddDate(0, months, 0))
}
// ShiftWeeks add or subtracts weeks
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftWeeks(weeks int) DateTime {
return DateTimeFromTime(dt.Time().AddDate(0, 0, weeks*WeekInDays))
}
// ShiftDays adds or subtracts days
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftDays(days int) DateTime {
return DateTimeFromTime(dt.Time().AddDate(0, 0, days))
}
// ShiftHours adds or subtracts hours
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftHours(hours int) DateTime {
duration, _ := time.ParseDuration(fmt.Sprintf("%dh", hours))
return DateTime(dt.Time().Add(duration))
}
// ShiftMinutes adds or subtracts minutes
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftMinutes(minutes int) DateTime {
duration, _ := time.ParseDuration(fmt.Sprintf("%dm", minutes))
return DateTime(dt.Time().Add(duration))
}
// ShiftSeconds adds or subtracts seconds
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftSeconds(second int) DateTime {
duration, _ := time.ParseDuration(fmt.Sprintf("%ds", second))
return DateTime(dt.Time().Add(duration))
}
// ShiftMilliSeconds adds or subtracts milliseconds
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftMilliSeconds(millisecond int) DateTime {
duration, _ := time.ParseDuration(fmt.Sprintf("%dms", millisecond))
return DateTime(dt.Time().Add(duration))
}
// ShiftMicroSeconds adds or subtracts microseconds
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftMicroSeconds(microsecond int) DateTime {
duration, _ := time.ParseDuration(fmt.Sprintf("%dus", microsecond))
return DateTime(dt.Time().Add(duration))
}
// ShiftNanoseconds adds or subtracts nanoseconds in current DateTime and returns a new DateTime
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) ShiftNanoseconds(nanosecond int) DateTime {
duration, _ := time.ParseDuration(fmt.Sprintf("%dns", nanosecond))
return DateTime(dt.Time().Add(duration))
}
// Shift adds or subtracts years, months, days, hours, minutes, seconds, and nanoseconds of current DateTime and returns a new DateTime
// Add is a positive integer
// Subtract is a negative integer
func (dt DateTime) Shift(years int, months int, days int, hours int, minutes int, seconds int, nanoseconds int) DateTime {
return dt.ShiftYears(years).
ShiftMonths(months).
ShiftDays(days).
ShiftHours(hours).
ShiftMinutes(minutes).
ShiftSeconds(seconds).
ShiftNanoseconds(nanoseconds)
}
// FloorYear returns a DateTime with all values to "floor" except year
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-01-01 00:00:00.00000000
//
func (dt DateTime) FloorYear() DateTime {
return dt.Replace(dt.Year(), 1, 1, 0, 0, 0, 0)
}
// FloorMonth returns a DateTime with all values to "floor" except year, month
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-01 00:00:00.00000000
//
func (dt DateTime) FloorMonth() DateTime {
return dt.Replace(dt.Year(), dt.Month(), 1, 0, 0, 0, 0)
}
// FloorWeek returns a DateTime with all values to "floor" of current DateTime's week except year, month
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 (wednesday) becomes 2012-12-10 00:00:00.000000000 (monday)
//
func (dt DateTime) FloorWeek() DateTime {
deltaWeekdays := int(dt.WeekDay() - time.Monday)
// currentWeekDay is already monday
if deltaWeekdays == 0 {
return dt.FloorDay()
}
if deltaWeekdays == -1 {
return dt.ShiftDays(-6).FloorDay()
}
return dt.ShiftDays(-deltaWeekdays).FloorDay()
}
// FloorDay returns a DateTime with all values to "floor" except year, month, day
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-12 00:00:00.00000000
//
func (dt DateTime) FloorDay() DateTime {
return dt.Replace(dt.Year(), dt.Month(), dt.Day(), 0, 0, 0, 0)
}
// FloorHour returns a DateTime with all values to "floor" except year, month, day, hour
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-12 12:00:00.00000000
//
func (dt DateTime) FloorHour() DateTime {
return dt.Replace(dt.Year(), dt.Month(), dt.Day(), dt.Hour(), 0, 0, 0)
}
// FloorMinute returns a DateTime with all values to "floor" except year, month, day, hour, minute
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-12 12:12:00.00000000
//
func (dt DateTime) FloorMinute() DateTime {
return dt.Replace(dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), 0, 0)
}
// FloorSecond returns a DateTime with all values to "floor" except year, month, day, hour, minute, second
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-31 12:12:12.00000000
//
func (dt DateTime) FloorSecond() DateTime {
return dt.Replace(dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second(), 0)
}
// CeilYear returns a DateTime with all values to "ceil" except year
//
// For Example:
//
// 2012-05-12 12:12:12.123456789 becomes 2012-12-31 23:59:59.999999999
//
func (dt DateTime) CeilYear() DateTime {
return dt.Replace(dt.Year(), 12, 31, 23, 59, 59, 999999999)
}
// CeilMonth returns a DateTime with all values to "ceil" except year, month
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-31 23:59:59.999999999
//
func (dt DateTime) CeilMonth() DateTime {
tempDateTime := dt.Copy()
tempDateTime = tempDateTime.ShiftMonths(1)
tempDateTime = tempDateTime.ReplaceDay(1)
tempDateTime = tempDateTime.ShiftDays(-1)
return dt.Replace(dt.Year(), dt.Month(), tempDateTime.Day(), 23, 59, 59, 999999999)
}
// CeilWeek returns a DateTime with all values to "ceil" of current DateTime's week except year, month
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 (wednesday) becomes 2012-12-16 23:59:59:123456789 (sunday)
//
func (dt DateTime) CeilWeek() DateTime {
currentWeekDay := int(dt.WeekDay())
if currentWeekDay == 0 {
return dt.CeilDay()
}
return dt.ShiftDays(WeekInDays - currentWeekDay).CeilDay()
}
// CeilDay returns a DateTime with all values to "ceil" except year, month, day, minute, second
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-12 23:59:59.999999999
//
func (dt DateTime) CeilDay() DateTime {
return dt.Replace(dt.Year(), dt.Month(), dt.Day(), 23, 59, 59, 999999999)
}
// CeilHour returns a DateTime with all values to "ceil" except year, month, day, hour
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-12 12:59:59.999999999
//
func (dt DateTime) CeilHour() DateTime {
return dt.Replace(dt.Year(), dt.Month(), dt.Day(), dt.Hour(), 59, 59, 999999999)
}
// CeilMinute returns a DateTime with all values to "ceil" except year, month, day, hour, minute
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-12 12:12:59.999999999
//
func (dt DateTime) CeilMinute() DateTime {
return dt.Replace(dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), 59, 999999999)
}
// CeilSecond returns a DateTime with all values to "ceil" except year, month, day, hour, minute, second
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes 2012-12-12 12:12:12.999999999
//
func (dt DateTime) CeilSecond() DateTime {
return dt.Replace(dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second(), 999999999)
}
// SpanYear returns the start and end DateTime of current year span
//
// For Example:
//
// 2012-05-12 12:12:12:123456789 becomes (2012-01-01 00:00:00.000000000, 2012-12-31 23:59:59.999999999)
//
func (dt DateTime) SpanYear() (DateTime, DateTime) {
return dt.FloorYear(), dt.CeilYear()
}
// SpanMonth returns the start and end DateTime of current month span
//
// For Example:
//
// 2012-12-12 12:12:12:123456789 becomes (2012-12-01 00:00:00.000000000, 2012-12-31 23:59:59.999999999)
//
func (dt DateTime) SpanMonth() (DateTime, DateTime) {
return dt.FloorMonth(), dt.CeilMonth()
}
// SpanWeek returns the start and end DateTime of current week span
//
// For Example:
//
// 2012-12-12 12:12:12.123456789 becomes (2012-12-10 00:00:00.00000000, 2012-12-16 23:59:59.999999999)
//
func (dt DateTime) SpanWeek() (DateTime, DateTime) {
return dt.FloorWeek(), dt.CeilWeek()
}
// SpanDay returns the start and end DateTime of current day span
//
// For Example:
//
// 2012-12-12 12:12:12:123456789 becomes (2012-12-12 00:00:00.000000000, 2012-12-12 23:59:59.999999999)
//
func (dt DateTime) SpanDay() (DateTime, DateTime) {
return dt.FloorDay(), dt.CeilDay()
}
// SpanHour returns the start and end DateTime of current hour span
//
// For Example:
//
// 2012-12-12 12:12:12:123456789 becomes (2012-12-12 12:00:00.000000000, 2012-12-12 12:59:59.999999999)
//
func (dt DateTime) SpanHour() (DateTime, DateTime) {
return dt.FloorHour(), dt.CeilHour()
}
// SpanMinute returns the start and end DateTime of current minute span
//
// For Example:
//
// 2012-12-12 12:12:12:123456789 becomes (2012-12-12 12:12:00.000000000, 2012-12-12 12:12:59.999999999)
//
func (dt DateTime) SpanMinute() (DateTime, DateTime) {
return dt.FloorMinute(), dt.CeilMinute()
}
// SpanSecond returns the start and end DateTime of current second span
//
// For Example:
//
// 2012-12-12 12:12:12:123456789 becomes (2012-12-12 12:12:12.000000000, 2012-12-12 12:12:12.999999999)
//
func (dt DateTime) SpanSecond() (DateTime, DateTime) {
return dt.FloorSecond(), dt.CeilSecond()
}
// WeekDay returns time.Weekday
func (dt DateTime) WeekDay() time.Weekday {
return dt.Time().Weekday()
}
// ReplaceYear will set the year of current DateTime and returns a new DateTime
func (dt DateTime) ReplaceYear(year int) DateTime {
return NewDateTime(
year,
dt.Month(),
dt.Day(),
dt.Hour(),
dt.Minute(),
dt.Second(),
dt.Nanosecond(),
dt.Timezone(),
)
}
// ReplaceMonth will set the month of current DateTime and returns a new DateTime
func (dt DateTime) ReplaceMonth(month int) DateTime {
return NewDateTime(
dt.Year(),
month,
dt.Day(),
dt.Hour(),
dt.Minute(),
dt.Second(),
dt.Nanosecond(),
dt.Timezone(),
)
}
// ReplaceDay will set the day of current DateTime and returns a new DateTime
func (dt DateTime) ReplaceDay(day int) DateTime {
return NewDateTime(
dt.Year(),
dt.Month(),
day,
dt.Hour(),
dt.Minute(),
dt.Second(),
dt.Nanosecond(),
dt.Timezone(),
)
}
// ReplaceHour will set the hour of current DateTime and returns a new DateTime
func (dt DateTime) ReplaceHour(hour int) DateTime {
return NewDateTime(
dt.Year(),
dt.Month(),
dt.Day(),
hour,
dt.Minute(),
dt.Second(),
dt.Nanosecond(),
dt.Timezone(),
)
}
// ReplaceMinute will set the minute of current DateTime and returns a new DateTime
func (dt DateTime) ReplaceMinute(minute int) DateTime {
return NewDateTime(
dt.Year(),
dt.Month(),
dt.Day(),
dt.Hour(),
minute,
dt.Second(),
dt.Nanosecond(),
dt.Timezone(),
)
}
// ReplaceSecond will set the second of current DateTime and returns a new DateTime
func (dt DateTime) ReplaceSecond(second int) DateTime {
return NewDateTime(
dt.Year(),
dt.Month(),
dt.Day(),
dt.Hour(),
dt.Minute(),
second,
dt.Nanosecond(),
dt.Timezone(),
)
}
// ReplaceNanosecond will set the nanosecond of current DateTime and returns a new DateTime
func (dt DateTime) ReplaceNanosecond(nanosecond int) DateTime {
return NewDateTime(
dt.Year(),
dt.Month(),
dt.Day(),
dt.Hour(),
dt.Minute(),
dt.Second(),
nanosecond,
dt.Timezone(),
)
}
// Replace will set the year, month, day, hour, minute, second, and nanosecond of current DateTime and returns a new DateTime
func (dt DateTime) Replace(year int, month int, day int, hour int, minute int, second int, nanosecond int) DateTime {
return dt.ReplaceYear(year).
ReplaceMonth(month).
ReplaceDay(day).
ReplaceHour(hour).
ReplaceMinute(minute).
ReplaceSecond(second).
ReplaceNanosecond(nanosecond)
}
// IsBetween checks if current DateTime is between start and end DateTimes
func (dt DateTime) IsBetween(start DateTime, end DateTime) bool {
return dt.Time().After(start.Time()) && dt.Time().Before(end.Time())
}