-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbdialect.go
67 lines (59 loc) · 1.98 KB
/
dbdialect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package sqlbless
import (
"errors"
"fmt"
"regexp"
"strings"
"time"
)
type DBDialect struct {
Usage string
SqlForDesc string
SqlForTab string
DisplayDateTimeLayout string
TypeNameToConv func(string) func(string) (string, error)
DSNFilter func(string) (string, error)
}
func (D *DBDialect) TryTypeNameToConv(typeName string) func(string) (string, error) {
if D.TypeNameToConv == nil {
return nil
}
return D.TypeNameToConv(typeName)
}
const (
DateTimeTzLayout = "2006-01-02 15:04:05.999999999 -07:00"
DateTimeLayout = "2006-01-02 15:04:05.999999999"
DateOnlyLayout = "2006-01-02"
TimeOnlyLayout = "15:04:05.999999999"
TimeTzLayout = "15:04:05.999999999 -07:00"
)
var (
rxDateTimeTz = regexp.MustCompile(`^\s*(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d(?:\.\d+)?)\s*([\-\+]?)(\d\d?):(\d\d)\s*$`)
rxDateTime = regexp.MustCompile(`^\s*(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d(?:\.\d+)?)\s*$`)
rxDateOnly = regexp.MustCompile(`^\s*(\d{4}-\d\d-\d\d)\s*$`)
rxTimeTz = regexp.MustCompile(`^\s*(?:\d{4}-\d\d-\d\d )?(\d\d:\d\d:\d\d(?:\.\d+)? [-\+]\d\d:\d\d)\s*$`)
rxTimeOnly = regexp.MustCompile(`^\s*(?:\d{4}-\d\d-\d\d )?(\d\d:\d\d:\d\d(?:\.\d+)?)\s*$`)
)
func ParseAnyDateTime(s string) (time.Time, error) {
if m := rxDateTimeTz.FindStringSubmatch(s); m != nil {
return time.Parse(DateTimeTzLayout,
fmt.Sprintf("%s %s%02s:%02s", m[1], m[2], m[3], m[4]))
}
if m := rxDateTime.FindStringSubmatch(s); m != nil {
return time.Parse(DateTimeLayout, m[1])
}
if m := rxDateOnly.FindStringSubmatch(s); m != nil {
return time.Parse(DateOnlyLayout, m[1])
}
if m := rxTimeTz.FindStringSubmatch(s); m != nil {
return time.Parse(TimeTzLayout, m[1])
}
if m := rxTimeOnly.FindStringSubmatch(s); m != nil {
return time.Parse(TimeOnlyLayout, m[1])
}
return time.Time{}, errors.New("not time format")
}
var dbDialect = map[string]*DBDialect{}
func RegisterDB(name string, setting *DBDialect) {
dbDialect[strings.ToUpper(name)] = setting
}