Skip to content

Commit 1e3095b

Browse files
Merge pull request #1559 from KimMachineGun/timepointer
fix format function to handle pointer of time.Time
2 parents 10732d7 + af8d279 commit 1e3095b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

bind.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ func format(tz *time.Location, scale TimeUnit, v any) (string, error) {
288288
return quote(v), nil
289289
case time.Time:
290290
return formatTime(tz, scale, v)
291+
case *time.Time:
292+
if v == nil {
293+
return "NULL", nil
294+
}
295+
return formatTime(tz, scale, *v)
291296
case bool:
292297
if v {
293298
return "1", nil

bind_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,15 @@ func TestFormatTime(t *testing.T) {
254254
val, _ = format(tz, Seconds, t1)
255255
assert.Equal(t, "toDateTime('2022-01-12 15:00:00', 'UTC')", val)
256256
}
257+
258+
// test with pointer to time.Time
259+
val, _ = format(t1.Location(), Seconds, &t1)
260+
assert.Equal(t, "toDateTime('2022-01-12 15:00:00')", val)
257261
}
262+
263+
// test with nil pointer to time.Time
264+
val, _ := format(time.UTC, Seconds, (*time.Time)(nil))
265+
assert.Equal(t, "NULL", val)
258266
}
259267

260268
func TestFormatScaledTime(t *testing.T) {

0 commit comments

Comments
 (0)