From 8efde26ef9fd73acccd60b443e4bb7c02da5e6f0 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Tue, 16 Mar 2021 18:42:55 +0100 Subject: [PATCH] internal: use http.TimeFormat to marshal Time values --- internal/elements.go | 2 +- internal/elements_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/internal/elements.go b/internal/elements.go index f5df41d..eb76842 100644 --- a/internal/elements.go +++ b/internal/elements.go @@ -321,7 +321,7 @@ func (t *Time) UnmarshalText(b []byte) error { } func (t *Time) MarshalText() ([]byte, error) { - s := time.Time(*t).Format(time.RFC1123Z) + s := time.Time(*t).UTC().Format(http.TimeFormat) return []byte(s), nil } diff --git a/internal/elements_test.go b/internal/elements_test.go index 4308833..6bece02 100644 --- a/internal/elements_test.go +++ b/internal/elements_test.go @@ -1,9 +1,11 @@ package internal import ( + "bytes" "encoding/xml" "strings" "testing" + "time" ) // https://tools.ietf.org/html/rfc4918#section-9.6.2 @@ -32,3 +34,26 @@ func TestMultistatus_Get_error(t *testing.T) { t.Errorf("HTTPError.Code = %v, expected 423", httpErr.Code) } } + +func TestTimeRoundTrip(t *testing.T) { + now := Time(time.Now().UTC()) + want, err := now.MarshalText() + if err != nil { + t.Fatalf("could not marshal time: %+v", err) + } + + var got Time + err = got.UnmarshalText(want) + if err != nil { + t.Fatalf("could not unmarshal time: %+v", err) + } + + raw, err := got.MarshalText() + if err != nil { + t.Fatalf("could not marshal back: %+v", err) + } + + if got, want := raw, want; !bytes.Equal(got, want) { + t.Fatalf("invalid round-trip:\ngot= %s\nwant=%s", got, want) + } +}