From 490ba2ea50f950c26dba02cbf3f37bbc1ade0b34 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 4 Feb 2021 21:06:01 +0100 Subject: [PATCH] Fix due dates with times when migrating from todoist --- pkg/modules/migration/todoist/todoist.go | 24 ++++++++------- pkg/modules/migration/todoist/todoist_test.go | 29 +++++++++++++++++-- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/pkg/modules/migration/todoist/todoist.go b/pkg/modules/migration/todoist/todoist.go index 8f8311c6..ff7d4cf5 100644 --- a/pkg/modules/migration/todoist/todoist.go +++ b/pkg/modules/migration/todoist/todoist.go @@ -226,6 +226,18 @@ func (m *Migration) AuthURL() string { "&state=" + utils.MakeRandomString(32) } +func parseDate(dateString string) (date time.Time, err error) { + date, err = time.Parse("2006-01-02T15:04:05Z", dateString) + if err != nil { + date, err = time.Parse("2006-01-02T15:04:05", dateString) + } + if err != nil { + date, err = time.Parse("2006-01-02", dateString) + } + + return date, err +} + func convertTodoistToVikunja(sync *sync) (fullVikunjaHierachie []*models.NamespaceWithLists, err error) { newNamespace := &models.NamespaceWithLists{ @@ -299,7 +311,7 @@ func convertTodoistToVikunja(sync *sync) (fullVikunjaHierachie []*models.Namespa // Put the due date together if i.Due != nil { - dueDate, err := time.Parse("2006-01-02", i.Due.Date) + dueDate, err := parseDate(i.Due.Date) if err != nil { return nil, err } @@ -408,15 +420,7 @@ func convertTodoistToVikunja(sync *sync) (fullVikunjaHierachie []*models.Namespa log.Debugf("Could not find task %d for reminder %d while trying to resolve reminders", r.ItemID, r.ID) } - var err error - var date time.Time - date, err = time.Parse("2006-01-02T15:04:05Z", r.Due.Date) - if err != nil { - date, err = time.Parse("2006-01-02T15:04:05", r.Due.Date) - } - if err != nil { - date, err = time.Parse("2006-01-02", r.Due.Date) - } + date, err := parseDate(r.Due.Date) if err != nil { return nil, err } diff --git a/pkg/modules/migration/todoist/todoist_test.go b/pkg/modules/migration/todoist/todoist_test.go index fc8ae551..dd308528 100644 --- a/pkg/modules/migration/todoist/todoist_test.go +++ b/pkg/modules/migration/todoist/todoist_test.go @@ -42,6 +42,9 @@ func TestConvertTodoistToVikunja(t *testing.T) { dueTime, err := time.Parse(time.RFC3339Nano, "2020-05-31T00:00:00Z") assert.NoError(t, err) dueTime = dueTime.In(config.GetTimeZone()) + dueTimeWithTime, err := time.Parse(time.RFC3339Nano, "2021-01-31T19:00:00Z") + assert.NoError(t, err) + dueTimeWithTime = dueTimeWithTime.In(config.GetTimeZone()) nilTime, err := time.Parse(time.RFC3339Nano, "0001-01-01T00:00:00Z") assert.NoError(t, err) exampleFile, err := ioutil.ReadFile(config.ServiceRootpath.GetString() + "/pkg/modules/migration/wunderlist/testimage.jpg") @@ -140,7 +143,29 @@ func TestConvertTodoistToVikunja(t *testing.T) { Checked: 0, DateAdded: time1, }, - makeTestItem(400000106, 396936926, true, true, true), + { + ID: 400000106, + UserID: 1855589, + ProjectID: 396936926, + Content: "Task400000106", + Priority: 1, + ParentID: 0, + ChildOrder: 1, + DateAdded: time1, + Checked: 1, + DateCompleted: time3, + Due: &dueDate{ + Date: "2021-01-31T19:00:00Z", + Timezone: nil, + IsRecurring: false, + }, + Labels: []int64{ + 80000, + 80001, + 80002, + 80003, + }, + }, makeTestItem(400000107, 396936926, false, false, true), makeTestItem(400000108, 396936926, false, false, true), { @@ -440,7 +465,7 @@ func TestConvertTodoistToVikunja(t *testing.T) { { Title: "Task400000106", Done: true, - DueDate: dueTime, + DueDate: dueTimeWithTime, Created: time1, DoneAt: time3, Labels: vikunjaLabels,