Skip to content

Commit 3b7fb49

Browse files
committed
msgpack: add string() for datetime
Added a datetime type conversion function to a string, added tests for this function. Added #322
1 parent 1b7dec6 commit 3b7fb49

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

datetime/datetime.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ func datetimeDecoder(d *msgpack.Decoder, v reflect.Value, extLen int) error {
350350
return ptr.UnmarshalMsgpack(b)
351351
}
352352

353+
// This method converts Datetime to String - formats to ISO8601.
354+
func (d Datetime) String() string {
355+
return d.time.Format(time.RFC3339Nano)
356+
}
357+
353358
func init() {
354359
msgpack.RegisterExtDecoder(datetimeExtID, Datetime{}, datetimeDecoder)
355360
msgpack.RegisterExtEncoder(datetimeExtID, Datetime{}, datetimeEncoder)

datetime/datetime_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,23 @@ func runTestMain(m *testing.M) int {
11741174
return m.Run()
11751175
}
11761176

1177+
func TestDatetimeString(t *testing.T) {
1178+
1179+
tm, _ := time.Parse(time.RFC3339Nano, "2010-05-24T17:51:56.000000009Z")
1180+
dt, err := MakeDatetime(tm)
1181+
if err != nil {
1182+
t.Fatalf("Unable to create Datetime from %s: %s", tm, err)
1183+
}
1184+
1185+
result := dt.String()
1186+
t.Logf("Result: %s", result)
1187+
1188+
expected := "2010-05-24T17:51:56.000000009Z"
1189+
if result != expected {
1190+
t.Errorf("Expected %s, got %s", expected, result)
1191+
}
1192+
1193+
}
11771194
func TestMain(m *testing.M) {
11781195
code := runTestMain(m)
11791196
os.Exit(code)

0 commit comments

Comments
 (0)