Add timezone support for front matter dates without one

Fixes #8810
This commit is contained in:
Bjørn Erik Pedersen
2021-07-27 13:45:05 +02:00
parent a57dda854b
commit efa5760db5
10 changed files with 196 additions and 73 deletions

View File

@@ -23,14 +23,16 @@ import (
func TestTimeLocation(t *testing.T) {
t.Parallel()
ns := New(translators.Get("en"))
loc, _ := time.LoadLocation("America/Antigua")
ns := New(translators.Get("en"), loc)
for i, test := range []struct {
value string
location string
location interface{}
expect interface{}
}{
{"2020-10-20", "", "2020-10-20 00:00:00 +0000 UTC"},
{"2020-10-20", nil, "2020-10-20 00:00:00 -0400 AST"},
{"2020-10-20", "America/New_York", "2020-10-20 00:00:00 -0400 EDT"},
{"2020-01-20", "America/New_York", "2020-01-20 00:00:00 -0500 EST"},
{"2020-10-20 20:33:59", "", "2020-10-20 20:33:59 +0000 UTC"},
@@ -41,7 +43,11 @@ func TestTimeLocation(t *testing.T) {
{"2020-01-20", "invalid-timezone", false}, // unknown time zone invalid-timezone
{"invalid-value", "", false},
} {
result, err := ns.AsTime(test.value, test.location)
var args []interface{}
if test.location != nil {
args = append(args, test.location)
}
result, err := ns.AsTime(test.value, args...)
if b, ok := test.expect.(bool); ok && !b {
if err == nil {
t.Errorf("[%d] AsTime didn't return an expected error, got %v", i, result)
@@ -61,7 +67,7 @@ func TestTimeLocation(t *testing.T) {
func TestFormat(t *testing.T) {
t.Parallel()
ns := New(translators.Get("en"))
ns := New(translators.Get("en"), time.UTC)
for i, test := range []struct {
layout string
@@ -101,7 +107,7 @@ func TestFormat(t *testing.T) {
func TestDuration(t *testing.T) {
t.Parallel()
ns := New(translators.Get("en"))
ns := New(translators.Get("en"), time.UTC)
for i, test := range []struct {
unit interface{}