Skip to content

Commit

Permalink
internal: use http.TimeFormat to marshal Time values
Browse files Browse the repository at this point in the history
  • Loading branch information
sbinet authored Mar 16, 2021
1 parent ed52608 commit 8efde26
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
25 changes: 25 additions & 0 deletions internal/elements_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package internal

import (
"bytes"
"encoding/xml"
"strings"
"testing"
"time"
)

// https://tools.ietf.org/html/rfc4918#section-9.6.2
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 8efde26

Please sign in to comment.