time.go (1553B)
1 package internal 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 const ( 9 dateFormat = "2006-01-02" 10 timeFormat = "15:04:05.999999999" 11 timetzFormat1 = "15:04:05.999999999-07:00:00" 12 timetzFormat2 = "15:04:05.999999999-07:00" 13 timetzFormat3 = "15:04:05.999999999-07" 14 timestampFormat = "2006-01-02 15:04:05.999999999" 15 timestamptzFormat1 = "2006-01-02 15:04:05.999999999-07:00:00" 16 timestamptzFormat2 = "2006-01-02 15:04:05.999999999-07:00" 17 timestamptzFormat3 = "2006-01-02 15:04:05.999999999-07" 18 ) 19 20 func ParseTime(s string) (time.Time, error) { 21 l := len(s) 22 23 if l >= len("2006-01-02 15:04:05") { 24 switch s[10] { 25 case ' ': 26 if c := s[l-6]; c == '+' || c == '-' { 27 return time.Parse(timestamptzFormat2, s) 28 } 29 if c := s[l-3]; c == '+' || c == '-' { 30 return time.Parse(timestamptzFormat3, s) 31 } 32 if c := s[l-9]; c == '+' || c == '-' { 33 return time.Parse(timestamptzFormat1, s) 34 } 35 return time.ParseInLocation(timestampFormat, s, time.UTC) 36 case 'T': 37 return time.Parse(time.RFC3339Nano, s) 38 } 39 } 40 41 if l >= len("15:04:05-07") { 42 if c := s[l-6]; c == '+' || c == '-' { 43 return time.Parse(timetzFormat2, s) 44 } 45 if c := s[l-3]; c == '+' || c == '-' { 46 return time.Parse(timetzFormat3, s) 47 } 48 if c := s[l-9]; c == '+' || c == '-' { 49 return time.Parse(timetzFormat1, s) 50 } 51 } 52 53 if l < len("15:04:05") { 54 return time.Time{}, fmt.Errorf("bun: can't parse time=%q", s) 55 } 56 57 if s[2] == ':' { 58 return time.ParseInLocation(timeFormat, s, time.UTC) 59 } 60 return time.ParseInLocation(dateFormat, s, time.UTC) 61 }