-
Notifications
You must be signed in to change notification settings - Fork 1
/
date_formatter.go
160 lines (135 loc) · 2.96 KB
/
date_formatter.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
package hrrule
import (
"strconv"
"strings"
"time"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
const (
commaByte = ','
monthLayout = "January"
weekDayLayout = "Monday"
UNKNOWN = "UNKNOWN"
)
var longMonthNames = []string{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
var longDayNames = []string{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
}
type formatterImpl struct {
loc *i18n.Localizer
}
func NewDateFormatterSimple(loc *i18n.Localizer) DateFormatter {
return &formatterImpl{
loc: loc,
}
}
func (df *formatterImpl) Format(year int, month time.Month, day int) string {
var date strings.Builder
date.Grow(32)
date.WriteString(strconv.Itoa(day))
date.WriteByte(spaceByte)
date.WriteString(monthName(month, df.loc))
date.WriteByte(spaceByte)
date.WriteString(strconv.Itoa(year))
// TODO: weekday from year, month, day
// timing.WriteString(weekdayName(weekday, lang))
return date.String()
}
func (df *formatterImpl) MonthName(month time.Month) string {
return monthName(month, df.loc)
}
// Nth return int with suffix
func (df *formatterImpl) Nth(i int) string {
var last string
if i < 0 {
last = df.loc.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{
ID: "Last",
Description: "The last for Nth return int with suffix",
Other: "last",
}})
}
if i == -1 {
return last
}
nPos := abs(i)
var nth strings.Builder
nth.WriteString(strconv.Itoa(nPos))
switch nPos {
case 1, 21, 31:
nth.WriteString(df.loc.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{
ID: "First",
Description: "Suffix for 1, 21, 31",
Other: "st",
}}))
case 2, 22:
nth.WriteString(df.loc.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{
ID: "Second",
Description: "Suffix for 2, 22",
Other: "nd",
}}))
case 3, 23:
nth.WriteString(df.loc.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{
ID: "Third",
Description: "Suffix for 3, 23",
Other: "rd",
}}))
default:
nth.WriteString(df.loc.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{
ID: "ThOther",
Description: "Suffix for other numbers",
Other: "th",
}}))
}
if i < 0 {
nth.WriteByte(spaceByte)
nth.WriteString(last)
return nth.String()
}
return nth.String()
}
// TODO: implement me
func (df *formatterImpl) WeekdayName(wDay Weekday) string {
weekday := getGoWeekday(wDay.weekday)
var sb strings.Builder
sb.Grow(16)
if wDay.n != 0 {
sb.WriteString(df.Nth(wDay.n))
sb.WriteByte(spaceByte)
}
sb.WriteString(time.Weekday(weekday).String())
return sb.String()
}
func monthName(month time.Month, _ *i18n.Localizer) string {
return month.String()
}
func getGoWeekday(num int) int {
if num == 6 {
return 0
}
return num + 1
}
func abs(n int) int {
if n < 0 {
return -n
}
return n
}