-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_layout.go
227 lines (193 loc) · 5.9 KB
/
time_layout.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package years
import (
"regexp"
"strings"
)
// DateUnit stays for the unit of a date like Day/Month/Year/etc
type DateUnit int
const (
UnitUndefined DateUnit = iota
// Day as day of the month
// TODO(nice-to-have) support day of the week + day of the year
Day DateUnit = 1 << (iota - 1)
Week // not supported yet
Month
Quarter // not supported yet
Year
// UnixSecond as well as UnixMillisecond, UnixMicrosecond, UnixNanosecond
// are special units for Unix timestamps
UnixSecond
UnixMillisecond
UnixMicrosecond
UnixNanosecond
)
func (du DateUnit) String() string {
switch du {
case UnitUndefined:
return ""
case Day:
return "day"
case Month:
return "month"
case Year:
return "year"
case UnixSecond:
return "unix_second"
case UnixMillisecond:
return "unix_millisecond"
case UnixMicrosecond:
return "unix_microsecond"
case UnixNanosecond:
return "unix_nanosecond"
default:
panic("fix DateUnit enum!")
}
}
func (du DateUnit) Defined() bool { return du != UnitUndefined }
// DateUnitsDict holds all available DateUnits
var DateUnitsDict = struct {
Day DateUnit
Month DateUnit
Year DateUnit
UnixSecond DateUnit
UnixMillisecond DateUnit
UnixMicrosecond DateUnit
UnixNanosecond DateUnit
}{
Day: Day,
Month: Month,
Year: Year,
// TODO: support week and quarter
UnixSecond: UnixSecond,
UnixMillisecond: UnixMillisecond,
UnixMicrosecond: UnixMicrosecond,
UnixNanosecond: UnixNanosecond,
}
type LayoutFormat int
const (
LayoutFormatUndefined LayoutFormat = iota
// LayoutFormatGo is a format that is supported by Go time.Parse
LayoutFormatGo = 1 << (iota - 1)
// LayoutFormatUnixTimestamp is a format that parses time from Unix timestamp (seconds or milliseconds)
LayoutFormatUnixTimestamp
// TODO(nice-to-have): support more formats, e.g. JS-like formats (YYYY, etc)
)
func (lf LayoutFormat) String() string {
switch lf {
case LayoutFormatGo:
return "go"
case LayoutFormatUnixTimestamp:
return "unix_timestamp"
default:
panic("fix LayoutFormat enum!")
}
}
// LayoutFormatDict holds all available LayoutFormats
var LayoutFormatDict = struct {
GoFormat LayoutFormat
UnixTimestamp LayoutFormat
}{
GoFormat: LayoutFormatGo,
UnixTimestamp: LayoutFormatUnixTimestamp,
}
const (
LayoutTimestampSeconds = "U@"
LayoutTimestampMilliseconds = "U@000"
LayoutTimestampMicroseconds = "U@000000"
LayoutTimestampNanoseconds = "U@000000000"
)
// LayoutDetails stores parsed meta information about given layout string
// e.g. "2006-02-01"
type LayoutDetails struct {
// MinimalUnit e.g. Day for "2006-01-02" and Month for "2006-01"
MinimalUnit DateUnit
// Format is the format of the time used in the layout
Format LayoutFormat
// Units met in layout
Units []DateUnit
}
func (lm *LayoutDetails) HasUnit(q DateUnit) bool {
for _, u := range lm.Units {
if u == q {
return true
}
}
return false
}
// Note: it's a pretty hacky/weak function, but we're OK with it for now
func ParseLayout(layout string) *LayoutDetails {
result := &LayoutDetails{Units: make([]DateUnit, 0)}
// Day of the month: "2" "_2" "02"
// weak check for now via regex: 2 not followed by 0 because of 2006
// TODO: stronger check: 2 should not be part of 2006
// also `_2` should not be confused with __2
twoNotFollowedByZero := regexp.MustCompile(`2([^0]|$)`)
containsDay := strings.Contains(layout, "_2") || strings.Contains(layout, "02") || twoNotFollowedByZero.MatchString(layout)
if containsDay {
result.MinimalUnit = Day
result.Units = append(result.Units, Day)
}
// Reference for future:
// Day of the week: "Mon" "Monday"
// Day of the year: "__2" "002"
// Month: "Jan" "January" "01" "1"
oneNotFollowedByFive := regexp.MustCompile(`0?1([^5]|$)`) // `1` is month but `15` are hours
containsMonth := strings.Contains(layout, "01") || strings.Contains(layout, "Jan") || oneNotFollowedByFive.MatchString(layout)
if containsMonth {
if !result.MinimalUnit.Defined() {
result.MinimalUnit = Month
}
result.Units = append(result.Units, Month)
}
// Year: "2006" "06"
containsYear := strings.Contains(layout, "2006") || strings.Contains(layout, "06")
if containsYear {
if !result.MinimalUnit.Defined() {
result.MinimalUnit = Year
}
result.Units = append(result.Units, Year)
}
if strings.Contains(layout, LayoutTimestampSeconds) {
result.Format = LayoutFormatUnixTimestamp
if strings.Contains(layout, LayoutTimestampNanoseconds) {
result.Units = append(result.Units, UnixNanosecond)
result.MinimalUnit = UnixNanosecond
} else if strings.Contains(layout, LayoutTimestampMicroseconds) {
result.Units = append(result.Units, UnixMicrosecond)
result.MinimalUnit = UnixMicrosecond
} else if strings.Contains(layout, LayoutTimestampMilliseconds) {
result.Units = append(result.Units, UnixMillisecond)
result.MinimalUnit = UnixMillisecond
} else {
result.Units = append(result.Units, UnixSecond)
result.MinimalUnit = UnixSecond
}
return result
}
if len(result.Units) == 0 {
// temporary very simple way of saying it's not a valid layout
return nil
}
// for now, we only support Go-format here
result.Format = LayoutFormatGo
return result
}
// find position (start,end) of the timestamp part in the layout (e.g. `U0.` or `U0.000` etc )
// e.g. `FileName_U0.txt` -> [10, 13]
func findTimestampPart(layout string) (start, end int) {
if !strings.Contains(layout, LayoutTimestampSeconds) {
return 0, 0
}
switch true {
case strings.Contains(layout, LayoutTimestampNanoseconds):
start = strings.Index(layout, LayoutTimestampNanoseconds)
end = start + len(LayoutTimestampNanoseconds)
case strings.Contains(layout, LayoutTimestampMicroseconds):
start = strings.Index(layout, LayoutTimestampMicroseconds)
end = start + len(LayoutTimestampMicroseconds)
case strings.Contains(layout, LayoutTimestampMilliseconds):
start = strings.Index(layout, LayoutTimestampMilliseconds)
end = start + len(LayoutTimestampMilliseconds)
}
return
}