Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formatter)!: removed error return from Format method #20

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 24 additions & 30 deletions nepalitime/formatter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nepalitime

import (
"errors"
"strconv"
"strings"
)
Expand All @@ -14,7 +13,7 @@ type NepaliFormatter struct {
nepaliTime *NepaliTime
}

func (obj *NepaliFormatter) Format(format string) (string, error) {
func (obj *NepaliFormatter) Format(format string) string {
index, num, timeStr := 0, len(format), ""

for index < num {
Expand All @@ -32,17 +31,11 @@ func (obj *NepaliFormatter) Format(format string) (string, error) {
if (index + 1) < num {
index++
char = string(format[index])
res, err := obj.getFormatString(specialChar + char)
if err != nil {
return "", errors.New("error while formatting NepaliTime with given format")
}
res := obj.getFormatString(specialChar + char)
timeStr += res
}
} else {
res, err := obj.getFormatString(char)
if err != nil {
return "", errors.New("error while formatting NepaliTime with given format")
}
res := obj.getFormatString(char)
timeStr += res
}
index++
Expand All @@ -51,48 +44,49 @@ func (obj *NepaliFormatter) Format(format string) (string, error) {
}
}

return timeStr, nil
return timeStr
}

// utility method that operates based on the type of directive
func (obj *NepaliFormatter) getFormatString(directive string) (string, error) {
func (obj *NepaliFormatter) getFormatString(directive string) string {
switch directive {
case "d":
return obj.day_(), nil
return obj.day_()
case "-d":
return obj.dayNonzero(), nil
return obj.dayNonzero()
case "m":
return obj.monthNumber(), nil
return obj.monthNumber()
case "-m":
return obj.monthNumberNonzero(), nil
return obj.monthNumberNonzero()
case "y":
return obj.yearHalf(), nil
return obj.yearHalf()
case "Y":
return obj.yearFull(), nil
return obj.yearFull()
case "H":
return obj.hour24(), nil
return obj.hour24()
case "-H":
return obj.hour24Nonzero(), nil
return obj.hour24Nonzero()
case "I":
return obj.hour12(), nil
return obj.hour12()
case "-I":
return obj.hour12Nonzero(), nil
return obj.hour12Nonzero()
case "p":
return obj.ampm(), nil
return obj.ampm()
case "M":
return obj.minute(), nil
return obj.minute()
case "-M":
return obj.minuteNonzero(), nil
return obj.minuteNonzero()
case "S":
return obj.second(), nil
return obj.second()
case "-S":
return obj.secondNonzero(), nil
return obj.secondNonzero()
case "f":
return obj.nanosecond_(), nil
return obj.nanosecond_()
case "-f":
return obj.nanosecondNonZero(), nil
return obj.nanosecondNonZero()
default:
return "", errors.New("error while getting format string for passed directive")
// if not match return the directive
return "%" + directive
}
}

Expand Down
Loading
Loading