Skip to content

Commit

Permalink
feat: support multiple S in format layout (#2917)
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <[email protected]>
  • Loading branch information
Yisaer committed Jun 14, 2024
1 parent 26f6cbd commit 5840f5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
22 changes: 10 additions & 12 deletions pkg/cast/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cast

import (
"bytes"
"fmt"
"time"

Expand Down Expand Up @@ -299,22 +300,19 @@ func convertFormat(f string) (string, error) {
out += "05"
}

case 'S': // S SS SSS
j := 1
for ; i+j < lenFormat && j <= 3; j++ {
if formatRune[i+j] != r {
case 'S': // S SS SSS....
j := 0
for ; i+j < lenFormat; j++ {
if formatRune[i+j] != 'S' {
break
}
}
i = i + j - 1
switch j {
case 1: // S
out += ".0"
case 2: // SS
out += ".00"
case 3: // SSS
out += ".000"
b := bytes.NewBufferString(".")
for x := 0; x < j; x++ {
b.WriteString("0")
}
out += b.String()
i = i + j - 1
case 'z': // z
out += "MST"
case 'Z': // Z
Expand Down
8 changes: 8 additions & 0 deletions pkg/cast/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,12 @@ func TestConvertFormat(t *testing.T) {

s, err = convertFormat("\\")
require.Error(t, err)

s, err = convertFormat("yyyy-MM-dd HH:mm:ssSSSSSSSXX")
require.NoError(t, err)
require.Equal(t, "2006-01-02 15:04:05.0000000-0700", s)

d, err := time.Parse("2006-01-02 15:04:05.0000000-0700", `2024-06-10 05:54:39.6574979-0700`)
require.NoError(t, err)
require.Equal(t, int64(1718024079657497900), d.UnixNano())
}

0 comments on commit 5840f5e

Please sign in to comment.