-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
121 lines (103 loc) · 2.28 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
package times
import (
"fmt"
"time"
)
func IsLeap(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}
func NumOfYearDays(year int) int {
if IsLeap(year) {
return 366
}
return 365
}
func GetDayTime(t time.Time) time.Duration {
return time.Hour*time.Duration(t.Hour()) +
time.Minute*time.Duration(t.Minute()) +
time.Second*time.Duration(t.Second()) +
time.Duration(t.Nanosecond())
}
func IsToday(t time.Time) bool {
return DateWithTime(t).IsToday()
}
func IsYesterday(t time.Time) bool {
return DateWithTime(t).IsYesterday()
}
func IsTomorrow(t time.Time) bool {
return DateWithTime(t).IsTomorrow()
}
func BeginOfDay(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
}
func EndOfDay(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 999999999, time.Local)
}
func ToTimerText(seconds int64) string {
h := seconds / 3600
m := (seconds % 3600) / 60
s := seconds % 60
if h < 100 {
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
return fmt.Sprintf("%d:%02d:%02d", h, m, s)
}
func UnixSecP(sec *int64) *time.Time {
if sec == nil {
return nil
}
t := time.Unix(*sec, 0)
return &t
}
func ToUnixSecP(t *time.Time) *int64 {
if t == nil {
return nil
}
sec := int64(t.Second())
return &sec
}
const (
Day = 24 * time.Hour
Week = 7 * 24 * time.Hour
)
var enWeekdaySymbols = [7]string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
func GetWeekdaySymbol(d int) string {
d = d % 7
return Localize(enWeekdaySymbols[d])
}
func abs[T ~int64](num T) T {
if num < 0 {
return -num
}
return num
}
func RepeatSchedule(repeat Repeat, start time.Time, before time.Time) []time.Time {
var res []time.Time
t := start
for t.Before(before) {
res = append(res, t)
if repeat == Never {
return res
}
t = RepeatNext(repeat, t)
}
return res
}
func RepeatNext(repeat Repeat, start time.Time) time.Time {
switch repeat {
case Daily:
return start.AddDate(0, 0, 1)
case Weekly:
return start.AddDate(0, 0, 7)
case BiWeekly:
return start.AddDate(0, 0, 14)
case Monthly:
return start.AddDate(0, 1, 0)
case Quarterly:
return start.AddDate(0, 3, 0)
case Yearly:
return start.AddDate(1, 0, 0)
default:
panic(fmt.Sprintf("invalid repeat %d: %v", repeat, repeat))
}
}