11package time
22
33import (
4+ "cmp"
45 "database/sql"
56 "database/sql/driver"
67 "encoding"
1718)
1819
1920var (
20- CST = time .UTC
21- OutputLayout = time .RFC3339
21+ CST = time .UTC
2222)
2323
2424func init () {
@@ -28,8 +28,19 @@ func init() {
2828 }
2929}
3030
31- func SetOutput (layout string , location * time.Location ) {
32- OutputLayout = layout
31+ var (
32+ outputLayout = time .RFC3339
33+ supportedLayouts = map [string ]* time.Location {
34+ time .RFC3339 : nil ,
35+ }
36+ )
37+
38+ func AddSupportedLayout (layout string , location * time.Location ) {
39+ supportedLayouts [layout ] = cmp .Or (location , CST )
40+ }
41+
42+ func SetOutputLayout (layout string , location * time.Location ) {
43+ outputLayout = layout
3344
3445 if location != nil {
3546 CST = location
@@ -62,19 +73,17 @@ func (Timestamp) DataType(engine string) string {
6273 return "bigint"
6374}
6475
65- func ParseTimestampFromString (s string ) (Timestamp , error ) {
66- if OutputLayout != time .RFC3339 {
67- t , err := time .ParseInLocation (OutputLayout , s , CST )
68- if err == nil {
76+ func ParseTimestampFromString (s string ) (d Timestamp , err error ) {
77+ for layout , cst := range supportedLayouts {
78+ // fallback
79+ t , e := time .ParseInLocation (layout , s , cst )
80+ if e == nil {
6981 return Timestamp (t ), nil
7082 }
83+ err = e
7184 }
72- // fallback
73- t , err := time .Parse (time .RFC3339 , s )
74- if err != nil {
75- return TimestampUnixZero , err
76- }
77- return Timestamp (t ), nil
85+
86+ return
7887}
7988
8089func ParseTimestampFromStringWithLayout (input , layout string ) (Timestamp , error ) {
@@ -124,7 +133,7 @@ func (dt Timestamp) String() string {
124133 if dt .IsZero () {
125134 return ""
126135 }
127- return time .Time (dt ).In (CST ).Format (OutputLayout )
136+ return time .Time (dt ).In (CST ).Format (outputLayout )
128137}
129138
130139func (dt Timestamp ) Format (layout string ) string {
0 commit comments