From 3d9c7be5fc5df0d259a3faca8976f42c38d83845 Mon Sep 17 00:00:00 2001 From: Gabor Boros Date: Mon, 11 Oct 2021 10:15:50 +0200 Subject: [PATCH] fix: solve time parsing issue when start or end date is defined --- cmd/root.go | 9 ++++++++- internal/cmd/utils/utils.go | 12 +++--------- internal/cmd/utils/utils_test.go | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b61c6e5..0d17dbc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,6 +9,7 @@ import ( "os" "regexp" "strings" + "time" "github.com/gabor-boros/minutes/internal/cmd/printer" "github.com/gabor-boros/minutes/internal/cmd/utils" @@ -297,9 +298,15 @@ func runRootCmd(_ *cobra.Command, _ []string) { start, err := utils.GetTime(viper.GetString("start"), dateFormat) cobra.CheckErr(err) - end, err := utils.GetTime(viper.GetString("end"), dateFormat) + rawEnd := viper.GetString("end") + end, err := utils.GetTime(rawEnd, dateFormat) cobra.CheckErr(err) + // No end date was set, hence we are setting the end date to next day midnight + if rawEnd == "" { + end = end.Add(time.Hour * 24) + } + fetcher, err := getFetcher() cobra.CheckErr(err) diff --git a/internal/cmd/utils/utils.go b/internal/cmd/utils/utils.go index cdc3472..a0e504b 100644 --- a/internal/cmd/utils/utils.go +++ b/internal/cmd/utils/utils.go @@ -53,18 +53,12 @@ func Prompt(message string) string { } // GetTime parses a string based on the given format and returns the time. -// FIXME: End and Start dates have the same value -// FIXME: Returns the UTC time, not local time +// If the rawDate was an empty string, the today's midnight will return. func GetTime(rawDate string, dateFormat string) (time.Time, error) { - var date time.Time - var err error - if rawDate == "" { year, month, day := time.Now().Date() - date = time.Date(year, month, day, 0, 0, 0, 0, time.Local) - } else { - return time.Parse(dateFormat, rawDate) + return time.Date(year, month, day, 0, 0, 0, 0, time.Local), nil } - return date, err + return time.ParseInLocation(dateFormat, rawDate, time.Local) } diff --git a/internal/cmd/utils/utils_test.go b/internal/cmd/utils/utils_test.go index 68802a8..08cc2a7 100644 --- a/internal/cmd/utils/utils_test.go +++ b/internal/cmd/utils/utils_test.go @@ -2,6 +2,7 @@ package utils_test import ( "testing" + "time" "github.com/gabor-boros/minutes/internal/cmd/utils" @@ -37,3 +38,18 @@ func TestIsSliceContains(t *testing.T) { require.False(t, utils.IsSliceContains("test", []string{"testing"})) require.True(t, utils.IsSliceContains("test", []string{"testing", "test"})) } + +func TestGetTime(t *testing.T) { + var parsed time.Time + var err error + + year, month, day := time.Now().Date() + + parsed, err = utils.GetTime("2021-01-01 01:00:00", "2006-01-02 15:04:05") + require.Nil(t, err) + require.Equal(t, time.Date(2021, 1, 1, 1, 0, 0, 0, time.Local), parsed) + + parsed, err = utils.GetTime("", "2006-01-02") + require.Nil(t, err) + require.Equal(t, time.Date(year, month, day, 0, 0, 0, 0, time.Local), parsed) +}