-
Notifications
You must be signed in to change notification settings - Fork 0
/
date-utils.go
39 lines (33 loc) · 896 Bytes
/
date-utils.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
package main
import "time"
// DisplayDateFormat defines the format for date output.
const DisplayDateFormat string = "2006-01-02 15:04:05"
func beginningOfDay() time.Time {
t := time.Now()
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
func beginningOfWeek() time.Time {
t := time.Now()
weekday := time.Duration(t.Weekday())
year, month, day := t.Date()
currentZeroDay := time.Date(year, month, day, 0, 0, 0, 0, t.Location())
return currentZeroDay.Add(-1 * (weekday) * 24 * time.Hour)
}
func beginningOfMonth() time.Time {
t := time.Now()
year, month, _ := t.Date()
return time.Date(year, month, 1, 0, 0, 0, 0, t.Location())
}
func maximumTime(times ...time.Time) time.Time {
if len(times) <= 0 {
return time.Time{}
}
maxTime := times[0]
for _, t := range times {
if t.After(maxTime) {
maxTime = t
}
}
return maxTime
}