github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/driver/time.go (about) 1 package driver 2 3 import ( 4 "time" 5 ) 6 7 // Convert a string in [time.RFC3339Nano] format into a [time.Time] 8 // if it roundtrips back to the same string. 9 // This way times can be persisted to, and recovered from, the database, 10 // but if a string is needed, [database/sql] will recover the same string. 11 func maybeTime(text string) (_ time.Time, _ bool) { 12 // Weed out (some) values that can't possibly be 13 // [time.RFC3339Nano] timestamps. 14 if len(text) < len("2006-01-02T15:04:05Z") { 15 return 16 } 17 if len(text) > len(time.RFC3339Nano) { 18 return 19 } 20 if text[4] != '-' || text[10] != 'T' || text[16] != ':' { 21 return 22 } 23 24 // Slow path. 25 var buf [len(time.RFC3339Nano)]byte 26 date, err := time.Parse(time.RFC3339Nano, text) 27 if err == nil && text == string(date.AppendFormat(buf[:0], time.RFC3339Nano)) { 28 return date, true 29 } 30 return 31 }