fix(migration): properly parse duration

This commit is contained in:
kolaente 2022-10-09 19:03:00 +02:00
parent 5871d32c2d
commit 5e40f4ec89
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -19,6 +19,7 @@ package ticktick
import ( import (
"encoding/csv" "encoding/csv"
"io" "io"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -54,6 +55,32 @@ type tickTickTask struct {
ParentID int64 ParentID int64
} }
// Copied from https://stackoverflow.com/a/57617885
var durationRegex = regexp.MustCompile(`P([\d\.]+Y)?([\d\.]+M)?([\d\.]+D)?T?([\d\.]+H)?([\d\.]+M)?([\d\.]+?S)?`)
// ParseDuration converts a ISO8601 duration into a time.Duration
func parseDuration(str string) time.Duration {
matches := durationRegex.FindStringSubmatch(str)
years := parseDurationPart(matches[1], time.Hour*24*365)
months := parseDurationPart(matches[2], time.Hour*24*30)
days := parseDurationPart(matches[3], time.Hour*24)
hours := parseDurationPart(matches[4], time.Hour)
minutes := parseDurationPart(matches[5], time.Second*60)
seconds := parseDurationPart(matches[6], time.Second)
return time.Duration(years + months + days + hours + minutes + seconds)
}
func parseDurationPart(value string, unit time.Duration) time.Duration {
if len(value) != 0 {
if parsed, err := strconv.ParseFloat(value[:len(value)-1], 64); err == nil {
return time.Duration(float64(unit) * parsed)
}
}
return 0
}
func convertTickTickToVikunja(tasks []*tickTickTask) (result []*models.NamespaceWithListsAndTasks) { func convertTickTickToVikunja(tasks []*tickTickTask) (result []*models.NamespaceWithListsAndTasks) {
namespace := &models.NamespaceWithListsAndTasks{ namespace := &models.NamespaceWithListsAndTasks{
Namespace: models.Namespace{ Namespace: models.Namespace{
@ -163,11 +190,6 @@ func (m *Migrator) Migrate(user *user.User, file io.ReaderAt, size int64) error
if err != nil { if err != nil {
return err return err
} }
// TODO: parse properly
reminder, err := time.ParseDuration(record[8])
if err != nil {
return err
}
priority, err := strconv.Atoi(record[10]) priority, err := strconv.Atoi(record[10])
if err != nil { if err != nil {
return err return err
@ -193,6 +215,8 @@ func (m *Migrator) Migrate(user *user.User, file io.ReaderAt, size int64) error
return err return err
} }
reminder := parseDuration(record[8])
allTasks = append(allTasks, &tickTickTask{ allTasks = append(allTasks, &tickTickTask{
ListName: record[1], ListName: record[1],
Title: record[2], Title: record[2],