2023-02-17 10:40:43 +00:00
|
|
|
package driver
|
|
|
|
|
|
2024-12-11 18:35:50 +00:00
|
|
|
import "time"
|
2023-02-17 10:40:43 +00:00
|
|
|
|
|
|
|
|
// Convert a string in [time.RFC3339Nano] format into a [time.Time]
|
|
|
|
|
// if it roundtrips back to the same string.
|
|
|
|
|
// This way times can be persisted to, and recovered from, the database,
|
2023-02-22 17:51:30 +00:00
|
|
|
// but if a string is needed, [database/sql] will recover the same string.
|
2024-01-23 17:50:11 +00:00
|
|
|
func maybeTime(text string) (_ time.Time, _ bool) {
|
2023-02-19 12:44:26 +00:00
|
|
|
// Weed out (some) values that can't possibly be
|
|
|
|
|
// [time.RFC3339Nano] timestamps.
|
2023-02-20 13:30:01 +00:00
|
|
|
if len(text) < len("2006-01-02T15:04:05Z") {
|
2024-01-23 17:50:11 +00:00
|
|
|
return
|
2023-02-19 12:44:26 +00:00
|
|
|
}
|
2023-02-20 13:30:01 +00:00
|
|
|
if len(text) > len(time.RFC3339Nano) {
|
2024-01-23 17:50:11 +00:00
|
|
|
return
|
2023-02-19 12:44:26 +00:00
|
|
|
}
|
2023-02-20 13:30:01 +00:00
|
|
|
if text[4] != '-' || text[10] != 'T' || text[16] != ':' {
|
2024-01-23 17:50:11 +00:00
|
|
|
return
|
2023-02-19 12:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Slow path.
|
2024-01-12 13:33:43 +00:00
|
|
|
var buf [len(time.RFC3339Nano)]byte
|
2023-12-15 00:42:12 +00:00
|
|
|
date, err := time.Parse(time.RFC3339Nano, text)
|
2024-01-12 13:33:43 +00:00
|
|
|
if err == nil && text == string(date.AppendFormat(buf[:0], time.RFC3339Nano)) {
|
2024-01-23 17:50:11 +00:00
|
|
|
return date, true
|
2023-02-17 10:40:43 +00:00
|
|
|
}
|
2024-01-23 17:50:11 +00:00
|
|
|
return
|
2023-02-17 10:40:43 +00:00
|
|
|
}
|