/
WriteJsonTime.go
76 lines (68 loc) · 1.51 KB
/
WriteJsonTime.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
package udwJsonLib
import (
"strconv"
"time"
)
func WriteJsonTime(ctx *Context, t time.Time) {
y := t.Year()
if y < 0 || y >= 10000 {
panic("WriteJsonTime: year outside of range [0,9999] " + strconv.Itoa(y))
}
year, month, day := t.Date()
hour, minute, second := t.Clock()
writerTryRealloc(ctx, len(time.RFC3339Nano)+2)
WriterWriteByte(ctx, '"')
writeIntWithWidth(ctx, year, 4)
WriterWriteByte(ctx, '-')
writeIntWithWidth(ctx, int(month), 2)
WriterWriteByte(ctx, '-')
writeIntWithWidth(ctx, day, 2)
WriterWriteByte(ctx, 'T')
writeIntWithWidth(ctx, hour, 2)
WriterWriteByte(ctx, ':')
writeIntWithWidth(ctx, minute, 2)
WriterWriteByte(ctx, ':')
writeIntWithWidth(ctx, second, 2)
nonsecond := t.Nanosecond()
if nonsecond > 0 {
WriterWriteByte(ctx, '.')
writeIntWithWidth(ctx, nonsecond, 9)
}
_, offset := t.Zone()
if offset == 0 {
WriterWriteByte(ctx, 'Z')
} else {
if offset < 0 {
WriterWriteByte(ctx, '-')
offset = -offset
} else {
WriterWriteByte(ctx, '+')
}
writeIntWithWidth(ctx, offset/3600, 2)
WriterWriteByte(ctx, ':')
writeIntWithWidth(ctx, (offset/60)%60, 2)
}
WriterWriteByte(ctx, '"')
return
}
func writeIntWithWidth(ctx *Context, x int, width int) {
u := uint(x)
if x < 0 {
WriterWriteByte(ctx, '-')
u = uint(-x)
}
var buf [20]byte
i := len(buf)
for u >= 10 {
i--
q := u / 10
buf[i] = byte('0' + u - q*10)
u = q
}
i--
buf[i] = byte('0' + u)
for w := len(buf) - i; w < width; w++ {
WriterWriteByte(ctx, '0')
}
WriterWriteByteList(ctx, buf[i:])
}