Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions cmd/history/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
logMilestone string
logToday bool
logWeek bool
logDate string
)

func LogCmd() *cobra.Command {
Expand Down Expand Up @@ -51,6 +52,16 @@ func LogCmd() *cobra.Command {
projectName = detectedProject
}
entries, err = db.GetEntriesByMilestone(projectName, logMilestone)
} else if logDate != "" {
parsedDate, err := parseDateFlag(logDate)
if err != nil {
ui.PrintError(ui.EmojiError, err.Error())
ui.NewlineBelow()
os.Exit(1)
}
start := time.Date(parsedDate.Year(), parsedDate.Month(), parsedDate.Day(), 0, 0, 0, 0, time.Local)
end := start.Add(24 * time.Hour)
entries, err = db.GetEntriesByDateRange(start, end)
} else if logToday {
year, month, day := time.Now().Year(), time.Now().Month(), time.Now().Day()
start := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
Expand Down Expand Up @@ -137,6 +148,40 @@ func LogCmd() *cobra.Command {
cmd.Flags().StringVarP(&logMilestone, "milestone", "m", "", "Filter by milestone")
cmd.Flags().BoolVarP(&logToday, "today", "t", false, "Show today's entries")
cmd.Flags().BoolVarP(&logWeek, "week", "w", false, "Show this week's entries")
cmd.Flags().StringVarP(&logDate, "date", "d", "", "Show entries for a specific date")

return cmd
}

func parseDateFlag(dateStr string) (time.Time, error) {
globalCfg, err := settings.LoadGlobalConfig()
if err != nil {
return time.Time{}, fmt.Errorf("loading config: %w", err)
}

layout := "2006-01-02"
displayFormat := "YYYY-MM-DD"

switch globalCfg.DateFormat {
case "MM/DD/YYYY":
layout = "01-02-2006"
displayFormat = "MM-DD-YYYY"
case "DD/MM/YYYY":
layout = "02-01-2006"
displayFormat = "DD-MM-YYYY"
}

parsedDate, err := time.ParseInLocation(layout, dateStr, time.Local)
if err == nil {
return parsedDate, nil
}

if layout != "2006-01-02" {
parsedDate, err = time.ParseInLocation("2006-01-02", dateStr, time.Local)
if err == nil {
return parsedDate, nil
}
}

return time.Time{}, fmt.Errorf("invalid date format. Please use %s or YYYY-MM-DD", displayFormat)
}
14 changes: 13 additions & 1 deletion cmd/history/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
var (
statsToday bool
statsWeek bool
statsDate string
)

func StatsCmd() *cobra.Command {
Expand All @@ -37,7 +38,17 @@ func StatsCmd() *cobra.Command {
var start, end time.Time
var periodName string

if statsToday {
if statsDate != "" {
parsedDate, err := parseDateFlag(statsDate)
if err != nil {
ui.PrintError(ui.EmojiError, err.Error())
ui.NewlineBelow()
os.Exit(1)
}
start = time.Date(parsedDate.Year(), parsedDate.Month(), parsedDate.Day(), 0, 0, 0, 0, parsedDate.Location()).UTC()
end = start.Add(24 * time.Hour)
periodName = parsedDate.Format("Jan 2, 2006")
} else if statsToday {
now := time.Now()
start = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).UTC()
end = start.Add(24 * time.Hour)
Expand Down Expand Up @@ -75,6 +86,7 @@ func StatsCmd() *cobra.Command {

cmd.Flags().BoolVarP(&statsToday, "today", "t", false, "Show today's stats")
cmd.Flags().BoolVarP(&statsWeek, "week", "w", false, "Show this week's stats")
cmd.Flags().StringVarP(&statsDate, "date", "d", "", "Show stats for a specific date")

return cmd
}
Expand Down
Loading