-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtime.go
98 lines (86 loc) · 2.79 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
package anytime
import (
"fmt"
"time"
)
// fixedZoneHM returns the time zone with the given offset in hours and minutes
// from UTC.
func fixedZoneHM(h, m int) *time.Location {
offset := h*60*60 + m*60
sign := "+"
if h < 0 {
sign = "-"
h = -h
}
name := fmt.Sprintf("%s%02d:%02d", sign, h, m)
return time.FixedZone(name, offset)
}
// fixedZone returns the time zone with the given offset in hours from UTC.
func fixedZone(offsetHours int) *time.Location {
return fixedZoneHM(offsetHours, 0)
}
// nextMonthDayTime returns the next month relative to time t, with given day of month and time of day.
func nextMonthDayTime(t time.Time, month time.Month, day int, hour int, min int, sec int) time.Time {
nm := nextSpecificMonth(t, month)
return time.Date(nm.start.Year(), nm.start.Month(), day, hour, min, sec, 0, t.Location())
}
// prevMonthDayTime returns the previous month relative to time t, with given day of month and time of day.
func prevMonthDayTime(t time.Time, month time.Month, day int, hour int, min int, sec int) time.Time {
pm := lastSpecificMonth(t, month)
return time.Date(pm.start.Year(), pm.start.Month(), day, hour, min, sec, 0, t.Location())
}
// lastYear returns the previous year relative to time now.
func lastYear(now time.Time) Range {
return truncateYear(now.AddDate(-1, 0, 0))
}
// nextSpecificMonth returns the next month relative to time t.
func nextSpecificMonth(t time.Time, month time.Month) Range {
y := t.Year()
if month-t.Month() <= 0 {
y++
}
d := time.Date(y, month, 1, 0, 0, 0, 0, t.Location())
e := d.AddDate(0, 1, 0)
dur := e.Sub(d)
return Range{d, dur}
}
// lastSpecificMonth returns the next month relative to time t.
func lastSpecificMonth(t time.Time, month time.Month) Range {
y := t.Year()
if t.Month()-month <= 0 {
y--
}
d := time.Date(y, month, 1, 0, 0, 0, 0, t.Location())
e := d.AddDate(0, 1, 0)
dur := e.Sub(d)
return Range{d, dur}
}
// truncateDay returns a date truncated to the day.
func truncateDay(t time.Time) Range {
y, m, d := t.Date()
s := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
e := s.AddDate(0, 0, 1)
return Range{s, e.Sub(s)}
}
// truncateWeek returns a date truncated to the week.
func truncateWeek(t time.Time) Range {
for t.Weekday() != time.Sunday {
t = t.AddDate(0, 0, -1)
}
s := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
e := s.AddDate(0, 0, 7)
return Range{s, e.Sub(s)}
}
// truncateMonth returns a date truncated to the month.
func truncateMonth(t time.Time) Range {
y, m, _ := t.Date()
s := time.Date(y, m, 1, 0, 0, 0, 0, t.Location())
e := s.AddDate(0, 1, 0)
return Range{s, e.Sub(s)}
}
// truncateYear returns a date truncated to the year.
func truncateYear(t time.Time) Range {
s := time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
e := s.AddDate(1, 0, 0)
return Range{s, e.Sub(s)}
}