Skip to content

Commit 438e0b1

Browse files
authored
feat: add Nepali months and weekdays strings for formatting (#27)
1 parent ddd3e0d commit 438e0b1

File tree

7 files changed

+102
-4
lines changed

7 files changed

+102
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ In this package, we provide 2 `go` packages, `nepalitime` and `dateConverter`.
128128
| `%-d` | Day of the month as a decimal number. | 1, 2, …, 31 |
129129
| `%m` | Month as a zero-padded decimal number. | 01, 02, …, 12 |
130130
| `%-m` | Month as a decimal number. | 1, 2, …, 12 |
131+
| `%B` | Month as a string | Baisakh, Jestha, ..., Chaitra |
132+
| `%A` | Full name of day of the week | Sunday, Monday, ..., Saturday |
133+
| `%a` | Half name of day of the week | Sun, Mon, ..., Sat |
131134
| `%y` | Year without century as a zero-padded decimal number. | 00, 01, …, 99 |
132135
| `%-y` | Year without century as a decimal number. | 0, 1, …, 99 |
133136
| `%Y` | Year with century as a zero-padded decimal number. | 0001, 0002, …, 2078, 2079, …, 9998, 9999 |

constants/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ package constants
33
const (
44
Timezone string = "Asia/Kathmandu"
55
)
6+
7+
var (
8+
NepaliMonths = [12]string{"Baisakh", "Jestha", "Ashadh", "Shrawan", "Bhadra", "Ashwin", "Kartik", "Mangsir", "Poush", "Magh", "Falgun", "Chaitra"}
9+
)

nepalitime/formatter.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package nepalitime
33
import (
44
"strconv"
55
"strings"
6+
7+
"github.com/opensource-nepal/go-nepali/constants"
68
)
79

810
func NewFormatter(nepaliTime *NepaliTime) *NepaliFormatter {
@@ -58,6 +60,12 @@ func (obj *NepaliFormatter) getFormatString(directive string) string {
5860
return obj.monthNumber()
5961
case "-m":
6062
return obj.monthNumberNonzero()
63+
case "B":
64+
return obj.monthName()
65+
case "A":
66+
return obj.weekDayFull()
67+
case "a":
68+
return obj.weekDayHalf()
6169
case "y":
6270
return obj.yearHalf()
6371
case "Y":
@@ -119,6 +127,21 @@ func (obj *NepaliFormatter) monthNumber() string {
119127
return month
120128
}
121129

130+
// %B
131+
func (obj *NepaliFormatter) monthName() string {
132+
return constants.NepaliMonths[obj.nepaliTime.month-1]
133+
}
134+
135+
// %A
136+
func (obj *NepaliFormatter) weekDayFull() string {
137+
return obj.nepaliTime.Weekday().String()
138+
}
139+
140+
// %a
141+
func (obj *NepaliFormatter) weekDayHalf() string {
142+
return obj.nepaliTime.Weekday().String()[:3]
143+
}
144+
122145
// %-m
123146
func (obj *NepaliFormatter) monthNumberNonzero() string {
124147
month := strconv.Itoa(obj.nepaliTime.month)

nepalitime/formatter_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ func TestNepaliFormatterFormatdmySlash(t *testing.T) {
3535
assert.Equal(t, "14/10/79", res, "%d/%m/%y did not match")
3636
}
3737

38+
func TestNepaliFormatterFormatdBYSlash(t *testing.T) {
39+
formatter := nepalitime.NewFormatter(globalNepaliTime)
40+
res := formatter.Format("%d/%B/%Y")
41+
42+
assert.Equal(t, "14/Magh/2079", res, "%d/%B/%Y did not match")
43+
}
44+
45+
func TestNepaliFormatterFormatdBYAComma(t *testing.T) {
46+
formatter := nepalitime.NewFormatter(globalNepaliTime)
47+
res := formatter.Format("%d %B, %Y, %A")
48+
49+
assert.Equal(t, "14 Magh, 2079, Saturday", res, "%d %B, %Y, %A did not match")
50+
}
51+
52+
func TestNepaliFormatterFormatdBYaComma(t *testing.T) {
53+
formatter := nepalitime.NewFormatter(globalNepaliTime)
54+
res := formatter.Format("%d %B, %Y, %a")
55+
56+
assert.Equal(t, "14 Magh, 2079, Sat", res, "%d %B, %Y, %a did not match")
57+
}
58+
3859
func TestNepaliFormatterFormatmdYSlash(t *testing.T) {
3960
formatter := nepalitime.NewFormatter(globalNepaliTime)
4061
res := formatter.Format("%m/%d/%Y")
@@ -77,6 +98,27 @@ func TestNepaliFormatterFormatdmyDash(t *testing.T) {
7798
assert.Equal(t, "14-10-79", res, "%d-%m-%y did not match")
7899
}
79100

101+
func TestNepaliFormatterFormatdBYDash(t *testing.T) {
102+
formatter := nepalitime.NewFormatter(globalNepaliTime)
103+
res := formatter.Format("%d-%B-%Y")
104+
105+
assert.Equal(t, "14-Magh-2079", res, "%d-%B-%Y-%A did not match")
106+
}
107+
108+
func TestNepaliFormatterFormatdBYADash(t *testing.T) {
109+
formatter := nepalitime.NewFormatter(globalNepaliTime)
110+
res := formatter.Format("%d-%B-%Y-%A")
111+
112+
assert.Equal(t, "14-Magh-2079-Saturday", res, "%d-%B-%Y-%A did not match")
113+
}
114+
115+
func TestNepaliFormatterFormatdBYaDash(t *testing.T) {
116+
formatter := nepalitime.NewFormatter(globalNepaliTime)
117+
res := formatter.Format("%d-%B-%Y-%a")
118+
119+
assert.Equal(t, "14-Magh-2079-Sat", res, "%d-%B-%Y-%a did not match")
120+
}
121+
80122
func TestNepaliFormatterFormatmdYDash(t *testing.T) {
81123
formatter := nepalitime.NewFormatter(globalNepaliTime)
82124
res := formatter.Format("%m-%d-%Y")

nepalitime/nepalitimeregex.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ func newNepaliTimeRegex() *nepaliTimeRegex {
3535
"y": `(?P<y>\d\d)`,
3636
"Y": `(?P<Y>\d\d\d\d)`,
3737
"z": `(?P<z>[+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?|(?-i:Z))`,
38-
39-
// "A": obj.__seqToRE(EnglishChar.days, "A"),
40-
// "a": obj.__seqToRE(EnglishChar.days_half, "a"),
41-
// "B": obj.__seqToRE(EnglishChar.months, "B"),
38+
"B": `(?P<B>Baisakh|Jestha|Ashadh|Shrawan|Bhadra|Ashwin|Kartik|Mangsir|Poush|Magh|Falgun|Chaitra")`,
39+
"A": `(?P<B>Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)`,
4240
// "b": obj.__seqToRE(EnglishChar.months, "b"),
4341
// "p": obj.__seqToRE(("AM", "PM",), "p"),
4442
// TODO: implement for the above commented directives

nepalitime/parser.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"errors"
55
"strconv"
66
"strings"
7+
8+
"github.com/opensource-nepal/go-nepali/constants"
79
)
810

911
var nepaliTimeReCache *nepaliTimeRegex
@@ -157,6 +159,19 @@ func transform(data map[string]string) (map[string]int, error) {
157159
}
158160

159161
day = intVal
162+
} else if key == "B" {
163+
intVal := 0
164+
for i, m := range constants.NepaliMonths {
165+
if m == val {
166+
intVal = i + 1
167+
break
168+
}
169+
}
170+
if intVal == 0 {
171+
return nil, errors.New("invalid value in %B")
172+
}
173+
174+
month = intVal
160175
} else if key == "H" {
161176
intVal, err := strconv.Atoi(val)
162177
if err != nil {

nepalitime/parser_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ func TestParserWithdmySlashForm(t *testing.T) {
6666
assert.Nil(t, err, "error should be nil")
6767
}
6868

69+
func TestParserWithdBYSlashForm(t *testing.T) {
70+
// 2079 magh 14
71+
datetimeStr := "14/Magh/2079"
72+
format := "%d/%B/%Y"
73+
74+
expected := "2079-10-14 00:00:00"
75+
76+
got, err := nepalitime.Parse(datetimeStr, format)
77+
78+
assert.Equal(t, expected, got.String(), fmt.Sprintf("expected: %s - got: %s", expected, got))
79+
assert.Nil(t, err, "error should be nil")
80+
}
81+
6982
func TestParserWithmdYSlashForm(t *testing.T) {
7083
// 2079 magh 14
7184
datetimeStr := "10/14/2079"

0 commit comments

Comments
 (0)