-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_timestamp.go
49 lines (39 loc) · 838 Bytes
/
tx_timestamp.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
package common
import (
"fmt"
"time"
)
var timeLayouts = []string{
"02.01.2006 15:04",
"02/01/2006 15:04",
"01/02/2006 15:04",
"2006-01-02 15:04",
"2006/01/02 15:04",
"02.01.2006 15:04:05",
"Jan 2, 2006 3:04 PM",
"2006/01/02 3:04 PM",
}
type TxTimestamp struct {
Time time.Time
}
func (ts *TxTimestamp) MarshalCSV() (string, error) {
return ts.Time.Format(time.RFC3339), nil
}
func (ts *TxTimestamp) UnmarshalCSV(csv string) error {
var parsed time.Time
for _, layout := range timeLayouts {
tempTime, err := time.Parse(layout, csv)
if err != nil {
continue
}
if !parsed.IsZero() {
return fmt.Errorf("timestamp %s matches more than one time layout", csv)
}
parsed = tempTime
}
if parsed.IsZero() {
return fmt.Errorf("timestamp %s could not be parsed", csv)
}
ts.Time = parsed
return nil
}