Skip to content

Commit 020b134

Browse files
committed
feat(sqltype/time): AddSupportedLayout
1 parent be1aaf8 commit 020b134

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

pkg/sqltype/time/timestamp.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package time
22

33
import (
4+
"cmp"
45
"database/sql"
56
"database/sql/driver"
67
"encoding"
@@ -17,8 +18,7 @@ var (
1718
)
1819

1920
var (
20-
CST = time.UTC
21-
OutputLayout = time.RFC3339
21+
CST = time.UTC
2222
)
2323

2424
func 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

8089
func 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

130139
func (dt Timestamp) Format(layout string) string {

pkg/sqltype/time/timestamp_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestTimestamp(t *testing.T) {
4040
})
4141

4242
b.Given("time string", func(b bdd.T) {
43-
t0, _ := time.Parse(time.RFC3339, "2017-03-27T23:58:59+08:00")
43+
t0, _ := time.ParseInLocation(time.RFC3339, "2017-03-27T23:58:59+08:00", nil)
4444
dt := Timestamp(t0)
4545

4646
b.Then("output RFC3339 string",
@@ -70,11 +70,12 @@ func TestTimestamp(t *testing.T) {
7070
})
7171

7272
b.Given("time string for custom output layout", func(b bdd.T) {
73-
t0, _ := time.Parse(time.RFC3339, "2017-03-27T23:58:59+08:00")
73+
t0, _ := time.ParseInLocation(time.RFC3339, "2017-03-27T23:58:59+08:00", nil)
7474
dt := Timestamp(t0)
7575

7676
b.When("marshal text", func(b bdd.T) {
77-
SetOutput("2006-01-02 15:04:05", nil)
77+
AddSupportedLayout("2006-01-02 15:04:05", nil)
78+
SetOutputLayout("2006-01-02 15:04:05", nil)
7879

7980
data := bdd.Must(dt.MarshalText())
8081

0 commit comments

Comments
 (0)