Skip to content

Commit bbed555

Browse files
committed
Pull the 2006-01-02 layout to the top when checking
The current top 2 formats are, from some checks I've done, 2 dominant formats used in Hugo projects. Checking these first has a big effect: ```bash name old time/op new time/op delta CommonTimeLayouts-10 1.45µs ± 0% 0.33µs ± 1% -77.45% (p=0.029 n=4+4) name old alloc/op new alloc/op delta CommonTimeLayouts-10 1.55kB ± 0% 0.18kB ± 0% -88.14% (p=0.029 n=4+4) name old allocs/op new allocs/op delta CommonTimeLayouts-10 38.0 ± 0% 6.0 ± 0% -84.21% (p=0.029 n=4+4) ``` See gohugoio/hugo#10942
1 parent ba0a5b6 commit bbed555

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

cast_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,17 @@ func BenchmarkTrimZeroDecimal(b *testing.B) {
897897
}
898898
}
899899

900+
func BenchmarkCommonTimeLayouts(b *testing.B) {
901+
for i := 0; i < b.N; i++ {
902+
for _, commonLayout := range []string{"2019-04-29", "2017-05-30T00:00:00Z"} {
903+
_, err := StringToDateInDefaultLocation(commonLayout, time.UTC)
904+
if err != nil {
905+
b.Fatal(err)
906+
}
907+
}
908+
}
909+
}
910+
900911
func TestIndirectPointers(t *testing.T) {
901912
c := qt.New(t)
902913

caste.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,8 @@ func (f timeFormat) hasTimezone() bool {
14061406

14071407
var (
14081408
timeFormats = []timeFormat{
1409+
// Keep common formats at the top.
1410+
{"2006-01-02", timeFormatNoTimezone},
14091411
{time.RFC3339, timeFormatNumericTimezone},
14101412
{"2006-01-02T15:04:05", timeFormatNoTimezone}, // iso8601 without timezone
14111413
{time.RFC1123Z, timeFormatNumericTimezone},
@@ -1421,7 +1423,6 @@ var (
14211423
{time.UnixDate, timeFormatNamedTimezone},
14221424
{time.RubyDate, timeFormatNumericTimezone},
14231425
{"2006-01-02 15:04:05Z07:00", timeFormatNumericTimezone},
1424-
{"2006-01-02", timeFormatNoTimezone},
14251426
{"02 Jan 2006", timeFormatNoTimezone},
14261427
{"2006-01-02 15:04:05 -07:00", timeFormatNumericTimezone},
14271428
{"2006-01-02 15:04:05 -0700", timeFormatNumericTimezone},

0 commit comments

Comments
 (0)