Skip to content

Commit 6ea7f80

Browse files
author
Bartlomiej Mika
committed
Added a few more funcs
1 parent 5a7e245 commit 6ea7f80

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

constants.go

+16
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,19 @@ var monthAbbreviations = map[time.Month]string{
2020
time.November: "Nov",
2121
time.December: "Dec",
2222
}
23+
24+
// monthNumberAbbreviations is a mapping from month numbers (1 to 12) to their three-letter abbreviations.
25+
var monthNumberAbbreviations = map[int]string{
26+
1: "Jan", // January
27+
2: "Feb", // February
28+
3: "Mar", // March
29+
4: "Apr", // April
30+
5: "May", // May
31+
6: "Jun", // June
32+
7: "Jul", // July
33+
8: "Aug", // August
34+
9: "Sep", // September
35+
10: "Oct", // October
36+
11: "Nov", // November
37+
12: "Dec", // December
38+
}

conversion.go

+9
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,12 @@ func GetMonthAbbreviation(month time.Month) string {
8080
}
8181
return abbreviation
8282
}
83+
84+
// GetMonthAbbreviationByInt returns the 3-character abbreviation for the provided month number.
85+
func GetMonthAbbreviationByInt(month int) string {
86+
abbreviation, found := monthNumberAbbreviations[month]
87+
if !found {
88+
return ""
89+
}
90+
return abbreviation
91+
}

conversion_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ func TestGetMonthAbbreviation(t *testing.T) {
227227
{time.October, "Oct"},
228228
{time.November, "Nov"},
229229
{time.December, "Dec"},
230+
{time.January - 1, ""}, // This case should return an empty string.
230231
}
231232

232233
for _, tc := range testCases {
@@ -238,3 +239,23 @@ func TestGetMonthAbbreviation(t *testing.T) {
238239
})
239240
}
240241
}
242+
243+
func TestGetMonthAbbreviationByInt(t *testing.T) {
244+
t.Run("ValidMonth", func(t *testing.T) {
245+
month := 3
246+
abbreviation := GetMonthAbbreviationByInt(month)
247+
expected := "Mar"
248+
if abbreviation != expected {
249+
t.Errorf("Expected abbreviation for March is %s, but got %s", expected, abbreviation)
250+
}
251+
})
252+
253+
t.Run("InvalidMonth", func(t *testing.T) { // This case should return an empty string.
254+
month := 13
255+
abbreviation := GetMonthAbbreviationByInt(month)
256+
expected := ""
257+
if abbreviation != expected {
258+
t.Errorf("Expected abbreviation for an invalid month is an empty string, but got %s", abbreviation)
259+
}
260+
})
261+
}

0 commit comments

Comments
 (0)