Skip to content

Commit

Permalink
date: improve parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <me@sumnerevans.com>
  • Loading branch information
sumnerevans committed Jun 17, 2023
1 parent 76e3afb commit 0be59e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
38 changes: 26 additions & 12 deletions lib/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lib

import (
"fmt"
"strings"
"time"
)

Expand All @@ -22,21 +23,34 @@ func Today() Date {
}

func (d *Date) UnmarshalText(text []byte) error {
switch string(text) {
now := time.Now().Local()
switch strings.ToLower(string(text)) {
case "today":
d.Time = time.Now().Local()
d.Time = now
case "yesterday":
d.Time = time.Now().Local().AddDate(0, 0, -1)
d.Time = now.AddDate(0, 0, -1)
case "monday":
if now.Weekday() == time.Monday {
d.Time = now
} else {
d.Time = now.AddDate(0, 0, int(time.Monday-now.Weekday()))
}
case "tuesday":
if now.Weekday() == time.Tuesday {
d.Time = now
} else {
d.Time = now.AddDate(0, 0, int(time.Tuesday-now.Weekday()))
}
default:
// formatsWithoutYear := []string{"01", "January", "Jan", "1"}
// for _, format := range formats {
// parsed, err := time.Parse(format, string(text))
// if err == nil {
// d.year = now.Year()
// d.month = parsed.Month()
// return nil
// }
// }
formatsWithoutYear := []string{"02", "2"}
for _, format := range formatsWithoutYear {
parsed, err := time.Parse(format, string(text))
if err == nil {
fmt.Printf("Parsed %v\n", parsed)
d.Time = time.Date(now.Year(), now.Month(), parsed.Day(), 0, 0, 0, 0, time.Local)
return nil
}
}
return fmt.Errorf("invalid date '%s'", string(text))
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion lib/entrylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (el *EntryList) TotalTimeForCustomer(customer Customer) time.Duration {
}

func (el *EntryList) AddEntry(entry *TimeEntry) {
insertIdx := len(el.entries) + 1
insertIdx := len(el.entries)
for i, e := range el.entries {
if e.Stop != nil && entry.Start.Between(e.Start, e.Stop) {
// The entry is being started in the middle of this one
Expand Down

0 comments on commit 0be59e8

Please sign in to comment.