Pad date and time helper strings with double digits

This commit is contained in:
Ice3man 2022-03-21 16:32:21 +05:30
parent bebdb29706
commit be0e4b227c
2 changed files with 20 additions and 8 deletions

View File

@ -161,11 +161,11 @@ func init() {
now := time.Now() now := time.Now()
switch value[1] { switch value[1] {
case "Y", "y": case "Y", "y":
item = strings.ReplaceAll(item, value[0], strconv.Itoa(now.Year())) item = strings.ReplaceAll(item, value[0], appendSingleDigitZero(strconv.Itoa(now.Year())))
case "M", "m": case "M", "m":
item = strings.ReplaceAll(item, value[0], strconv.Itoa(int(now.Month()))) item = strings.ReplaceAll(item, value[0], appendSingleDigitZero(strconv.Itoa(int(now.Month()))))
case "D", "d": case "D", "d":
item = strings.ReplaceAll(item, value[0], strconv.Itoa(now.Day())) item = strings.ReplaceAll(item, value[0], appendSingleDigitZero(strconv.Itoa(now.Day())))
default: default:
return nil, fmt.Errorf("invalid date format string: %s", value[0]) return nil, fmt.Errorf("invalid date format string: %s", value[0])
} }
@ -182,11 +182,11 @@ func init() {
now := time.Now() now := time.Now()
switch value[1] { switch value[1] {
case "H", "h": case "H", "h":
item = strings.ReplaceAll(item, value[0], strconv.Itoa(now.Hour())) item = strings.ReplaceAll(item, value[0], appendSingleDigitZero(strconv.Itoa(now.Hour())))
case "M", "m": case "M", "m":
item = strings.ReplaceAll(item, value[0], strconv.Itoa(now.Minute())) item = strings.ReplaceAll(item, value[0], appendSingleDigitZero(strconv.Itoa(now.Minute())))
case "S", "s": case "S", "s":
item = strings.ReplaceAll(item, value[0], strconv.Itoa(now.Second())) item = strings.ReplaceAll(item, value[0], appendSingleDigitZero(strconv.Itoa(now.Second())))
default: default:
return nil, fmt.Errorf("invalid time format string: %s", value[0]) return nil, fmt.Errorf("invalid time format string: %s", value[0])
} }
@ -466,6 +466,18 @@ func init() {
} }
} }
// appendSingleDigitZero appends zero at front if not exists already doing two digit padding
func appendSingleDigitZero(value string) string {
if len(value) == 1 && !strings.HasPrefix(value, "0") {
builder := &strings.Builder{}
builder.WriteRune('0')
builder.WriteString(value)
newVal := builder.String()
return newVal
}
return value
}
func createSignaturePart(numberOfParameters int) string { func createSignaturePart(numberOfParameters int) string {
params := make([]string, 0, numberOfParameters) params := make([]string, 0, numberOfParameters)
for i := 1; i <= numberOfParameters; i++ { for i := 1; i <= numberOfParameters; i++ {

View File

@ -177,8 +177,8 @@ func TestDslExpressions(t *testing.T) {
`hex_encode("aa")`: "6161", `hex_encode("aa")`: "6161",
`html_escape("<body>test</body>")`: "&lt;body&gt;test&lt;/body&gt;", `html_escape("<body>test</body>")`: "&lt;body&gt;test&lt;/body&gt;",
`html_unescape("&lt;body&gt;test&lt;/body&gt;")`: "<body>test</body>", `html_unescape("&lt;body&gt;test&lt;/body&gt;")`: "<body>test</body>",
`date("%Y-%M-%D")`: fmt.Sprintf("%d-%d-%d", now.Year(), now.Month(), now.Day()), `date("%Y-%M-%D")`: fmt.Sprintf("%02d-%02d-%02d", now.Year(), now.Month(), now.Day()),
`time("%H-%M")`: fmt.Sprintf("%d-%d", now.Hour(), now.Minute()), `time("%H-%M")`: fmt.Sprintf("%02d-%02d", now.Hour(), now.Minute()),
`md5("Hello")`: "8b1a9953c4611296a827abf8c47804d7", `md5("Hello")`: "8b1a9953c4611296a827abf8c47804d7",
`md5(1234)`: "81dc9bdb52d04dc20036dbd8313ed055", `md5(1234)`: "81dc9bdb52d04dc20036dbd8313ed055",
`mmh3("Hello")`: "316307400", `mmh3("Hello")`: "316307400",