package main import ( "errors" "log" "regexp" "time" ) type Entry struct { Id int Date Date From *Time To *Time Tag *string Comment string } type Task struct { Id int Group *string Ident *string Text 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 NewEntry(date Date) *Entry { return &Entry{ Id: -1, From: nil, Date: date, To: nil, Tag: nil, Comment: "", } } 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 } func (d Date) ToStd() time.Time { t, err := time.Parse("2006-01-02", string(d)) if err != nil { log.Fatal(err) } return t }