diff --git a/cmd/web/templates.go b/cmd/web/templates.go index ba74078..d43562f 100644 --- a/cmd/web/templates.go +++ b/cmd/web/templates.go @@ -23,7 +23,10 @@ type templateData struct { } func humanDate(t time.Time) string { - return t.Format("02 Jan 2006 at 15:04") + if t.IsZero() { + return "" + } + return t.UTC().Format("02 Jan 2006 at 15:04") } var functions = template.FuncMap{ diff --git a/cmd/web/templates_test.go b/cmd/web/templates_test.go new file mode 100644 index 0000000..35e35c8 --- /dev/null +++ b/cmd/web/templates_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "testing" + "time" + + "gitea.local.lab/Lbenedar/snippetbox/internal/assert" +) + +func TestHumanDate(t *testing.T) { + tests := []struct { + name string + tm time.Time + want string + }{ + { + name: "UTC", + tm: time.Date(2022, 3, 17, 10, 15, 0, 0, time.UTC), + want: "17 Mar 2022 at 10:15", + }, + { + name: "Empty", + tm: time.Time{}, + want: "", + }, + { + name: "CET", + tm: time.Date(2022, 3, 17, 10, 15, 0, 0, time.FixedZone("CET", 1*60*60)), + want: "17 Mar 2022 at 09:15", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + hd := humanDate(tt.tm) + assert.Equal(t, hd, tt.want) + }) + } +} diff --git a/internal/assert/assert.go b/internal/assert/assert.go new file mode 100644 index 0000000..e69de29