internal: use http.TimeFormat to marshal Time values

This commit is contained in:
Sebastien Binet 2021-03-16 18:42:55 +01:00 committed by GitHub
parent ed52608852
commit 8efde26ef9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -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
}

View File

@ -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)
}
}