diff options
author | Teddy Wing | 2017-03-12 21:59:58 +0100 |
---|---|---|
committer | Teddy Wing | 2017-03-12 21:59:58 +0100 |
commit | ca1e693cbde486d4a8f62bc6b41f72fc3922c299 (patch) | |
tree | 64e12e6c2a65f3c20e37f1fefca2446bc9976cbb /timetask | |
parent | 4b1de1d7bfb0261d28a192171b73388cb639eae4 (diff) | |
download | timetasker-ca1e693cbde486d4a8f62bc6b41f72fc3922c299.tar.bz2 |
GenerateWeeklyTimesheet: Auto-fill the date with all days in the week
Get Monday's date from the current week using the When library (which
provides natural language date parsing, making it super easy to get a
time object for Monday). Then when creating the `TimeEntry`ies for the
generator, fill in Monday–Friday's dates in the output.
Diffstat (limited to 'timetask')
-rw-r--r-- | timetask/generator.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/timetask/generator.go b/timetask/generator.go index 25986ae..5d0fa7f 100644 --- a/timetask/generator.go +++ b/timetask/generator.go @@ -4,12 +4,29 @@ import ( "io" "log" "text/template" + "time" + + "github.com/olebedev/when" + "github.com/olebedev/when/rules/common" + "github.com/olebedev/when/rules/en" ) func GenerateWeeklyTimesheet(wr io.Writer, defaults TimeEntry) { + w := when.New(nil) + w.Add(en.All...) + w.Add(common.All...) + + monday, err := w.Parse("last monday", time.Now()) + if err != nil { + log.Panic(err) + } + time_entries := []TimeEntry{} + day := monday.Time for i := 1; i <= 5; i++ { time_entries = append(time_entries, defaults) + time_entries[len(time_entries) - 1].Date = day + day = day.AddDate(0, 0, 1) // Add 1 day } t, err := template.ParseFiles( |