forked from nanjishidu/gomini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
126 lines (114 loc) · 3.36 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
package gomini
import (
"strings"
"time"
"github.com/jinzhu/now"
)
var WeekdayString = []string{"周日", "周一", "周二", "周三", "周四", "周五", "周六"}
// GetWeekdayCnName
// 获取周一到周日
func GetWeekdayCnName(value string) (string, error) {
tpDate, err := now.ParseInLocation(time.Local, value)
if err != nil {
return "", err
}
return WeekdayString[tpDate.Weekday()], nil
}
// WebTime
func WebTime(t time.Time) string {
ftime := t.Format(time.RFC1123)
if strings.HasSuffix(ftime, "UTC") {
ftime = ftime[0:len(ftime)-3] + "GMT"
}
return ftime
}
// GetTimeAgo
func GetTimeAgo(t int64) (s string) {
tt := time.Now().Unix() - t
if tt < 60 {
s = GetInt64Str(tt) + "秒以前"
} else if tt < 3600 {
m := tt / 60
s = GetInt64Str(m) + "分钟以前"
} else if tt < 86400 {
m := tt / 3600
s = GetInt64Str(m) + "小时以前"
} else if tt < 2592000 {
m := tt / 86400
s = GetInt64Str(m) + "天以前"
} else if tt < 2592000*12 {
m := tt / 2592000
s = GetInt64Str(m) + "月以前"
} else {
m := tt / (2592000 * 12)
s = GetInt64Str(m) + "年以前"
}
return
}
const (
DateTypeYear = iota
DateTypeMonth
DateTypeWeekday
DateTypeDay
)
// GetDateBetweenByDateTypeAndDateTimeAndTimeRange
func GetDateBetweenByDateTypeAndDateTimeAndTimeRange(dateType int, dateTime string, timeFormatParams ...string) (string, string, [][]string) {
startTime, endTime := getDateBetweenByDateTypeAndDateTime(dateType, dateTime)
var timeFormat = "2006-01-02"
if len(timeFormatParams) > 0 {
timeFormat = timeFormatParams[0]
}
var (
startTimeStr = startTime.Format(timeFormat)
endTimeStr = endTime.Format(timeFormat)
timeRangeSlice [][]string
)
for {
dateTimeSlice := []string{startTime.Format(timeFormat)}
switch dateType {
case DateTypeYear:
startTime = startTime.AddDate(0, 1, 0)
case DateTypeMonth:
startTime = startTime.AddDate(0, 0, 1)
case DateTypeWeekday:
startTime = startTime.AddDate(0, 0, 1)
case DateTypeDay:
startTime = startTime.Add(time.Hour)
}
if endTime.Before(startTime) {
break
}
dateTimeSlice = append(dateTimeSlice, startTime.Format(timeFormat))
timeRangeSlice = append(timeRangeSlice, dateTimeSlice)
}
return startTimeStr, endTimeStr, timeRangeSlice
}
// GetDateBetweenByDateTypeAndDateTime
func GetDateBetweenByDateTypeAndDateTime(dateType int, dateTime string, timeFormatParams ...string) (startTimeStr, endTimeStr string) {
startTime, endTime := getDateBetweenByDateTypeAndDateTime(dateType, dateTime)
var timeFormat = "2006-01-02"
if len(timeFormatParams) > 0 {
timeFormat = timeFormatParams[0]
}
return startTime.Format(timeFormat), endTime.Format(timeFormat)
}
// getDateBetweenByDateTypeAndDateTime
func getDateBetweenByDateTypeAndDateTime(dateType int, dateTime string) (startTime, endTime time.Time) {
t := now.MustParseInLocation(time.Local, dateTime)
switch dateType {
case DateTypeYear:
startTime = now.With(t).BeginningOfYear()
endTime = now.With(t.AddDate(1, 0, 0)).BeginningOfYear()
case DateTypeMonth:
startTime = now.With(t).BeginningOfMonth()
endTime = now.With(t.AddDate(0, 1, 0)).BeginningOfMonth()
case DateTypeWeekday:
now.WeekStartDay = time.Monday
startTime = now.With(t).BeginningOfWeek()
endTime = startTime.AddDate(0, 0, 7)
case DateTypeDay:
startTime = now.With(t).BeginningOfDay()
endTime = now.With(t.AddDate(0, 0, 1)).BeginningOfDay()
}
return
}