-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_time.go
79 lines (69 loc) · 2.04 KB
/
type_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
package glog
import (
"time"
"github.com/yu31/glog/pkg/buffer"
)
const (
defaultTimeLayout = time.RFC3339Nano
)
// Defines the time format type.
const (
// TimeFormatUnixSecond defines a time format that makes time fields to be
// serialized as Unix timestamp integers in seconds.
TimeFormatUnixSecond = "UnixSecond"
// TimeFormatUnixMilli defines a time format that makes time fields to be
// serialized as Unix timestamp integers in milliseconds.
TimeFormatUnixMilli = "UnixMilli"
// TimeFormatUnixMicro defines a time format that makes time fields to be
// serialized as Unix timestamp integers in microseconds.
TimeFormatUnixMicro = "UnixMicro"
// TimeFormatUnixNano defines a time format that makes time fields to be
// serialized as Unix timestamp integers in nanoseconds.
TimeFormatUnixNano = "UnixNano"
)
// Defines the format type for time.Duration.
const (
DurationFormatNano int8 = iota
DurationFormatMicro
DurationFormatMilli
DurationFormatSecond
DurationFormatMinute
DurationFormatHour
)
const (
nanoSuffix = "ns"
microSuffix = "µs"
milliSuffix = "ms"
secondSuffix = "s"
minuteSuffix = "min"
hourSuffix = "h"
)
// AppendDuration encode the time.Duration to a strings by specified layout.
func AppendDuration(buf *buffer.Buffer, d time.Duration, layout int8) {
switch layout {
case DurationFormatNano:
buf.AppendInt(int64(d))
buf.AppendString(nanoSuffix)
case DurationFormatMicro:
sec := d / time.Microsecond
nsec := d % time.Microsecond
buf.AppendFloat(float64(sec)+float64(nsec)/1e3, 64)
buf.AppendString(microSuffix)
case DurationFormatMilli:
sec := d / time.Millisecond
nsec := d % time.Millisecond
buf.AppendFloat(float64(sec)+float64(nsec)/1e6, 64)
buf.AppendString(milliSuffix)
case DurationFormatSecond:
buf.AppendFloat(d.Seconds(), 64)
buf.AppendString(secondSuffix)
case DurationFormatMinute:
buf.AppendFloat(d.Minutes(), 64)
buf.AppendString(minuteSuffix)
case DurationFormatHour:
buf.AppendFloat(d.Hours(), 64)
buf.AppendString(hourSuffix)
default:
buf.AppendString(d.String())
}
}