From 8c77107df3757c4ec5eae6e9d7c018618e708bfa Mon Sep 17 00:00:00 2001 From: mehanizm Date: Fri, 10 Apr 2020 17:01:27 +0300 Subject: [PATCH] fix: change millisecond time format If millisecond in go time is empty they will be not exist in result string if using "999" in format. And jira api will response with error in the case. Using "000" fix the problem. Add test for time marshaling. --- issue.go | 2 +- issue_test.go | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/issue.go b/issue.go index 46ad849..5a5da64 100644 --- a/issue.go +++ b/issue.go @@ -351,7 +351,7 @@ func (t *Time) UnmarshalJSON(b []byte) error { // MarshalJSON will transform the time.Time into a JIRA time // during the creation of a JIRA request func (t Time) MarshalJSON() ([]byte, error) { - return []byte(time.Time(t).Format("\"2006-01-02T15:04:05.999-0700\"")), nil + return []byte(time.Time(t).Format("\"2006-01-02T15:04:05.000-0700\"")), nil } // UnmarshalJSON will transform the JIRA date into a time.Time diff --git a/issue_test.go b/issue_test.go index a7c1565..21236a0 100644 --- a/issue_test.go +++ b/issue_test.go @@ -9,11 +9,9 @@ import ( "reflect" "strings" "testing" - - "github.com/google/go-cmp/cmp" - "time" + "github.com/google/go-cmp/cmp" "github.com/trivago/tgo/tcontainer" ) @@ -1799,3 +1797,34 @@ func TestIssueService_AddRemoteLink(t *testing.T) { t.Errorf("Error given: %s", err) } } + +func TestTime_MarshalJSON(t *testing.T) { + timeFormatParseFrom := "2006-01-02T15:04:05.999Z" + testCases := []struct { + name string + inputTime string + expected string + }{ + { + name: "test without ms", + inputTime: "2020-04-01T01:01:01.000Z", + expected: "\"2020-04-01T01:01:01.000+0000\"", + }, + { + name: "test with ms", + inputTime: "2020-04-01T01:01:01.001Z", + expected: "\"2020-04-01T01:01:01.001+0000\"", + }, + } + + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + rawTime, _ := time.Parse(timeFormatParseFrom, tt.inputTime) + time := Time(rawTime) + got, _ := time.MarshalJSON() + if string(got) != tt.expected { + t.Errorf("Time.MarshalJSON() = %v, want %v", string(got), tt.expected) + } + }) + } +}