|
| 1 | +package lua |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestStrftime(t *testing.T) { |
| 10 | + type testCase struct { |
| 11 | + T time.Time |
| 12 | + Fmt string |
| 13 | + Expected string |
| 14 | + } |
| 15 | + |
| 16 | + t1 := time.Date(2016, time.February, 3, 13, 23, 45, 123, time.FixedZone("Plus2", 60*60*2)) |
| 17 | + t2 := time.Date(1945, time.September, 6, 7, 35, 4, 989, time.FixedZone("Minus5", 60*60*-5)) |
| 18 | + |
| 19 | + cases := []testCase{ |
| 20 | + {t1, "foo%nbar%tbaz 100%% cool", "foo\nbar\tbaz 100% cool"}, |
| 21 | + |
| 22 | + {t1, "%Y %y", "2016 16"}, |
| 23 | + {t1, "%G %g", "2016 16"}, |
| 24 | + {t1, "%b %B %m", "Feb February 02"}, |
| 25 | + {t1, "%V", "5"}, |
| 26 | + {t1, "%w", "3"}, |
| 27 | + {t1, "%j", "034"}, |
| 28 | + {t1, "%d", "03"}, |
| 29 | + {t1, "%e", " 3"}, |
| 30 | + {t1, "%a %A", "Wed Wednesday"}, |
| 31 | + {t1, "%H %I %l", "13 01 1"}, |
| 32 | + {t1, "%M", "23"}, |
| 33 | + {t1, "%S", "45"}, |
| 34 | + {t1, "%c", "03 Feb 16 13:23 Plus2"}, |
| 35 | + {t1, "%D %x", "02/03/16 02/03/16"}, |
| 36 | + {t1, "%F", "2016-02-03"}, |
| 37 | + {t1, "%r", "01:23:45 PM"}, |
| 38 | + {t1, "%R %T %X", "13:23 13:23:45 13:23:45"}, |
| 39 | + {t1, "%p %P", "PM pm"}, |
| 40 | + {t1, "%z %Z", "+0200 Plus2"}, |
| 41 | + |
| 42 | + {t2, "%Y %y", "1945 45"}, |
| 43 | + {t2, "%G %g", "1945 45"}, |
| 44 | + {t2, "%b %B %m", "Sep September 09"}, |
| 45 | + {t2, "%V", "36"}, |
| 46 | + {t2, "%w", "4"}, |
| 47 | + {t2, "%j", "249"}, |
| 48 | + {t2, "%d", "06"}, |
| 49 | + {t2, "%e", " 6"}, |
| 50 | + {t2, "%a %A", "Thu Thursday"}, |
| 51 | + {t2, "%H %I %l", "07 07 7"}, |
| 52 | + {t2, "%M", "35"}, |
| 53 | + {t2, "%S", "04"}, |
| 54 | + {t2, "%c", "06 Sep 45 07:35 Minus5"}, |
| 55 | + {t2, "%D %x", "09/06/45 09/06/45"}, |
| 56 | + {t2, "%F", "1945-09-06"}, |
| 57 | + {t2, "%r", "07:35:04 AM"}, |
| 58 | + {t2, "%R %T %X", "07:35 07:35:04 07:35:04"}, |
| 59 | + {t2, "%p %P", "AM am"}, |
| 60 | + {t2, "%z %Z", "-0500 Minus5"}, |
| 61 | + } |
| 62 | + for i, c := range cases { |
| 63 | + t.Run(fmt.Sprintf("Case %d (\"%s\")", i, c.Fmt), func(t *testing.T) { |
| 64 | + actual := strftime(c.T, c.Fmt) |
| 65 | + if actual != c.Expected { |
| 66 | + t.Errorf("bad strftime: expected \"%s\" but got \"%s\"", c.Expected, actual) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
0 commit comments