Skip to content

Commit

Permalink
wrap time
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Feb 18, 2017
1 parent a736142 commit 6a17e71
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
52 changes: 52 additions & 0 deletions todoist/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package todoist

import (
"strconv"
"time"
)

const layout = "Mon 2 Jan 2006 15:04:05 -0700"
const shortLayout = "2006-01-02(Mon) 15:04"

type Time struct {
time.Time
}

func Parse(value string) (Time, error) {
t, err := time.Parse(layout, value)
if err != nil {
return Time{}, err
}
return Time{t}, nil
}

func (t Time) Equal(u Time) bool {
return t.Time.Equal(u.Time)
}

func (t Time) MarshalJSON() ([]byte, error) {
if t.IsZero() {
return []byte("null"), nil
}
return []byte(strconv.Quote(t.Time.Format(layout))), nil
}

func (t *Time) UnmarshalJSON(b []byte) (err error) {
s, err := strconv.Unquote(string(b))
if err != nil {
*t = Time{time.Time{}} // null value
} else {
*t, err = Parse(s)
if err != nil {
return err
}
}
return nil
}

func (t *Time) ShortString() string {
if t.IsZero() {
return ""
}
return t.Time.Local().Format(shortLayout)
}
86 changes: 86 additions & 0 deletions todoist/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package todoist

import (
"encoding/json"
"reflect"
"strconv"
"testing"
"time"
)

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

func TestParse(t *testing.T) {
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)
}
}
}

func TestTime_MarshalJSON(t *testing.T) {
test := testTimes[0]
b, err := test.v.MarshalJSON()
if err != nil || string(b) != strconv.Quote(test.s) {
t.Errorf("Expect %s, but got %s", strconv.Quote(test.s), string(b))
}

b, err = Time{}.MarshalJSON()
if err != nil || string(b) != "null" {
t.Errorf("Expect %s, but got %s", strconv.Quote(test.s), string(b))
}
}

func TestTime_UnmarshalJSON(t *testing.T) {
for _, test := range testTimes {
var v Time
err := v.UnmarshalJSON([]byte(strconv.Quote(test.s)))
if !reflect.DeepEqual(err, test.e) {
t.Errorf("Expect %s, but got %s", test.e, err)
} else if test.e == nil && !v.Equal(test.v) {
t.Errorf("Expect %s, but got %s", test.v, v)
}
}
var v Time
err := v.UnmarshalJSON([]byte("null"))
if err != nil {
t.Errorf("Unexpect error: %s", err)
}
if !v.Equal(Time{}) {
t.Errorf("Expect %s, but got %s", Time{}, v)
}
}

func TestTimeJson(t *testing.T) {
j := []byte(`"Fri 26 Sep 2014 08:25:05 +0000"`)
v := Time{time.Date(2014, 9, 26, 8, 25, 5, 0, time.UTC)}

m, err := json.Marshal(v)
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if !reflect.DeepEqual(m, j) {
t.Errorf("mismatch:\n exp=%#v\n got=%#v\n\n", j, m)
}

var um Time
if err = json.Unmarshal(j, &um); err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if !um.Equal(v) {
t.Errorf("mismatch:\n exp=%#v\n got=%#v\n\n", v, um)
}
}

0 comments on commit 6a17e71

Please sign in to comment.