From 5d0856f6f343bc58afc148d86672a64e0f871690 Mon Sep 17 00:00:00 2001 From: "marko.tikvic" Date: Thu, 30 Apr 2020 10:18:54 +0200 Subject: [PATCH] when parsing time make sure to include the date as well... because reasons --- nullables.go | 113 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 56 insertions(+), 57 deletions(-) diff --git a/nullables.go b/nullables.go index 1bed7d6..bb42966 100644 --- a/nullables.go +++ b/nullables.go @@ -335,6 +335,30 @@ func (nt *NullDateTime) CastToSQL() NullDateTime { return *nt } +func parseSQLDateTime(str string, loc *time.Location) (t time.Time, err error) { + base := "0000-00-00 00:00:00.0000000" + timeFormat := "2006-01-02 15:04:05.999999" + switch len(str) { + case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM" + if str == base[:len(str)] { + return + } + t, err = time.Parse(timeFormat[:len(str)], str) + default: + err = fmt.Errorf("invalid time string: %s", str) + return + } + + // Adjust location + if err == nil && loc != time.UTC { + y, mo, d := t.Date() + h, mi, s := t.Clock() + t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil + } + + return +} + // NullDate ... type NullDate struct { Time time.Time @@ -402,8 +426,7 @@ func (nt *NullDate) UnmarshalJSON(b []byte) error { } if temp != nil { - nt.Valid = true - nt.Time = *temp + nt.Scan(t1) } else { nt.Valid = false } @@ -414,6 +437,30 @@ func (nt *NullDate) CastToSQL() NullDate { return *nt } +func parseSQLDate(str string, loc *time.Location) (t time.Time, err error) { + base := "0000-00-00" + timeFormat := "2006-01-02" + switch len(str) { + case 10: + if str == base[:len(str)] { + return + } + t, err = time.Parse(timeFormat[:len(str)], str) + default: + err = fmt.Errorf("invalid time string: %s", str) + return + } + + // Adjust location + if err == nil && loc != time.UTC { + y, mo, d := t.Date() + h, mi, s := t.Clock() + t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil + } + + return +} + // NullTime ... type NullTime struct { Time time.Time @@ -473,7 +520,7 @@ func (nt *NullTime) UnmarshalJSON(b []byte) error { if s1 == "null" { temp = nil } else { - t1, err = time.Parse("15:04:05", s2) + t1, err = time.Parse("2006-05-04 15:04:05", "1970-01-01 "+s2) if err != nil { return err } @@ -481,8 +528,7 @@ func (nt *NullTime) UnmarshalJSON(b []byte) error { } if temp != nil { - nt.Valid = true - nt.Time = *temp + nt.Scan(t1) } else { nt.Valid = false } @@ -493,63 +539,16 @@ func (nt *NullTime) CastToSQL() NullTime { return *nt } -func parseSQLDateTime(str string, loc *time.Location) (t time.Time, err error) { - base := "0000-00-00 00:00:00.0000000" - timeFormat := "2006-01-02 15:04:05.999999" - switch len(str) { - case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM" - if str == base[:len(str)] { - return - } - t, err = time.Parse(timeFormat[:len(str)], str) - default: - err = fmt.Errorf("invalid time string: %s", str) - return - } - - // Adjust location - if err == nil && loc != time.UTC { - y, mo, d := t.Date() - h, mi, s := t.Clock() - t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil - } - - return -} - -func parseSQLDate(str string, loc *time.Location) (t time.Time, err error) { - base := "0000-00-00" - timeFormat := "2006-01-02" - switch len(str) { - case 10: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM" - if str == base[:len(str)] { - return - } - t, err = time.Parse(timeFormat[:len(str)], str) - default: - err = fmt.Errorf("invalid time string: %s", str) - return - } - - // Adjust location - if err == nil && loc != time.UTC { - y, mo, d := t.Date() - h, mi, s := t.Clock() - t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil - } - - return -} - +// NOTE(marko): Date must be included because database can't convert it to TIME otherwise. func parseSQLTime(str string, loc *time.Location) (t time.Time, err error) { - base := "00:00:00.0000000" - timeFormat := "15:04:05.999999" + base := "00:00:00" + timeFormat := "15:04:05" switch len(str) { - case 12, 15: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM" + case 8: if str == base[:len(str)] { return } - t, err = time.Parse(timeFormat[:len(str)], str) + t, err = time.Parse("2006-05-04 "+timeFormat[:len(str)], "1970-01-01 "+str) default: err = fmt.Errorf("invalid time string: %s", str) return -- 1.8.1.2