Skip to content

Commit

Permalink
due date can have time
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Sep 23, 2019
1 parent 47c558c commit 46d5dde
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 12 additions & 3 deletions todoist/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
)

const (
localLayout = "2006-01-02(Mon) 15:04"
dateLayout = "2006-01-02"
datetimeLayout = time.RFC3339
localLayout = "2006-01-02(Mon) 15:04"
)

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

func Parse(value string) (Time, error) {
t, err := time.Parse(time.RFC3339, value)
t, err := time.Parse(datetimeLayout, value)
if err != nil {
t, err = time.Parse(dateLayout, value)
}
if err != nil {
return Time{}, err
}
Expand All @@ -54,7 +59,11 @@ func (t Time) MarshalJSON() ([]byte, error) {
if t.IsZero() {
return []byte("null"), nil
}
return []byte(strconv.Quote(t.Time.Format(time.RFC3339))), nil
layout := datetimeLayout
if t.Hour() == 0 && t.Minute() == 0 && t.Second() == 0 {
layout = dateLayout
}
return []byte(strconv.Quote(t.Time.Format(layout))), nil
}

func (t *Time) UnmarshalJSON(b []byte) (err error) {
Expand Down
5 changes: 5 additions & 0 deletions todoist/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ var testTimes = []struct {
v: Time{time.Date(2014, 9, 26, 8, 25, 5, 0, time.UTC)},
e: nil,
},
{
s: "2014-09-26",
v: Time{time.Date(2014, 9, 26, 0,0,0,0,time.UTC)},
e: nil,
},
}

func TestParse(t *testing.T) {
Expand Down

0 comments on commit 46d5dde

Please sign in to comment.