forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotime.go
129 lines (105 loc) · 3.08 KB
/
gotime.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
package timex
import (
"time"
)
// some time layout or time
const (
DatetimeLayout = "2006-01-02 15:04:05"
LayoutWithMs3 = "2006-01-02 15:04:05.000"
LayoutWithMs6 = "2006-01-02 15:04:05.000000"
DateOnlyLayout = "2006-01-02"
TimeOnlyLayout = "15:04:05"
// ZeroUnix zero unix timestamp
ZeroUnix int64 = -62135596800
)
var (
// DefaultLayout template for format time
DefaultLayout = DatetimeLayout
// ZeroTime zero time instance
ZeroTime = time.Time{}
)
// SetLocalByName set local by tz name. eg: UTC, PRC
func SetLocalByName(tzName string) error {
location, err := time.LoadLocation(tzName)
if err != nil {
return err
}
time.Local = location
return nil
}
// NowAddDay add some day time from now
func NowAddDay(day int) time.Time {
return time.Now().AddDate(0, 0, day)
}
// NowAddHour add some hour time from now
func NowAddHour(hour int) time.Time {
return time.Now().Add(time.Duration(hour) * OneHour)
}
// NowAddMinutes add some minutes time from now
func NowAddMinutes(minutes int) time.Time {
return time.Now().Add(time.Duration(minutes) * OneMin)
}
// NowAddSec add some seconds time from now. alias of NowAddSeconds()
func NowAddSec(seconds int) time.Time {
return time.Now().Add(time.Duration(seconds) * time.Second)
}
// NowAddSeconds add some seconds time from now
func NowAddSeconds(seconds int) time.Time {
return time.Now().Add(time.Duration(seconds) * time.Second)
}
// NowHourStart time
func NowHourStart() time.Time {
return HourStart(time.Now())
}
// NowHourEnd time
func NowHourEnd() time.Time {
return HourEnd(time.Now())
}
// AddDay add some day time for given time
func AddDay(t time.Time, day int) time.Time {
return t.AddDate(0, 0, day)
}
// AddHour add some hour time for given time
func AddHour(t time.Time, hour int) time.Time {
return t.Add(time.Duration(hour) * OneHour)
}
// AddMinutes add some minutes time for given time
func AddMinutes(t time.Time, minutes int) time.Time {
return t.Add(time.Duration(minutes) * OneMin)
}
// AddSeconds add some seconds time for given time
func AddSeconds(t time.Time, seconds int) time.Time {
return t.Add(time.Duration(seconds) * time.Second)
}
// AddSec add some seconds time for given time. alias of AddSeconds()
func AddSec(t time.Time, seconds int) time.Time {
return t.Add(time.Duration(seconds) * time.Second)
}
// HourStart time for given time
func HourStart(t time.Time) time.Time {
y, m, d := t.Date()
return time.Date(y, m, d, t.Hour(), 0, 0, 0, t.Location())
}
// HourEnd time for given time
func HourEnd(t time.Time) time.Time {
y, m, d := t.Date()
return time.Date(y, m, d, t.Hour(), 59, 59, int(time.Second-time.Nanosecond), t.Location())
}
// DayStart time for given time
func DayStart(t time.Time) time.Time {
y, m, d := t.Date()
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
}
// DayEnd time for given time
func DayEnd(t time.Time) time.Time {
y, m, d := t.Date()
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
}
// TodayStart time
func TodayStart() time.Time {
return DayStart(time.Now())
}
// TodayEnd time
func TodayEnd() time.Time {
return DayEnd(time.Now())
}