-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
135 lines (104 loc) · 2.24 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
135
package gouse
import (
"strconv"
"time"
)
func Second() int {
return time.Now().Second()
}
func Minute() int {
return time.Now().Minute()
}
func Hour() int {
return time.Now().Hour()
}
func Day() int {
return time.Now().Day()
}
func Month() int {
return int(time.Now().Month())
}
func Year() int {
return time.Now().Year()
}
func Weekday() int {
return int(time.Now().Weekday())
}
func Unix() int64 {
return time.Now().Unix()
}
func UnixMilli() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func UnixMicro() int64 {
return time.Now().UnixNano() / int64(time.Microsecond)
}
func UnixNano() int64 {
return time.Now().UnixNano()
}
func UnixMilliToTime(milli int64) time.Time {
return time.Unix(0, milli*int64(time.Millisecond))
}
func UnixMicroToTime(micro int64) time.Time {
return time.Unix(0, micro*int64(time.Microsecond))
}
func UnixNanoToTime(nano int64) time.Time {
return time.Unix(0, nano)
}
func ToSecond(second int) time.Duration {
return time.Duration(second) * time.Second
}
func ToMinute(minute int) time.Duration {
return time.Duration(minute) * time.Minute
}
func ToHour(hour int) time.Duration {
return time.Duration(hour) * time.Hour
}
func SleepSecond(second int) {
time.Sleep(ToSecond(second))
}
func SleepMinute(minute int) {
time.Sleep(ToMinute(minute))
}
func SleepHour(hour int) {
time.Sleep(ToHour(hour))
}
/* Clock format */
func formatTime(t time.Time) string {
hour := t.Hour()
minute := t.Minute()
second := t.Second()
hourStr := strconv.Itoa(hour)
minuteStr := strconv.Itoa(minute)
secondStr := strconv.Itoa(second)
if hour < 10 {
hourStr = Sprintf("0%s", hourStr)
}
if minute < 10 {
minuteStr = Sprintf("0%s", minuteStr)
}
if second < 10 {
secondStr = Sprintf("0%s", secondStr)
}
return Sprintf("%s:%s:%s", hourStr, minuteStr, secondStr)
}
func TerminalClock() {
msgTime := make(chan time.Time)
go func() {
for {
time.Sleep(1 * time.Second)
msgTime <- time.Now()
}
}()
for t := range msgTime {
Cls()
Println(formatTime(t))
}
}
func DiffTime(t1, t2 time.Time) time.Duration {
return t2.Sub(t1)
}
func DiffTimeNow(startTime time.Time) time.Duration {
elapsedTime := float64(time.Since(startTime).Seconds() * 1000)
return time.Duration(elapsedTime)
}