Skip to content

Commit

Permalink
add due struct
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Feb 24, 2020
1 parent 34d04b8 commit 04cd951
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 48 deletions.
41 changes: 18 additions & 23 deletions todoist/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,24 @@ import (

type Item struct {
Entity
UserID ID `json:"user_id,omitempty"`
ProjectID ID `json:"project_id,omitempty"`
Content string `json:"content"`
Due struct {
Date Time `json:"date"`
Timezone string `json:"timezone"`
IsRecurring bool `json:"is_recurring"`
String string `json:"string"`
Lang string `json:"lang"`
} `json:"due,omitempty"`
Priority int `json:"priority,omitempty"`
ParentID ID `json:"parent_id,omitempty"`
ChildOrder int `json:"child_order,omitempty"`
DayOrder int `json:"day_order,omitempty"`
Collapsed int `json:"collapsed,omitempty"`
Labels []ID `json:"labels,omitempty"`
AssignedByUID ID `json:"assigned_by_uid,omitempty"`
ResponsibleUID ID `json:"responsible_uid,omitempty"`
Checked int `json:"checked,omitempty"`
InHistory int `json:"in_history,omitempty"`
SyncID int `json:"sync_id,omitempty"`
DateAdded Time `json:"date_added,omitempty"`
CompletedDate Time `json:"completed_date"`
UserID ID `json:"user_id,omitempty"`
ProjectID ID `json:"project_id,omitempty"`
Content string `json:"content"`
Due Due `json:"due,omitempty"`
Priority int `json:"priority,omitempty"`
ParentID ID `json:"parent_id,omitempty"`
ChildOrder int `json:"child_order,omitempty"`
DayOrder int `json:"day_order,omitempty"`
Collapsed IntBool `json:"collapsed,omitempty"`
Labels []ID `json:"labels,omitempty"`
AssignedByUID ID `json:"assigned_by_uid,omitempty"`
ResponsibleUID ID `json:"responsible_uid,omitempty"`
Checked IntBool `json:"checked,omitempty"`
InHistory IntBool `json:"in_history,omitempty"`
SyncID int `json:"sync_id,omitempty"`
DateAdded Time `json:"date_added,omitempty"`
CompletedDate Time `json:"completed_date"`
}
}

func (i Item) IsOverDueDate() bool {
Expand Down
16 changes: 5 additions & 11 deletions todoist/reminder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ package todoist

type Reminder struct {
Entity
NotifyUID ID `json:"notify_uid"`
ItemID ID `json:"item_id"`
Service string `json:"service"`
Type string `json:"type"`
Due struct {
Date Time `json:"date"`
Timezone string `json:"timezone"`
IsRecurring bool `json:"is_recurring"`
String string `json:"string"`
Lang string `json:"lang"`
} `json:"due"`
NotifyUID ID `json:"notify_uid"`
ItemID ID `json:"item_id"`
Service string `json:"service"`
Type string `json:"type"`
Due Due `json:"due"`
MmOffset int `json:"mm_offset"`
Name string `json:"name"`
LocLat string `json:"loc_lat"`
Expand Down
52 changes: 42 additions & 10 deletions todoist/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,28 @@ import (
)

const (
dateLayout = "2006-01-02"
datetimeLayout = time.RFC3339
localLayout = "2006-01-02(Mon) 15:04"
dateLayout = "2006-01-02"
datetimeLayout = "2006-01-02T15:04:05"
datetimeTzLayout = time.RFC3339
localDateLayout = "2006-01-02(Mon)"
localDatetimeLayout = "2006-01-02(Mon) 15:04"
)

/*
Due#date has 3 formats.
- Full-day dates: date=YYYY-MM-DD, timezone=null
- Floating due dates: date=YYYY-MM-DDTHH:MM:SS, timezone=null (use current user's timezone)
- Due dates: date=YYYY-MM-DDTHH:MM:SSZ, timezone=Europe/Madrid
*/

type Due struct {
Date Time `json:"date"`
Timezone string `json:"timezone"`
String string `json:"string"`
Lang string `json:"lang"`
IsRecurring bool `json:"is_recurring"`
}

type Time struct {
time.Time
}
Expand All @@ -29,11 +46,15 @@ func Next7Days() Time {
}

func Parse(value string) (Time, error) {
t, err := time.Parse(datetimeLayout, value)
if err != nil {
t, err = time.Parse(dateLayout, value)
var t time.Time
var err error
for _, layout := range []string{dateLayout, datetimeLayout} {
// FIXME: refer to user.tz_info.timezone
if t, err = time.ParseInLocation(layout, value, time.Local); err == nil {
return Time{t}, nil
}
}
if err != nil {
if t, err = time.Parse(datetimeTzLayout, value); err != nil {
return Time{}, err
}
return Time{t}, nil
Expand All @@ -55,14 +76,21 @@ func (t Time) Local() Time {
return Time{t.Time.Local()}
}

func (t Time) IsFullDay() bool {
return t.Hour() == 0 && t.Minute() == 0 && t.Second() == 0
}

func (t Time) MarshalJSON() ([]byte, error) {
if t.IsZero() {
return []byte("null"), nil
}
layout := datetimeLayout
if t.Hour() == 0 && t.Minute() == 0 && t.Second() == 0 {
if t.IsFullDay() {
layout = dateLayout
}
if t.Time.Location() == time.UTC {
layout = datetimeTzLayout
}
return []byte(strconv.Quote(t.Time.Format(layout))), nil
}

Expand All @@ -83,11 +111,15 @@ func (t Time) String() string {
if t.IsZero() {
return ""
}
return t.Time.Local().Format(localLayout)
layout := localDatetimeLayout
if t.IsFullDay() {
layout = localDateLayout
}
return t.Time.Local().Format(layout)
}

func (t Time) ColorString() string {
if !t.IsZero() && t.Before(Time{time.Now()}) {
if !t.IsZero() && t.Before(Time{time.Now()}) && !t.IsFullDay() {
return color.New(color.BgRed).Sprint(t.String())
}
return t.String()
Expand Down
13 changes: 9 additions & 4 deletions todoist/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ var testTimes = []struct {
e error
}{
{
s: "2014-09-26T08:25:05Z",
v: Time{time.Date(2014, 9, 26, 8, 25, 5, 0, time.UTC)},
s: "2014-09-26",
v: Time{time.Date(2014, 9, 26, 0, 0, 0, 0, time.Local)},
e: nil,
},
{
s: "2014-09-26",
v: Time{time.Date(2014, 9, 26, 0,0,0,0,time.UTC)},
s: "2014-09-26T08:25:05",
v: Time{time.Date(2014, 9, 26, 8, 25, 5, 0, time.Local)},
e: nil,
},
{
s: "2014-09-26T08:25:05Z",
v: Time{time.Date(2014, 9, 26, 8, 25, 5, 0, time.UTC)},
e: nil,
},
}
Expand Down

0 comments on commit 04cd951

Please sign in to comment.