Skip to content

Commit c171da1

Browse files
jvoisinfguillot
authored andcommitted
refactor(reader): minor date parsing simplifications
- There is no need to use two replacers instead of one. It might help keep the input in a low CPU cache. - Don't cast an int to float (twice) for nothing in checkTimezoneRange.
1 parent 11ea137 commit c171da1

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

internal/reader/date/parser.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ var dateFormats = [...]string{
228228
"02.01.06",
229229
}
230230

231-
var invalidTimezoneReplacer = strings.NewReplacer(
231+
var replacer = strings.NewReplacer(
232+
// Timezones
232233
"Europe/Brussels", "CET",
233234
"America/Los_Angeles", "PDT",
234235
"GMT+0000 (Coordinated Universal Time)", "GMT",
235236
"GMT-", "GMT -",
236-
)
237237

238-
var invalidLocalizedDateReplacer = strings.NewReplacer(
238+
// Localized dates
239239
"Mo,", "Mon,",
240240
"Di,", "Tue,",
241241
"Mi,", "Wed,",
@@ -325,8 +325,7 @@ func Parse(rawInput string) (t time.Time, err error) {
325325
return time.Unix(timestamp, 0), nil
326326
}
327327

328-
processedInput := invalidLocalizedDateReplacer.Replace(rawInput)
329-
processedInput = invalidTimezoneReplacer.Replace(processedInput)
328+
processedInput := replacer.Replace(rawInput)
330329

331330
for _, layout := range dateFormatsLocalTimesOnly {
332331
if t, err = parseLocalTimeDates(layout, processedInput); err == nil {
@@ -366,7 +365,7 @@ func parseLocalTimeDates(layout, ds string) (t time.Time, err error) {
366365
// Avoid "pq: time zone displacement out of range" errors
367366
func checkTimezoneRange(t time.Time) time.Time {
368367
_, offset := t.Zone()
369-
if float64(offset) > 14*60*60 || float64(offset) < -12*60*60 {
368+
if offset > 14*60*60 || offset < -12*60*60 {
370369
t = t.UTC()
371370
}
372371
return t

0 commit comments

Comments
 (0)