Skip to content

Commit 7ce15ae

Browse files
authored
update time parsing (#22)
* update time parsing * update testdata
1 parent c38faf6 commit 7ce15ae

File tree

3 files changed

+93
-16
lines changed

3 files changed

+93
-16
lines changed

server/localization.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,36 @@ func (l *Locale) ForumHost() string {
4040
return "www.pathofexile.com"
4141
}
4242

43+
var esMonthReplacer = strings.NewReplacer(
44+
"ene", "Jan",
45+
"feb", "Feb",
46+
"mar", "Mar",
47+
"abr", "Apr",
48+
"may", "May",
49+
"jun", "Jun",
50+
"jul", "Jul",
51+
"ago", "Aug",
52+
"sep", "Sep",
53+
"oct", "Oct",
54+
"nov", "Nov",
55+
"dic", "Dec",
56+
)
57+
58+
var brMonthReplacer = strings.NewReplacer(
59+
"de jan de", "Jan",
60+
"de fev de", "Feb",
61+
"de mar de", "Mar",
62+
"de abr de", "Apr",
63+
"de mai de", "May",
64+
"de jun de", "Jun",
65+
"de jul de", "Jul",
66+
"de ago de", "Aug",
67+
"de set de", "Sep",
68+
"de out de", "Oct",
69+
"de nov de", "Nov",
70+
"de dez de", "Dec",
71+
)
72+
4373
var thMonthReplacer = strings.NewReplacer(
4474
"ม.ค.", "Jan",
4575
"ก.พ.", "Feb",
@@ -70,20 +100,35 @@ var frMonthReplacer = strings.NewReplacer(
70100
"déc.", "Dec",
71101
)
72102

103+
var ruMonthReplacer = strings.NewReplacer(
104+
"янв.", "Jan",
105+
"февр.", "Feb",
106+
"марта", "Mar",
107+
"апр.", "Apr",
108+
"мая", "May",
109+
"июня", "Jun",
110+
"июля", "Jul",
111+
"авг.", "Aug",
112+
"сент.", "Sep",
113+
"окт.", "Oct",
114+
"нояб.", "Nov",
115+
"дек.", "Dec",
116+
)
117+
73118
// TODO: add translations
74119
var Locales = []*Locale{
75120
{
76121
IncludeReddit: true,
77122
Image: "static/images/locales/gb.png",
78123
ParseTime: func(s string, tz *time.Location) (time.Time, error) {
79-
return time.ParseInLocation("Jan _2, 2006 3:04:05 PM", s, tz)
124+
return time.ParseInLocation("Jan _2, 2006, 3:04:05 PM", s, tz)
80125
},
81126
},
82127
{
83128
Subdomain: "br",
84129
Image: "static/images/locales/br.png",
85130
ParseTime: func(s string, tz *time.Location) (time.Time, error) {
86-
return time.ParseInLocation("2/1/2006 15:04:05", s, tz)
131+
return time.ParseInLocation("2 Jan 2006 15:04:05", brMonthReplacer.Replace(s), tz)
87132
},
88133
Translations: map[string]string{
89134
"Activity": "Atividade",
@@ -97,7 +142,7 @@ var Locales = []*Locale{
97142
Subdomain: "ru",
98143
Image: "static/images/locales/ru.png",
99144
ParseTime: func(s string, tz *time.Location) (time.Time, error) {
100-
return time.ParseInLocation("2.1.2006 15:04:05", s, tz)
145+
return time.ParseInLocation("2 Jan 2006 г., 15:04:05", ruMonthReplacer.Replace(s), tz)
101146
},
102147
Translations: map[string]string{
103148
"Activity": "Активность",
@@ -111,14 +156,14 @@ var Locales = []*Locale{
111156
Subdomain: "th",
112157
Image: "static/images/locales/th.png",
113158
ParseTime: func(s string, tz *time.Location) (time.Time, error) {
114-
return time.ParseInLocation("_2 Jan 2006, 15:04:05", thMonthReplacer.Replace(s), tz)
159+
return time.ParseInLocation("_2 Jan 2006 15:04:05", thMonthReplacer.Replace(s), tz)
115160
},
116161
},
117162
{
118163
Subdomain: "de",
119164
Image: "static/images/locales/de.png",
120165
ParseTime: func(s string, tz *time.Location) (time.Time, error) {
121-
return time.ParseInLocation("2.1.2006 15:04:05", s, tz)
166+
return time.ParseInLocation("2.1.2006, 15:04:05", s, tz)
122167
},
123168
Translations: map[string]string{
124169
"Activity": "Aktivität",
@@ -146,7 +191,7 @@ var Locales = []*Locale{
146191
Subdomain: "es",
147192
Image: "static/images/locales/es.png",
148193
ParseTime: func(s string, tz *time.Location) (time.Time, error) {
149-
return time.ParseInLocation("2/1/2006 15:04:05", s, tz)
194+
return time.ParseInLocation("2 Jan. 2006 15:04:05", esMonthReplacer.Replace(s), tz)
150195
},
151196
Translations: map[string]string{
152197
"Activity": "Actividad",

server/localization_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package server
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestLocale_ParseTime(t *testing.T) {
11+
for subdomain, s := range map[string]string{
12+
"": "Aug 29, 2018, 5:51:19 PM",
13+
"br": "31 de ago de 2018 00:50:19",
14+
"ru": "1 сент. 2018 г., 2:09:52",
15+
"th": "31 ส.ค. 2018 00:50:25",
16+
"de": "31.08.2018, 00:50:20",
17+
"fr": "31 août 2018 00:50:22",
18+
"es": "31 ago. 2018 0:50:23",
19+
} {
20+
t.Run(subdomain, func(t *testing.T) {
21+
var locale *Locale
22+
for _, l := range Locales {
23+
if l.Subdomain == subdomain {
24+
locale = l
25+
break
26+
}
27+
}
28+
_, err := locale.ParseTime(s, time.FixedZone("UTC-5", -5*60*60))
29+
assert.NoError(t, err)
30+
})
31+
}
32+
}

0 commit comments

Comments
 (0)