Skip to content

Commit 7b578d0

Browse files
committed
optimize the performance of the function appendFormatRFC3339
1 parent 55f00fd commit 7b578d0

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/time/format_rfc3339.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import "errors"
1818
func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte {
1919
_, offset, abs := t.locabs()
2020

21-
// Format date.
21+
// Format date and time.
2222
year, month, day := abs.days().date()
23-
// Format time.
2423
hour, min, sec := abs.clock()
2524

2625
b = appendIntWidth4(b, year)

src/time/format_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,10 +1095,10 @@ func FuzzParseRFC3339(f *testing.F) {
10951095
func TestAppendIntWidth(t *testing.T) {
10961096
values := []int{0, -1, 1, 10, -10, 99, -99, 9999, -9999, 10001}
10971097
for _, v := range values {
1098-
exp := AppendInt(nil, v, 4)
1098+
want := AppendInt(nil, v, 4)
10991099
got := AppendIntWidth4(nil, v)
1100-
if !bytes.Equal(got, exp) {
1101-
t.Errorf("AppendIntWidth4(%d) = %s, want %s", v, got, exp)
1100+
if !bytes.Equal(got, want) {
1101+
t.Errorf("AppendIntWidth4(%d) = %s, want %s", v, got, want)
11021102
}
11031103
}
11041104
}
@@ -1107,23 +1107,23 @@ func BenchmarkAppendIntWidth4(b *testing.B) {
11071107
b.Run("name=AppendInt", func(b *testing.B) {
11081108
var buf = make([]byte, 0, 8)
11091109
b.ResetTimer()
1110-
for i := 0; i < b.N; i++ {
1110+
for b.Loop() {
11111111
buf = AppendInt(buf[:0], 360, 4)
11121112
}
11131113
})
11141114
b.Run("name=AppendIntWidth4", func(b *testing.B) {
11151115
var buf = make([]byte, 0, 8)
11161116
b.ResetTimer()
1117-
for i := 0; i < b.N; i++ {
1117+
for b.Loop() {
11181118
buf = AppendIntWidth4(buf[:0], 360)
11191119
}
11201120
})
11211121
}
11221122

11231123
func BenchmarkTimeFormatRFC3339(b *testing.B) {
1124-
tm := Now()
1124+
tm := Unix(1661201140, 676836973)
11251125
buf := make([]byte, 0, 64)
1126-
for i := 0; i < b.N; i++ {
1126+
for b.Loop() {
11271127
buf = tm.AppendFormat(buf[:0], RFC3339)
11281128
}
11291129
}

0 commit comments

Comments
 (0)