-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
152 lines (125 loc) · 4.71 KB
/
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
// Package simpletime is an extension of the go standard library time
// package which provides a number of convenience functions for retrieving
// new times based on the provided reference time
package simpletime
import (
"time"
)
// SimpleTime struct embeds time.Time
type SimpleTime struct {
time.Time
}
// NewSimpleTime returns a pointer to a SimpleTime struct
func NewSimpleTime(t time.Time) *SimpleTime {
return &SimpleTime{t}
}
// BeginningOfYear returns the beginning of the year from the SimpleTime struct
func (t *SimpleTime) BeginningOfYear() time.Time {
return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
}
// BeginningOfMonth returns the beginning of the month from the SimpleTime struct
func (t *SimpleTime) BeginningOfMonth() time.Time {
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
}
// BeginningOfDay returns the beginning of the day from the SimpleTime struct
func (t *SimpleTime) BeginningOfDay() time.Time {
return t.Truncate(time.Hour * 24)
}
// BeginningOfHour returns the beginning of the hour from the SimpleTime struct
func (t *SimpleTime) BeginningOfHour() time.Time {
return t.Truncate(time.Hour)
}
// BeginningOfMinute returns the beginning of the minute from the SimpleTime struct
func (t *SimpleTime) BeginningOfMinute() time.Time {
return t.Truncate(time.Minute)
}
// BeginningOfSecond returns the beginning of the second from the SimpleTime struct
func (t *SimpleTime) BeginningOfSecond() time.Time {
return t.Truncate(time.Second)
}
// EndOfYear returns the end of the year from the SimpleTime struct
func (t *SimpleTime) EndOfYear() time.Time {
return t.BeginningOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond)
}
// EndOfMonth returns the end of the month from the SimpleTime struct
func (t *SimpleTime) EndOfMonth() time.Time {
return t.BeginningOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
}
// EndOfDay returns the end of the day from the SimpleTime struct
func (t *SimpleTime) EndOfDay() time.Time {
return t.BeginningOfDay().AddDate(0, 0, 1).Add(-time.Nanosecond)
}
// EndOfHour returns the end of the hour from the SimpleTime struct
func (t *SimpleTime) EndOfHour() time.Time {
return t.BeginningOfHour().Add(time.Hour - time.Nanosecond)
}
// EndOfMinute returns the end of the minute from the SimpleTime struct
func (t *SimpleTime) EndOfMinute() time.Time {
return t.BeginningOfMinute().Add(time.Minute - time.Nanosecond)
}
// EndOfSecond returns the end of the second from the SimpleTime struct
func (t *SimpleTime) EndOfSecond() time.Time {
return t.BeginningOfSecond().Add(time.Second - time.Nanosecond)
}
// NextOccurrenceOfWeekday returns the next coming or occurence of the
// time.Weekday argument passed in
func (t *SimpleTime) NextOccurrenceOfWeekday(weekday time.Weekday) time.Time {
days := (int(weekday) - int(t.Weekday()) + 7) % 7
next := NewSimpleTime(t.AddDate(0, 0, days))
if days > 0 {
return next.BeginningOfDay()
}
return next.Time
}
// NextSunday returns the next coming Sunday from the
// date of the SimpleTime struct
func (t *SimpleTime) NextSunday() time.Time {
return t.NextOccurrenceOfWeekday(time.Weekday(0))
}
// NextMonday returns the next coming Monday from the
// date of the SimpleTime struct
func (t *SimpleTime) NextMonday() time.Time {
return t.NextOccurrenceOfWeekday(time.Weekday(1))
}
// NextTuesday returns the next coming Tuesday from the
// date of the SimpleTime struct
func (t *SimpleTime) NextTuesday() time.Time {
return t.NextOccurrenceOfWeekday(time.Weekday(2))
}
// NextWednesday returns the next coming Wednesday from the
// date of the SimpleTime struct
func (t *SimpleTime) NextWednesday() time.Time {
return t.NextOccurrenceOfWeekday(time.Weekday(3))
}
// NextThursday returns the next coming Thursday from the
// date of the SimpleTime struct
func (t *SimpleTime) NextThursday() time.Time {
return t.NextOccurrenceOfWeekday(time.Weekday(4))
}
// NextFriday returns the next coming Friday from the
// date of the SimpleTime struct
func (t *SimpleTime) NextFriday() time.Time {
return t.NextOccurrenceOfWeekday(time.Weekday(5))
}
// NextSaturday returns the next coming Saturday from the
// date of the SimpleTime struct
func (t *SimpleTime) NextSaturday() time.Time {
return t.NextOccurrenceOfWeekday(time.Weekday(6))
}
// AddDays adds n days to the SimpleTime date
func (t *SimpleTime) AddDays(days int) time.Time {
return t.AddDate(0, 0, days)
}
// NextDay returns the next day in time.Time format
func (t *SimpleTime) NextDay() time.Time {
return t.AddDays(1)
}
// PrevDay subtracts one day
func (t *SimpleTime) PrevDay() time.Time {
return t.AddDays(-1)
}
// Since returns the time elapsed since u from the reference time t
func (t *SimpleTime) Since(u time.Time) *SimpleDuration {
d := t.Sub(u)
return NewSimpleDuration(d)
}