forked from goodsign/monday
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformat_common.go
142 lines (124 loc) · 4.05 KB
/
format_common.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
package monday
import "strings"
func findInString(where string, what string, foundIndex *int, trimRight *int) (found bool) {
ind := strings.Index(where, what)
if ind != -1 {
*foundIndex = ind
*trimRight = len(where) - ind - len(what)
return true
}
return false
}
// commonFormatFunc is used for languages which don't have changed forms of month names dependent
// on their position (after day or standalone)
func commonFormatFunc(value, format string,
knownDaysShort, knownDaysLong, knownMonthsShort, knownMonthsLong, knownPeriods map[string]string) (res string) {
l := stringToLayoutItems(value)
f := stringToLayoutItems(format)
for i, v := range l {
var knw map[string]string
// number of symbols before replaced term
foundIndex := 0
trimRight := 0
lowerCase := false
switch {
case findInString(f[i].item, "Monday", &foundIndex, &trimRight):
knw = knownDaysLong
case findInString(f[i].item, "Mon", &foundIndex, &trimRight):
knw = knownDaysShort
case findInString(f[i].item, "January", &foundIndex, &trimRight):
knw = knownMonthsLong
case findInString(f[i].item, "Jan", &foundIndex, &trimRight):
knw = knownMonthsShort
case findInString(f[i].item, "PM", &foundIndex, &trimRight):
knw = knownPeriods
case findInString(f[i].item, "pm", &foundIndex, &trimRight):
lowerCase = true
knw = knownPeriods
}
if knw != nil {
trimmedItem := v.item[foundIndex : len(v.item)-trimRight]
tr, ok := knw[trimmedItem]
if lowerCase == true {
tr = strings.ToLower(tr)
}
if ok {
res = res + v.item[:foundIndex] + tr + v.item[len(v.item)-trimRight:]
} else {
res = res + v.item
}
} else {
res = res + v.item
}
}
return res
}
func hasDigitBefore(l []dateStringLayoutItem, position int) bool {
if position >= 2 {
return l[position-2].isDigit && len(l[position-2].item) <= 2
}
return false
}
// commonGenitiveFormatFunc is used for languages with genitive forms of names, like Russian.
func commonGenitiveFormatFunc(value, format string,
knownDaysShort, knownDaysLong, knownMonthsShort, knownMonthsLong,
knownMonthsGenShort, knownMonthsGenLong, knownPeriods map[string]string) (res string) {
l := stringToLayoutItems(value)
f := stringToLayoutItems(format)
for i, v := range l {
var knw map[string]string
switch f[i].item {
case "Mon":
knw = knownDaysShort
case "Monday":
knw = knownDaysLong
case "Jan":
if hasDigitBefore(l, i) {
knw = knownMonthsGenShort
} else {
knw = knownMonthsShort
}
case "January":
if hasDigitBefore(l, i) {
knw = knownMonthsGenLong
} else {
knw = knownMonthsLong
}
}
if knw != nil {
tr, _ := knw[v.item]
res = res + tr
} else {
res = res + v.item
}
}
return res
}
func createCommonFormatFunc(locale Locale) internalFormatFunc {
return func(value, layout string) (res string) {
return commonFormatFunc(value, layout,
knownDaysShort[locale], knownDaysLong[locale], knownMonthsShort[locale], knownMonthsLong[locale], knownPeriods[locale])
}
}
func createCommonFormatFuncWithGenitive(locale Locale) internalFormatFunc {
return func(value, layout string) (res string) {
return commonGenitiveFormatFunc(value, layout,
knownDaysShort[locale], knownDaysLong[locale], knownMonthsShort[locale], knownMonthsLong[locale],
knownMonthsGenitiveShort[locale], knownMonthsGenitiveLong[locale], knownPeriods[locale])
}
}
func createCommonParseFunc(locale Locale) internalParseFunc {
return func(layout, value string) string {
return commonFormatFunc(value, layout,
knownDaysShortReverse[locale], knownDaysLongReverse[locale],
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale], knownPeriods[locale])
}
}
func createCommonParsetFuncWithGenitive(locale Locale) internalParseFunc {
return func(layout, value string) (res string) {
return commonGenitiveFormatFunc(value, layout,
knownDaysShortReverse[locale], knownDaysLongReverse[locale],
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale],
knownMonthsGenitiveShortReverse[locale], knownMonthsGenitiveLongReverse[locale], knownPeriods[locale])
}
}