Skip to content

Commit 4201362

Browse files
committed
✨ Add new date functions
1 parent 7325484 commit 4201362

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

namespace/date_functions.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package namespace
2+
3+
import (
4+
"time"
5+
6+
"github.com/mattn/go-sqlite3"
7+
)
8+
9+
func registerDateFunctions(conn *sqlite3.SQLiteConn) {
10+
var dateFunctions = []struct {
11+
name string
12+
function any
13+
pure bool
14+
}{
15+
{"utc_timestamp", utc_timestamp, false},
16+
{"now", now, false},
17+
{"toYYYYMMDDHHMMSS", toYYYYMMDDHHMMSS, true},
18+
{"toYYYYMMDD", toYYYYMMDD, true},
19+
{"toYYYYMM", toYYYYMM, true},
20+
{"toYYYY", toYYYY, true},
21+
{"toHH", toHH, true},
22+
{"toMM", toMM, true},
23+
{"toSS", toSS, true},
24+
}
25+
for _, f := range dateFunctions {
26+
conn.RegisterFunc(f.name, f.function, f.pure)
27+
}
28+
}
29+
30+
func utc_timestamp() string {
31+
utc := time.Now().UTC()
32+
return utc.Format("2006-01-02 15:04:05")
33+
}
34+
35+
func now() string {
36+
return time.Now().Format("2006-01-02 15:04:05")
37+
}
38+
39+
func parseDate(date string) time.Time {
40+
// Try to parse in different formats
41+
// such as "YYYY-MM-DD", "YYYY-MM-DD HH:MM:SS", "YYYY-MM-DD HH:MM:SS.ssssss"
42+
43+
formats := []string{
44+
45+
time.RFC1123,
46+
time.RFC1123Z,
47+
time.UnixDate,
48+
time.RFC3339,
49+
time.RFC3339Nano,
50+
"2006-01-02",
51+
"2006-01-02 15:04:05",
52+
"2006-01-02 15:04:05.000000",
53+
"15:04:05",
54+
"15:04:05.000000",
55+
}
56+
57+
var t time.Time
58+
var err error
59+
for _, format := range formats {
60+
t, err = time.Parse(format, date)
61+
if err == nil {
62+
return t
63+
}
64+
}
65+
66+
return t
67+
}
68+
69+
func toYYYYMMDDHHMMSS(userVal string) string {
70+
parsed := parseDate(userVal)
71+
return parsed.Format("2006-01-02 15:04:05")
72+
}
73+
74+
func toYYYYMMDD(userVal string) string {
75+
parsed := parseDate(userVal)
76+
return parsed.Format("2006-01-02")
77+
}
78+
79+
func toYYYYMM(userVal string) string {
80+
parsed := parseDate(userVal)
81+
return parsed.Format("2006-01")
82+
}
83+
84+
func toYYYY(userVal string) string {
85+
parsed := parseDate(userVal)
86+
return parsed.Format("2006")
87+
}
88+
89+
func toHH(userVal string) string {
90+
parsed := parseDate(userVal)
91+
return parsed.Format("15")
92+
}
93+
94+
func toMM(userVal string) string {
95+
parsed := parseDate(userVal)
96+
return parsed.Format("04")
97+
}
98+
99+
func toSS(userVal string) string {
100+
parsed := parseDate(userVal)
101+
return parsed.Format("05")
102+
}

namespace/namespace.go

+6
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ func (n *Namespace) Register(registerName string) (*sql.DB, error) {
319319
// Register the crypto functions
320320
registerCryptoFunctions(conn)
321321

322+
// Register the date functions
323+
registerDateFunctions(conn)
324+
322325
// Register the other functions
323326
registerOtherFunctions(conn)
324327

@@ -331,6 +334,9 @@ func (n *Namespace) Register(registerName string) (*sql.DB, error) {
331334
if err != nil {
332335
return nil, err
333336
}
337+
db.SetConnMaxIdleTime(0)
338+
db.SetConnMaxLifetime(0)
339+
db.SetMaxIdleConns(32)
334340

335341
n.registered = true
336342

0 commit comments

Comments
 (0)