File tree 2 files changed +13
-1
lines changed
2 files changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,10 @@ var conversion = map[rune]string{
56
56
// %z numbers representing the timezone, ex: "-0700"
57
57
// %L milliseconds, ex: ".000"
58
58
func DateFormat (t * time.Time , format string ) string {
59
+ if t == nil {
60
+ return ""
61
+ }
62
+
59
63
retval := make ([]byte , 0 , len (format ))
60
64
for i , ni := 0 , 0 ; i < len (format ); i = ni + 2 {
61
65
ni = strings .IndexByte (format [i :], '%' )
Original file line number Diff line number Diff line change @@ -2,9 +2,10 @@ package utils
2
2
3
3
import (
4
4
"fmt"
5
- . "github.com/smartystreets/goconvey/convey"
6
5
"testing"
7
6
"time"
7
+
8
+ . "github.com/smartystreets/goconvey/convey"
8
9
)
9
10
10
11
func ExampleDateFormat () {
@@ -22,4 +23,11 @@ func TestDateFormat(t *testing.T) {
22
23
So (dateFmt , ShouldEqual , "2009-11-10 23:00" )
23
24
})
24
25
})
26
+
27
+ Convey ("It should not panic when trying to format nil dates" , t , func () {
28
+ dateFmt := DateFormat (nil , "%Y-%m-%d %H:%M" )
29
+ Convey ("Test DateFormat" , func () {
30
+ So (dateFmt , ShouldEqual , "" )
31
+ })
32
+ })
25
33
}
You can’t perform that action at this time.
0 commit comments