-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeUtil.go
72 lines (59 loc) · 2.15 KB
/
timeUtil.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
package serverutils
import "time"
const TIME_FORMAT_CLIENT string = "2006-01-02 03:04:05"
const TIME_FORMAT_LOG_FILE string = "20060102_030405"
const TIME_FORMAT_DATE string = "2006-01-02"
const TIME_FORMAT_TIME string = "030405"
type timeUtil struct {
}
//Return the time at 00:00:00 of the same day as givenTime
func (*timeUtil) GetTimeBeginOfDay(givenTime time.Time) time.Time {
year, month, day := givenTime.Date()
return time.Date(year, month, day, 0, 0, 0, 0, givenTime.Location())
}
//Return the time at 00:00:00 of the day before givenTime
func (*timeUtil) GetTimeBeginOfYesterday(givenTime time.Time) time.Time {
year, month, day := givenTime.Date()
return time.Date(year, month, day, 0, 0, 0, 0, givenTime.Location()).Add(time.Hour * 24 * -1)
}
func (u *timeUtil) GetSecondsSinceBeginOfDay(t time.Time) int64 {
bod := u.GetTimeBeginOfDay(t)
result := t.Sub(bod).Seconds()
return int64(result)
}
func (*timeUtil) GetTimeBeginOfNextDay(t time.Time) time.Time {
year, month, day := t.Date()
bod := time.Date(year, month, day, 0, 0, 0, 0, t.Location())
return bod.Add(time.Duration(24) * time.Hour)
}
func (*timeUtil) IsDifferentDay(t1 time.Time, t2 time.Time) bool {
day1 := time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, t1.Location())
day2 := time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, t2.Location())
return !day2.Equal(day1)
}
func (*timeUtil) CalculateDayPassed(from time.Time, to time.Time) int {
dayFrom := time.Date(from.Year(), from.Month(), from.Day(), 0, 0, 0, 0, from.Location())
dayTo := time.Date(to.Year(), to.Month(), to.Day(), 0, 0, 0, 0, to.Location())
return int(dayTo.Sub(dayFrom).Hours() / 24)
}
func (*timeUtil) FormatTimeLogTime(from time.Time) string {
return from.Format(TIME_FORMAT_LOG_FILE)
}
/*
Return formatted time for log file, YYYYMMDD_hhmmss
*/
func (*timeUtil) FormatTimeLogFile(from time.Time) string {
return from.Format(TIME_FORMAT_LOG_FILE)
}
/*
Return formatted date as YYYY-MM-DD
*/
func (*timeUtil) GetDate(from time.Time) string {
return from.Format(TIME_FORMAT_DATE)
}
/*
Return formatted date as YYYY-MM-DD
*/
func (*timeUtil) GetTime(from time.Time) string {
return from.Format(TIME_FORMAT_TIME)
}