-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
extension.go
67 lines (61 loc) · 1.67 KB
/
extension.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
package strftime
import (
"strconv"
"time"
)
// NOTE: declare private variable and iniitalize once in init(),
// and leave the Milliseconds() function as returning static content.
// This way, `go doc -all` does not show the contents of the
// milliseconds function
var milliseconds Appender
var microseconds Appender
var unixseconds Appender
func init() {
milliseconds = AppendFunc(func(b []byte, t time.Time) []byte {
millisecond := int(t.Nanosecond()) / int(time.Millisecond)
if millisecond < 100 {
b = append(b, '0')
}
if millisecond < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(millisecond)...)
})
microseconds = AppendFunc(func(b []byte, t time.Time) []byte {
microsecond := int(t.Nanosecond()) / int(time.Microsecond)
if microsecond < 100000 {
b = append(b, '0')
}
if microsecond < 10000 {
b = append(b, '0')
}
if microsecond < 1000 {
b = append(b, '0')
}
if microsecond < 100 {
b = append(b, '0')
}
if microsecond < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(microsecond)...)
})
unixseconds = AppendFunc(func(b []byte, t time.Time) []byte {
return append(b, strconv.FormatInt(t.Unix(), 10)...)
})
}
// Milliseconds returns the Appender suitable for creating a zero-padded,
// 3-digit millisecond textual representation.
func Milliseconds() Appender {
return milliseconds
}
// Microsecond returns the Appender suitable for creating a zero-padded,
// 6-digit microsecond textual representation.
func Microseconds() Appender {
return microseconds
}
// UnixSeconds returns the Appender suitable for creating
// unix timestamp textual representation.
func UnixSeconds() Appender {
return unixseconds
}