-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2116071
commit 08bd1fc
Showing
2 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package persistence | ||
|
||
import "time" | ||
|
||
type StringTime struct { | ||
Time time.Time | ||
} | ||
|
||
// this seems to be the format that we are using! | ||
// 2021-08-04 16:17:10.241794246+00:00 | ||
// it's rfc3339-like, but not exactly | ||
var ( | ||
formatString = "2006-01-02 15:04:05.999999999-07:00" | ||
) | ||
|
||
// Scan implements the Scanner interface. | ||
func (s *StringTime) Scan(value interface{}) error { | ||
switch v := value.(type) { | ||
case *time.Time: | ||
s.Time = *v | ||
break | ||
case string: | ||
t, err := time.Parse(formatString, v) | ||
if err != nil { | ||
return err | ||
} | ||
s.Time = t | ||
break | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type NullStringTime struct { | ||
Time time.Time | ||
Valid bool | ||
} | ||
|
||
// Scan implements the Scanner interface. | ||
func (s *NullStringTime) Scan(value interface{}) error { | ||
if value == nil { | ||
s.Valid = false | ||
return nil | ||
} | ||
|
||
switch v := value.(type) { | ||
case *time.Time: | ||
s.Time = *v | ||
s.Valid = true | ||
break | ||
case string: | ||
t, err := time.Parse(formatString, v) | ||
if err != nil { | ||
return err | ||
} | ||
s.Time = t | ||
s.Valid = true | ||
break | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters