-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
232 lines (187 loc) · 4.15 KB
/
date.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package gouse
import (
"fmt"
"strconv"
"time"
)
/* Date formats */
func format(input interface{}, format string) string {
var t time.Time
switch v := input.(type) {
case time.Time:
t = v
case string:
var err error
t, err = time.Parse(time.RFC3339, v)
if err != nil {
return ""
}
default:
return ""
}
return t.Format(format)
}
func ISODate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "2006-01-02T15:04:05.999Z")
} else {
return format(date[0], "2006-01-02T15:04:05.999Z")
}
}
func NormalDate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "01/02/2006")
} else {
return format(date[0].(time.Time), "01/02/2006")
}
}
func ReverseDate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "2006/01/02")
} else {
return format(date[0].(time.Time), "2006/01/02")
}
}
func DashDate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "2006-01-02")
} else {
return format(date[0].(time.Time), "2006-01-02")
}
}
func DotDate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "2006.01.02")
} else {
return format(date[0].(time.Time), "2006.01.02")
}
}
func UnderlineDate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "2006_01_02")
} else {
return format(date[0].(time.Time), "2006_01_02")
}
}
func SpaceDate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "2006 01 02")
} else {
return format(date[0].(time.Time), "2006 01 02")
}
}
func MonthDate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "01/2006")
} else {
return format(date[0].(time.Time), "01/2006")
}
}
func LongDate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "Monday, January 2, 2006")
} else {
return format(date[0].(time.Time), "Monday, January 2, 2006")
}
}
func UTCDate(date ...interface{}) string {
if len(date) == 0 {
return format(time.Now(), "Jan 2, 2006 at 3:04pm (MST)")
} else {
return format(date[0].(time.Time), "Jan 2, 2006 at 3:04pm (MST)")
}
}
/* Time formats */
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)
}
/* 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 = fmt.Sprintf("0%s", hourStr)
}
if minute < 10 {
minuteStr = fmt.Sprintf("0%s", minuteStr)
}
if second < 10 {
secondStr = fmt.Sprintf("0%s", secondStr)
}
return fmt.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()
fmt.Println(formatTime(t))
}
}
/* Utilities */
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))
}