-
Notifications
You must be signed in to change notification settings - Fork 0
/
xoutil.go
54 lines (44 loc) · 1.09 KB
/
xoutil.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
// Package xoutil provides additional types for the xo cli tool.
package xoutil
import (
"database/sql/driver"
"errors"
"fmt"
"reflect"
"time"
"github.com/mattn/go-sqlite3"
)
// SqTime provides a type that will correctly scan the various timestamps
// values stored by the github.com/mattn/go-sqlite3 driver for time.Time
// values, as well as correctly satisfying the sql/driver/Valuer interface.
type SqTime struct {
time.Time
}
// Value satisfies the Valuer interface.
func (t SqTime) Value() (driver.Value, error) {
return t.Time, nil
}
// Scan satisfies the Scanner interface.
func (t *SqTime) Scan(v interface{}) error {
switch x := v.(type) {
case []byte:
return t.parse(string(x))
case string:
return t.parse(x)
}
return fmt.Errorf("cannot convert type %s to time.Time", reflect.TypeOf(v))
}
// parse attempts to parse string s to t.
func (t *SqTime) parse(s string) error {
if s == "" {
return nil
}
for _, f := range sqlite3.SQLiteTimestampFormats {
z, err := time.Parse(f, s)
if err == nil {
t.Time = z
return nil
}
}
return errors.New("could not parse time")
}