-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformats.go
104 lines (79 loc) · 2.29 KB
/
formats.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"errors"
"regexp"
"strconv"
"strings"
"time"
)
type sFmt struct{}
func (u sFmt) String() string { return "unix-seconds" }
func (u sFmt) Match(value string) bool {
return regexp.MustCompile(`^\d{1,11}$`).Match([]byte(value))
}
func (u sFmt) Parse(value string) (time.Time, error) {
if value, err := strconv.ParseInt(value, 10, 64); err != nil {
return time.Time{}, err
} else {
return time.Unix(value, 0), nil
}
}
type msFmt struct{}
func (u msFmt) String() string { return "unix-milliseconds" }
func (u msFmt) Match(value string) bool {
return regexp.MustCompile(`^\d{12,14}$`).Match([]byte(value))
}
func (u msFmt) Parse(value string) (time.Time, error) {
if value, err := strconv.ParseInt(value, 10, 64); err != nil {
return time.Time{}, err
} else {
return time.Unix(value/1000, value%1000*1000000), nil
}
}
type usFmt struct{}
func (u usFmt) String() string { return "unix-microseconds" }
func (u usFmt) Match(value string) bool {
return regexp.MustCompile(`^\d{15,16}$`).Match([]byte(value))
}
func (u usFmt) Parse(value string) (time.Time, error) {
if value, err := strconv.ParseInt(value, 10, 64); err != nil {
return time.Time{}, err
} else {
return time.Unix(value/1000000, value%1000000*1000), nil
}
}
type nsFmt struct{}
func (u nsFmt) String() string { return "unix-nanoseconds" }
func (u nsFmt) Match(value string) bool {
return regexp.MustCompile(`^\d{16,}$`).Match([]byte(value))
}
func (u nsFmt) Parse(value string) (time.Time, error) {
if value, err := strconv.ParseInt(value, 10, 64); err != nil {
return time.Time{}, err
} else {
return time.Unix(value/1000000000, value%1000000000), nil
}
}
type fsFmt struct{}
func (u fsFmt) String() string { return "unix-float-seconds" }
func (u fsFmt) Match(value string) bool {
return regexp.MustCompile(`^\d{1,11}\.\d{1,9}$`).Match([]byte(value))
}
func (u fsFmt) Parse(value string) (time.Time, error) {
parts := strings.Split(value, ".")
if len(parts) != 2 {
return time.Time{}, errors.New("invalid float seconds format")
}
for len(parts[1]) < 9 {
parts[1] += "0"
}
sec, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return time.Time{}, err
}
nsecs, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(sec, nsecs), nil
}