From 2f83424ce8a0653e6e158318df1511daecf1a42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20J=C3=B8rgensen?= Date: Fri, 24 May 2024 21:30:23 +0200 Subject: Time format rework --- model.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) (limited to 'model.go') diff --git a/model.go b/model.go index 0f52ef9..b6d5c79 100644 --- a/model.go +++ b/model.go @@ -1,11 +1,62 @@ package main -import "time" +import ( + "errors" + "regexp" + "time" +) -type Task struct { +type Entry struct { Id int - From *time.Time - To *time.Time + Date time.Time + From *Time + To *Time Tag *string Comment string } + +type Date string + +type Time string + +var timeMatch = regexp.MustCompile("\\d\\d:\\d\\d") +var dateMatch = regexp.MustCompile("\\d\\d\\d\\d-\\d\\d-\\d\\d") + +var ( + ErrInvalidTime = errors.New("Invalid time string") + ErrInvalidDate = errors.New("Invalid date string") +) + +func TimeFromStd(time time.Time) Time { + return Time(time.Format("15:04")) +} + +func TimeFromString(str string) (*Time, error) { + if str == "" { + return nil, nil + } + + matched := timeMatch.MatchString(str) + if !matched { + return nil, ErrInvalidTime + } + time := Time(str) + return &time, nil +} + +func DateFromStd(time time.Time) Date { + return Date(time.Format("2006-01-02")) +} + +func DateFromString(str string) (*Date, error) { + if str == "" { + return nil, nil + } + + matched := dateMatch.MatchString(str) + if !matched { + return nil, ErrInvalidTime + } + date := Date(str) + return &date, nil +} -- cgit v1.2.3