-
Notifications
You must be signed in to change notification settings - Fork 6
/
time.go
134 lines (110 loc) · 3.25 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
127
128
129
130
131
132
133
134
/**
* Copyright 2018 gd Author. All Rights Reserved.
* Author: Chuck1024
*/
package utls
import (
"net"
"net/url"
"time"
)
const (
Nanosecond = int64(time.Nanosecond) // Nanosecond
Microsecond = 1000 * Nanosecond // Nanosecond
Millisecond = 1000 * Microsecond // Nanosecond
Second = 1000 * Millisecond // Nanosecond
Minute = 60 * Second // Nanosecond
Hour = 60 * Minute // Nanosecond
Day = 24 * Hour // Nanosecond
Year = 365 * Day // Nanosecond
SecondsPerYear = Year / Second // Second
SecondsPerDay = Day / Second // Second
SecondsPerHour = Hour / Second // Second
SecondsPerMinute = Minute / Second // Second
MillisecondsPerYear = Year / Millisecond // Millisecond
MillisecondsPerDay = Day / Millisecond // Millisecond
MillisecondsPerHour = Hour / Millisecond // Millisecond
MillisecondsPerMinute = Minute / Millisecond // Millisecond
MillisecondsPerSecond = Second / Millisecond // Millisecond
MicrosecondsPerYear = Year / Microsecond // Microsecond
MicrosecondsPerDay = Day / Microsecond // Microsecond
MicrosecondsPerHour = Hour / Microsecond // Microsecond
MicrosecondsPerMinute = Minute / Microsecond // Microsecond
MicrosecondsPerSecond = Second / Microsecond // Microsecond
MicrosecondsPerMillisecond = Millisecond / Microsecond // Microsecond
)
const timeLayout = "2006-01-02 15:04:05"
func GetCurrentSecond() int64 {
return time.Now().Unix()
}
func GetCurrentMillisecond() int64 {
return time.Now().UnixNano() / Millisecond
}
func GetCurrentMicrosecond() int64 {
return time.Now().UnixNano() / Microsecond
}
func GetCurrentNanosecond() int64 {
return time.Now().UnixNano()
}
func FromSecondToLocalDate(v int64) string {
return time.Unix(v, 0).Format(timeLayout)
}
func GetCurrentTime() string {
return time.Now().Format(timeLayout)
}
func FromLocalDateToSecond(v string) int64 {
loc, _ := time.LoadLocation("Local")
t, _ := time.ParseInLocation(timeLayout, v, loc)
return t.Unix()
}
func IsSameDay(t1, t2 time.Time) bool {
year1, month1, day1 := t1.Date()
year2, month2, day2 := t2.Date()
if year1 == year2 && month1 == month2 && day1 == day2 {
return true
}
return false
}
func IsSameDayWithTimestamp(d1, d2 int64) bool {
t1 := time.Unix(d1, 0)
t2 := time.Unix(d2, 0)
return IsSameDay(t1, t2)
}
const (
offset_day = 1
offset_monty = 100 * offset_day
offset_year = 100 * offset_monty
)
func FromTime2TimeInt(t time.Time) int {
y, m, d := t.Date()
return y*offset_year + int(m)*offset_monty + d*offset_day
}
func SplitTimeInt(t int) (year, month, day int) {
return t / offset_year, t % offset_year / offset_monty, t % offset_monty
}
func DGetCurrentTime(layout string) string {
return time.Now().Format(layout)
}
func Sleep(millisecond int64) {
time.Sleep(time.Duration(millisecond) * time.Millisecond)
}
func Duration(d int64) time.Duration {
return time.Duration(d)
}
func IsTimeoutError(err error) bool {
if err == nil {
return false
}
switch err := err.(type) {
case *url.Error:
if err, ok := err.Err.(net.Error); ok && err.Timeout() {
return true
}
case net.Error:
if err.Timeout() {
return true
}
default:
}
return false
}