-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
121 lines (95 loc) · 2.71 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
// Copyright 2024 mlycore. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"time"
_ "unsafe"
)
const (
sepDash = '-'
sepColon = ':'
sepT = 'T'
sepZ = 'Z'
sepSpace = ' '
)
func (e *LogEntry) SetTimestamp(format string) *LogEntry {
if len(format) != 0 {
e.timestamp = []byte(time.Now().Format(format))
} else {
e.fastTimestamp()
}
return e
}
func (e *LogEntry) fastTimestamp() *LogEntry {
// TODO: make timestamp configurable
// NOTE: this fast timestamp will be generated without location
// change time format from 2024-03-08T16:30:00Z to 2024-03-08 16:30:00
var tmp [20]byte
totalsec, _, _ := now()
totalsec += 9223372028715321600
year, month, day, _ := absDate(uint64(totalsec), true)
hour, minute, second := absClock(uint64(totalsec))
// year location tmp[0:3]
ya := year / 100 * 2
tmp[0] = itoa[ya]
tmp[1] = itoa[ya+1]
yb := year % 100 * 2
tmp[2] = itoa[yb]
tmp[3] = itoa[yb+1]
// seperator
tmp[4] = sepDash
// month location tmp[5:6]
m := month % 100 * 2
tmp[5] = itoa[m]
tmp[6] = itoa[m+1]
// seperator
tmp[7] = sepDash
// day location tmp[8:9]
d := day % 100 * 2
tmp[8] = itoa[d]
tmp[9] = itoa[d+1]
// seperator
tmp[10] = sepSpace
// hour location tmp[11:12]
h := hour % 100 * 2
tmp[11] = itoa[h]
tmp[12] = itoa[h+1]
// seperator
tmp[13] = sepColon
// minute location tmp[14:15]
min := minute % 100 * 2
tmp[14] = itoa[min]
tmp[15] = itoa[min+1]
// seperator
tmp[16] = sepColon
// second location tmp[17:18]
s := second % 100 * 2
tmp[17] = itoa[s]
tmp[18] = itoa[s+1]
// end
stmp := tmp[:20]
e.timestamp = append(e.timestamp, stmp...)
return e
}
// fast conversion from int to alphabet
const itoa = "00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899"
//go:noescape
//go:linkname now time.now
func now() (sec int64, nsec int32, mono int64)
//go:noescape
//go:linkname absDate time.absDate
func absDate(abs uint64, full bool) (year int, month time.Month, day int, yday int)
//go:noescape
//go:linkname absClock time.absClock
func absClock(abs uint64) (hour, min, sec int)