forked from shenghui0779/yiigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
43 lines (35 loc) · 1.11 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
package yiigo
import "time"
// GMT8 东八区时区
var GMT8 = time.FixedZone("CST", 8*3600)
// TimeToStr 时间戳格式化为时间字符串
// 若 timestamp < 0,则使用 `time.Now()`
func TimeToStr(layout string, timestamp int64, loc *time.Location) string {
if timestamp < 0 {
return time.Now().In(loc).Format(layout)
}
return time.Unix(timestamp, 0).In(loc).Format(layout)
}
// StrToTime 时间字符串解析为时间戳
func StrToTime(layout, datetime string, loc *time.Location) time.Time {
t, _ := time.ParseInLocation(layout, datetime, loc)
return t
}
// WeekAround 返回给定时间戳所在周的「周一」和「周日」时间字符串
func WeekAround(layout string, now time.Time) (monday, sunday string) {
weekday := now.Weekday()
// monday
offset := int(time.Monday - weekday)
if offset > 0 {
offset = -6
}
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
monday = today.AddDate(0, 0, offset).Format(layout)
// sunday
offset = int(time.Sunday - weekday)
if offset < 0 {
offset += 7
}
sunday = today.AddDate(0, 0, offset).Format(layout)
return
}