2023-02-17 10:40:43 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql/driver"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
// but if a string is needed, [database.sql] will recover the same string.
|
|
|
|
|
func maybeDate(text string) driver.Value {
|
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") {
|
2023-02-19 12:44:26 +00:00
|
|
|
return text
|
|
|
|
|
}
|
2023-02-20 13:30:01 +00:00
|
|
|
if len(text) > len(time.RFC3339Nano) {
|
2023-02-19 12:44:26 +00:00
|
|
|
return text
|
|
|
|
|
}
|
2023-02-20 13:30:01 +00:00
|
|
|
if text[4] != '-' || text[10] != 'T' || text[16] != ':' {
|
|
|
|
|
return text
|
2023-02-19 12:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Slow path.
|
2023-02-17 10:40:43 +00:00
|
|
|
date, err := time.Parse(time.RFC3339Nano, text)
|
|
|
|
|
if err == nil && date.Format(time.RFC3339Nano) == text {
|
|
|
|
|
return date
|
|
|
|
|
}
|
|
|
|
|
return text
|
|
|
|
|
}
|