From 85ac65cdc332dea128b8bf02d290d22c16a27c14 Mon Sep 17 00:00:00 2001 From: Aleksandr Pasechnik Date: Sun, 6 May 2018 13:05:25 -0400 Subject: [PATCH] something is broken Time doesn't get formatted correctly, decimal seconds disappear, and something about the benchmarks results in null characters for the day of the month in the benchmarks. --- time_test.go | 92 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 22 deletions(-) diff --git a/time_test.go b/time_test.go index f925267..19a4375 100644 --- a/time_test.go +++ b/time_test.go @@ -1,6 +1,7 @@ package cstrftime import ( + "fmt" "testing" "time" @@ -8,33 +9,80 @@ import ( ) func TestFormat(t *testing.T) { - tm := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC) + tm := time.Date(2000, 2, 1, 16, 30, 22, 123456789, time.UTC) - t.Run("weekday as locale’s abbreviated name", func(t *testing.T) { - assert.Equal(t, "Tue", Format("%a", tm)) - }) + cases := []struct { + name, format, result string + }{ + {"weekday as locale’s abbreviated name", "%a", "Tue"}, + {"weekday as locale’s full name", "%A", "Tuesday"}, + {"weekday as a decimal number", "%w", "2"}, + {"day of the month as a zero-padded decimal number", "%d", "01"}, + {"month as locale’s abbreviated name", "%b", "Feb"}, + {"month as locale’s full name", "%B", "February"}, + {"month as a zero-padded decimal number", "%m", "02"}, + {"24 hour Hours", "%H", "16"}, + {"12 hour Hours", "%I", "04"}, + {"Minutes", "%M", "30"}, + {"Seconds", "%S", "22"}, + {"seconds with decimals", "%S.%f", "22.123456"}, + {"american date", "%m/%d/%Y", "02/01/2000"}, + {"proper date", "%Y/%m/%d", "2000/02/01"}, + {"dashed date", "%Y-%m-%d", "2000-02-01"}, + } - t.Run("weekday as locale’s full name", func(t *testing.T) { - assert.Equal(t, "Tuesday", Format("%A", tm)) - }) + for _, c := range cases { + c := c - t.Run("weekday as a decimal number", func(t *testing.T) { - assert.Equal(t, "2", Format("%w", tm)) - }) + t.Run(c.name, func(t *testing.T) { + assert.Equal(t, c.result, Format(c.format, tm)) + }) + } +} + +func BenchmarkFormat(b *testing.B) { + tm := time.Date(2000, time.February, 13, 16, 30, 20, 12345, time.UTC) + + cases := []struct { + name, cFormat, goFormat string + }{ + {"year", "%Y", "2006"}, + {"date", "%Y/%m/%d", "2006/01/02"}, + } + + for _, c := range cases { + assert.Equal(b, tm.Format(c.goFormat), Format(c.cFormat, tm), c.name) + } - t.Run("day of the month as a zero-padded decimal number", func(t *testing.T) { - assert.Equal(t, "01", Format("%d", tm)) - }) + for _, c := range cases { + for _, native := range []bool{true, false} { + c := c + native := native - t.Run("month as locale’s abbreviated name", func(t *testing.T) { - assert.Equal(t, "Feb", Format("%b", tm)) - }) + b.Run(fmt.Sprintf("%s - native(%t)", c.name, native), func(b *testing.B) { + var ( + want = tm.Format(c.goFormat) + got string + ) - t.Run("month as locale’s full name", func(t *testing.T) { - assert.Equal(t, "February", Format("%B", tm)) - }) + switch native { + case true: + b.ResetTimer() + for i := 0; i < b.N; i++ { + if got = tm.Format(c.goFormat); want != got { + b.Fatalf("want: %q got: %q", want, got) + } + } - t.Run("month as a zero-padded decimal number", func(t *testing.T) { - assert.Equal(t, "02", Format("%m", tm)) - }) + case false: + b.ResetTimer() + for i := 0; i < b.N; i++ { + if got = Format(c.cFormat, tm); want != got { + b.Fatalf("want: %q got: %q", want, got) + } + } + } + }) + } + } }