Skip to content

Commit

Permalink
change time format to rfc3339
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Sep 10, 2019
1 parent fa5ef9e commit 51f22ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 36 deletions.
11 changes: 3 additions & 8 deletions todoist/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
)

const (
marshalLayout = "2006-01-02T15:04"
unmarshalLayout = "Mon 2 Jan 2006 15:04:05 -0700"
localLayout = "2006-01-02(Mon) 15:04"
localLayout = "2006-01-02(Mon) 15:04"
)

type Time struct {
Expand All @@ -29,10 +27,7 @@ func Next7Days() Time {
}

func Parse(value string) (Time, error) {
t, err := time.Parse(unmarshalLayout, value)
if err != nil {
t, err = time.Parse(marshalLayout, value)
}
t, err := time.Parse(time.RFC3339, value)
if err != nil {
return Time{}, err
}
Expand All @@ -59,7 +54,7 @@ func (t Time) MarshalJSON() ([]byte, error) {
if t.IsZero() {
return []byte("null"), nil
}
return []byte(strconv.Quote(t.Time.Format(marshalLayout))), nil
return []byte(strconv.Quote(t.Time.Format(time.RFC3339))), nil
}

func (t *Time) UnmarshalJSON(b []byte) (err error) {
Expand Down
34 changes: 6 additions & 28 deletions todoist/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,31 @@ import (
"time"
)

var marshalTimes = []struct {
var testTimes = []struct {
s string
v Time
e error
}{
{
s: "2014-09-26T08:25",
v: Time{time.Date(2014, 9, 26, 8, 25, 5, 0, time.UTC)},
e: nil,
},
}

var unmarshalTimes = []struct {
s string
v Time
e error
}{
{
s: "Fri 26 Sep 2014 08:25:05 +0000",
s: "2014-09-26T08:25:05Z",
v: Time{time.Date(2014, 9, 26, 8, 25, 5, 0, time.UTC)},
e: nil,
},
}

func TestParse(t *testing.T) {
for i, tt := range unmarshalTimes {
for i, tt := range testTimes {
tim, err := Parse(tt.s)
if !reflect.DeepEqual(err, tt.e) {
t.Errorf("%d. %q error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.e, err)
} else if tt.e == nil && !tim.Equal(tt.v) {
t.Errorf("%d. %q mismatch:\n exp=%#v\n got=%#v\n\n", i, tt.s, tt.v, tim)
}
}
for i, tt := range marshalTimes {
tim, err := Parse(tt.s)
if !reflect.DeepEqual(err, tt.e) {
t.Errorf("%d. %q error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.e, err)
} else if tt.e == nil && !tim.Equal(Time{tt.v.Truncate(time.Minute)}) {
t.Errorf("%d. %q mismatch:\n exp=%#v\n got=%#v\n\n", i, tt.s, tt.v, tim)
}
}
}

func TestTime_MarshalJSON(t *testing.T) {
for _, tt := range marshalTimes {
for _, tt := range testTimes {
b, err := tt.v.MarshalJSON()
if err != nil || string(b) != strconv.Quote(tt.s) {
t.Errorf("Expect %s, but got %s", strconv.Quote(tt.s), string(b))
Expand All @@ -65,7 +45,7 @@ func TestTime_MarshalJSON(t *testing.T) {
}

func TestTime_UnmarshalJSON(t *testing.T) {
for _, test := range unmarshalTimes {
for _, test := range testTimes {
var v Time
err := v.UnmarshalJSON([]byte(strconv.Quote(test.s)))
if !reflect.DeepEqual(err, test.e) {
Expand All @@ -85,17 +65,15 @@ func TestTime_UnmarshalJSON(t *testing.T) {
}

func TestTimeJson(t *testing.T) {
for _, tt := range marshalTimes {
for _, tt := range testTimes {
m, err := json.Marshal(tt.v)
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if !reflect.DeepEqual(string(m), strconv.Quote(tt.s)) {
t.Errorf("mismatch:\n exp=%#v\n got=%#v\n\n", strconv.Quote(tt.s), string(m))
}
}

for _, tt := range unmarshalTimes {
var um Time
if err := json.Unmarshal([]byte(strconv.Quote(tt.s)), &um); err != nil {
t.Errorf("unexpected error: %s", err.Error())
Expand Down

0 comments on commit 51f22ee

Please sign in to comment.