-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiso8601_replacer.go
144 lines (124 loc) · 3.23 KB
/
iso8601_replacer.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
package main
import (
"regexp"
"strconv"
"strings"
"time"
"unicode"
)
const dateLayout = "2006-01-02"
const hoursMinutesLayout = "15:04"
const secondsLayout = "05"
// Iso8601Replacer parses iso 8601 dates and formats them per format and location
type Iso8601Replacer struct {
Regex *regexp.Regexp
}
func getTimePortion(dateTimeString string) string {
dateTimeFields := strings.FieldsFunc(dateTimeString, func(c rune) bool {
return c == 'T' || c == ' '
})
return dateTimeFields[1]
}
func getNanosLength(timePortion string) int {
lastPeriod := strings.LastIndex(timePortion, ".")
if lastPeriod < 1 {
return 0
}
length := 0
for i := lastPeriod + 1; i < len(timePortion); i++ {
if unicode.IsDigit(rune(timePortion[i])) {
length++
} else {
break
}
}
return length
}
func writeNanos(sb *strings.Builder, nanosLen int) {
if nanosLen > 0 {
sb.WriteString(".")
for i := 1; i <= nanosLen; i++ {
sb.WriteString("0")
}
}
}
func getNonUtcTimeLayout(timePortion string, hasT bool, nanosLen int) string {
hasZ := strings.Contains(timePortion, "Z")
offsetIndex := strings.LastIndexFunc(timePortion, func(c rune) bool {
return c == '+' || c == '-'
})
timezoneLength := len(timePortion) - offsetIndex - 1
var sb strings.Builder
sb.WriteString(dateLayout)
if hasT {
sb.WriteString("T")
} else {
sb.WriteString(" ")
}
sb.WriteString(hoursMinutesLayout)
hasSeconds := strings.Count(timePortion, ":") >= 2
if hasSeconds {
sb.WriteString(":")
sb.WriteString(secondsLayout)
}
writeNanos(&sb, nanosLen)
if hasZ {
sb.WriteString("Z")
}
switch timezoneLength {
case 2:
sb.WriteString("-07")
case 4:
sb.WriteString("-0700")
case 5:
sb.WriteString("-07:00")
default:
panic("Unsupported non UTC time layout " + timePortion)
}
return sb.String()
}
func getTimeLayout(dateString string) string {
hasT := strings.Contains(dateString, "T")
hasZ := strings.Contains(dateString, "Z")
timePortion := getTimePortion(dateString)
nanosLen := getNanosLength(timePortion)
if strings.Contains(timePortion, "+") || strings.Contains(timePortion, "-") {
return getNonUtcTimeLayout(timePortion, hasT, nanosLen)
}
var sb strings.Builder
sb.WriteString(dateLayout)
if hasT {
sb.WriteString("T")
} else {
sb.WriteString(" ")
}
sb.WriteString(hoursMinutesLayout)
hasSeconds := strings.Count(timePortion, ":") >= 2
if hasSeconds {
sb.WriteString(":")
sb.WriteString(secondsLayout)
}
writeNanos(&sb, nanosLen)
if hasZ {
sb.WriteString("Z")
}
return sb.String()
}
// ReplaceDates replaces multiple instances in the input string
func (replacer Iso8601Replacer) ReplaceDates(input string, format string, location *time.Location) string {
return replacer.Regex.ReplaceAllStringFunc(input, func(dateString string) string {
layout := getTimeLayout(dateString)
t, err := time.Parse(layout, dateString)
if err != nil {
panic(err.Error())
}
if format == UnixSeconds {
return strconv.FormatInt(t.Unix(), 10)
}
return t.In(location).Format(format)
})
}
// NewIso8601Replacer creates a new replacer with the regex initialized
func NewIso8601Replacer() *Iso8601Replacer {
return &Iso8601Replacer{regexp.MustCompile(`\d{4}-\d{2}-\d{2}(T| )\d{2}:\d{2}(:\d{2}(\.\d{1,})?Z?((\+|\-)(\d{4}|\d{2}:\d{2}|\d{2}))?|Z)?`)}
}