From 4b73a1d7263b7c0447a5877d95f2867ab023f1ea Mon Sep 17 00:00:00 2001 From: Sylvain Gaunet <3924333-sgaunet@users.noreply.gitlab.com> Date: Mon, 30 Jan 2023 12:14:16 +0100 Subject: [PATCH] Fix #3 Error when using different format --- calcdatelib/calcdatelib.go | 2 +- calcdatelib/calcdatelib_test.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/calcdatelib/calcdatelib.go b/calcdatelib/calcdatelib.go index 89441e8..f2ed81a 100644 --- a/calcdatelib/calcdatelib.go +++ b/calcdatelib/calcdatelib.go @@ -160,7 +160,7 @@ func (d *Date) initIndexSecond() error { func (d *Date) initUnitTime(defaultValue int, ptrToInit *int, ptrRelativToInit *int, idx int) error { *ptrToInit = defaultValue *ptrRelativToInit = 0 - if idx != -1 { + if idx > 0 { value := d.submatch[idx] if value != "" { convert, err := strconv.Atoi(value) diff --git a/calcdatelib/calcdatelib_test.go b/calcdatelib/calcdatelib_test.go index 3c31b28..dd34661 100644 --- a/calcdatelib/calcdatelib_test.go +++ b/calcdatelib/calcdatelib_test.go @@ -208,6 +208,15 @@ func TestNewDate(t *testing.T) { expectedString: "1981/12/01 13:00:00", expectedErr: false, }, + { + Name: "different format", + nowFct: func() time.Time { return time.Date(1982, 01, 01, 00, 00, 00, 0, time.UTC) }, // 1982-01-01 00:00:00 + argDate: "2022/12/01", + argIfmt: "%YYYY/%MM/%DD", + argTz: "", + expectedString: "2022/12/01 00:00:00", + expectedErr: false, + }, } for _, test := range testCases {