Moving since/until parsing to helpers and add Range to it

This commit is contained in:
Martin (Schretzi) Fuchsluger
2025-02-14 20:03:35 +01:00
parent 4169bbc325
commit d5c41a921a
4 changed files with 72 additions and 41 deletions
+2 -21
View File
@@ -3,9 +3,7 @@ package z
import (
"os"
"fmt"
"time"
"encoding/json"
"github.com/jinzhu/now"
"github.com/spf13/cobra"
)
@@ -44,25 +42,7 @@ var exportCmd = &cobra.Command{
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
var sinceTime time.Time
var untilTime time.Time
if since != "" {
sinceTime, err = now.Parse(since)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
if until != "" {
untilTime, err = now.Parse(until)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
sinceTime, untilTime := ParseSinceUntil(since, until, listRange)
var filteredEntries []Entry
filteredEntries, err = GetFilteredEntries(entries, project, task, sinceTime, untilTime)
@@ -100,6 +80,7 @@ func init() {
exportCmd.Flags().StringVar(&format, "format", "zeit", "Format to export, possible values: zeit, tyme")
exportCmd.Flags().StringVar(&since, "since", "", "Date/time to start the export from")
exportCmd.Flags().StringVar(&until, "until", "", "Date/time to export until")
exportCmd.Flags().StringVar(&listRange, "range", "", "shortcut to set since/until for a given range (today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth)")
exportCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be exported")
exportCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be exported")
}
+67
View File
@@ -10,9 +10,12 @@ import (
"time"
"math"
"errors"
"fmt"
"os"
"github.com/araddon/dateparse"
"github.com/spf13/viper"
"github.com/jinzhu/now"
)
@@ -193,3 +196,67 @@ func GetGitLog(repo string, since time.Time, until time.Time) (string, string, e
return stdoutStr, stderrStr, nil
}
func ParseSinceUntil(since string, until string, listRange string) (time.Time, time.Time) {
var sinceTime time.Time
var untilTime time.Time
var err error
if since != "" {
sinceTime, err = now.Parse(since)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
if until != "" {
untilTime, err = now.Parse(until)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
if listRange != "" {
if since != "" || until != "" {
fmt.Println("Range and since/until can't be used together, select one of them")
os.Exit(1)
}
if viper.GetBool("firstWeekDayMonday") {
now.WeekStartDay = time.Monday
}
loc, _ := time.LoadLocation("Local")
time.Local = loc
switch listRange {
case "today":
sinceTime = now.BeginningOfDay()
untilTime = now.EndOfDay()
case "yesterday":
sinceTime = now.BeginningOfDay().AddDate(0, 0, -1)
untilTime = now.EndOfDay().AddDate(0, 0, -1)
case "thisWeek":
sinceTime = now.BeginningOfWeek()
untilTime = now.EndOfWeek()
case "lastWeek":
lastWeekDay := time.Now().AddDate(0, 0, -7)
sinceTime = now.With(lastWeekDay).BeginningOfWeek()
untilTime = now.With(lastWeekDay).EndOfWeek()
case "thisMonth":
sinceTime = now.BeginningOfMonth()
untilTime = now.EndOfMonth()
case "lastMonth":
lastMonthDay := time.Now().AddDate(0, -1, 0)
sinceTime = now.With(lastMonthDay).BeginningOfMonth()
untilTime = now.With(lastMonthDay).EndOfMonth()
default:
fmt.Println("unkown range selection: (today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth)")
os.Exit(1)
}
}
return sinceTime, untilTime
}
+2 -20
View File
@@ -3,9 +3,7 @@ package z
import (
"fmt"
"os"
"time"
"github.com/jinzhu/now"
"github.com/shopspring/decimal"
"github.com/spf13/cobra"
)
@@ -29,24 +27,7 @@ var listCmd = &cobra.Command{
os.Exit(1)
}
var sinceTime time.Time
var untilTime time.Time
if since != "" {
sinceTime, err = now.Parse(since)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
if until != "" {
untilTime, err = now.Parse(until)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
sinceTime, untilTime := ParseSinceUntil(since, until, listRange)
var filteredEntries []Entry
filteredEntries, err = GetFilteredEntries(entries, project, task, sinceTime, untilTime)
@@ -108,6 +89,7 @@ func init() {
rootCmd.AddCommand(listCmd)
listCmd.Flags().StringVar(&since, "since", "", "Date/time to start the list from")
listCmd.Flags().StringVar(&until, "until", "", "Date/time to list until")
listCmd.Flags().StringVar(&listRange, "range", "", "shortcut to set since/until for a given range (today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth)")
listCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be listed")
listCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be listed")
listCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
+1
View File
@@ -18,6 +18,7 @@ var notes string
var since string
var until string
var listRange string
var format string
var force bool