Skip to content

Commit

Permalink
fix: solve time parsing issue when start or end date is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
gabor-boros committed Oct 11, 2021
1 parent 67721bf commit 3d9c7be
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
9 changes: 8 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
12 changes: 3 additions & 9 deletions internal/cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 16 additions & 0 deletions internal/cmd/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils_test

import (
"testing"
"time"

"github.com/gabor-boros/minutes/internal/cmd/utils"

Expand Down Expand Up @@ -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)
}

0 comments on commit 3d9c7be

Please sign in to comment.