Skip to content

Commit 8c77107

Browse files
mehanizmandygrunwald
authored andcommitted
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.
1 parent f200e15 commit 8c77107

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

issue.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (t *Time) UnmarshalJSON(b []byte) error {
351351
// MarshalJSON will transform the time.Time into a JIRA time
352352
// during the creation of a JIRA request
353353
func (t Time) MarshalJSON() ([]byte, error) {
354-
return []byte(time.Time(t).Format("\"2006-01-02T15:04:05.999-0700\"")), nil
354+
return []byte(time.Time(t).Format("\"2006-01-02T15:04:05.000-0700\"")), nil
355355
}
356356

357357
// UnmarshalJSON will transform the JIRA date into a time.Time

issue_test.go

+32-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import (
99
"reflect"
1010
"strings"
1111
"testing"
12-
13-
"github.com/google/go-cmp/cmp"
14-
1512
"time"
1613

14+
"github.com/google/go-cmp/cmp"
1715
"github.com/trivago/tgo/tcontainer"
1816
)
1917

@@ -1799,3 +1797,34 @@ func TestIssueService_AddRemoteLink(t *testing.T) {
17991797
t.Errorf("Error given: %s", err)
18001798
}
18011799
}
1800+
1801+
func TestTime_MarshalJSON(t *testing.T) {
1802+
timeFormatParseFrom := "2006-01-02T15:04:05.999Z"
1803+
testCases := []struct {
1804+
name string
1805+
inputTime string
1806+
expected string
1807+
}{
1808+
{
1809+
name: "test without ms",
1810+
inputTime: "2020-04-01T01:01:01.000Z",
1811+
expected: "\"2020-04-01T01:01:01.000+0000\"",
1812+
},
1813+
{
1814+
name: "test with ms",
1815+
inputTime: "2020-04-01T01:01:01.001Z",
1816+
expected: "\"2020-04-01T01:01:01.001+0000\"",
1817+
},
1818+
}
1819+
1820+
for _, tt := range testCases {
1821+
t.Run(tt.name, func(t *testing.T) {
1822+
rawTime, _ := time.Parse(timeFormatParseFrom, tt.inputTime)
1823+
time := Time(rawTime)
1824+
got, _ := time.MarshalJSON()
1825+
if string(got) != tt.expected {
1826+
t.Errorf("Time.MarshalJSON() = %v, want %v", string(got), tt.expected)
1827+
}
1828+
})
1829+
}
1830+
}

0 commit comments

Comments
 (0)