-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeStr.go
123 lines (103 loc) · 2.6 KB
/
timeStr.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
package gtool
import (
"fmt"
"strconv"
"time"
)
//timestamp format year month day
func TsFormatYmd(unix int64) string {
if unix == 0 {
return time.Unix(time.Now().Unix(), 0).Format(FORMAT_TIME_YMD)
} else {
return time.Unix(unix, 0).Format(FORMAT_TIME_YMD)
}
}
//timestamp format month day
func TsFormatMd(ts int64) string {
t, isNow := isNowYear(ts)
if isNow {
return t.Format(FORMAT_TIME_MD)
} else {
return t.Format(FORMAT_TIME_YMD)
}
}
//timestamp format hour minute
func TsFormatHi(ts int64) string {
t, isNow := isNowYear(ts)
if isNow {
return t.Format(FORMAT_TIME_MD_HI)
} else {
return t.Format(FORMAT_TIME_YMD_HI)
}
}
//accurate timestamp string
func TsFormatAccurate(ts int64) string {
var retStr string
nowTs := TsNow()
offset := nowTs - ts
if offset >= 0 { //以前
if offset < 60 {
retStr = strconv.FormatInt(offset, 10) + "秒前"
} else if offset < 3600 {
retStr = strconv.FormatInt(offset/60, 10) + "分钟前"
} else if offset < 86400 {
retStr = strconv.FormatInt(offset/3600, 10) + "小时前"
} else if offset < 259200 {
retStr = strconv.FormatInt(offset/86400, 10) + "天前"
} else {
retStr = TsFormatYmd(ts)
}
} else { //未来
offset = ts - nowTs
if offset < 60 {
retStr = strconv.FormatInt(offset, 10) + "秒后"
} else if offset < 3600 {
retStr = strconv.FormatInt(offset/60, 10) + "分钟后"
} else if offset < 86400 {
retStr = strconv.FormatInt(offset/3600, 10) + "小时后"
} else if offset < 259200 {
retStr = strconv.FormatInt(offset/86400, 10) + "天后"
} else {
retStr = TsFormatYmd(ts)
}
}
return retStr
}
//time string covert timestamp
func TimeStrToTs(timeStr string) (int64) {
theTime, _ := time.ParseInLocation(FORMAT_TIME_YMD_HIS, timeStr, TimeLoc())
return theTime.Unix()
}
//now time string
func TimeNow() string {
return time.Now().Format(FORMAT_TIME_YMD_HIS)
}
//date string today
func DateToday() (dateStr string) {
return DateYmd(time.Now().Unix())
}
//date string today begin time
func DateTodayBegin() string {
return fmt.Sprintf("%s 00:00:00", DateToday())
}
//date string today end time
func DateTodayEnd() string {
return fmt.Sprintf("%s 23:59:59", DateToday())
}
//date string yesterday
func DateYesterday() (dateStr string) {
return DateYmdDay(-1)
}
//date string tomorrow
func DateTomorrow() (dateStr string) {
return DateYmdDay(1)
}
//date string use days
func DateYmdDay(days int) (dateStr string) {
ts := time.Now().AddDate(0, 0, days).Unix()
return DateYmd(ts)
}
//date string use timestamp
func DateYmd(ts int64) (dateStr string) {
return time.Unix(ts, 0).Format(FORMAT_TIME_YMD)
}