Skip to content

Commit

Permalink
Merge pull request #20 from aj3sh/formatter-error
Browse files Browse the repository at this point in the history
feat(formatter)!: removed error return from Format method
  • Loading branch information
subashcs authored Apr 1, 2024
2 parents 8a4517b + 102df7a commit f3d3bf8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 87 deletions.
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

0 comments on commit f3d3bf8

Please sign in to comment.