Skip to content

Commit

Permalink
feat: support esacape character for time layout (#2844)
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <[email protected]>
  • Loading branch information
Yisaer authored May 11, 2024
1 parent 9071176 commit 8aacb1b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/en_US/sqls/functions/string_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ common in many languages like Java, etc. The supported symbols in Kuiper are
| z | time zone name | z(MST) |
| Z | 4 digits time zone offset | Z(-0700) |
| X | time zone offset | X(-07), XX(-0700), XXX(-07:00) |
| \ | Escape character | \Z(Z) \X(X) |

Examples:

- YYYY-MM-dd T HH:mm:ss -> 2006-01-02 T 15:04:05
- YYYY/MM/dd HH:mm:ssSSS XXX -> 2006/01/02 15:04:05.000 -07:00
- yyyy-MM-ddTHH:mm:ssSS\ZXX -> 2006-01-02T15:04:05.00Z-0700

## INDEXOF

Expand Down
2 changes: 2 additions & 0 deletions docs/zh_CN/sqls/functions/string_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ format_time(col, format)
| z | 时区名 | z(MST) |
| Z | 4位数的时区 | Z(-0700) |
| X | 时区 | X(-07), XX(-0700), XXX(-07:00) |
| \ | 转义符 | \Z(Z) \X(X) |

示例:

- YYYY-MM-dd T HH:mm:ss -> 2006-01-02 T 15:04:05
- YYYY/MM/dd HH:mm:ssSSS XXX -> 2006/01/02 15:04:05.000 -07:00
- yyyy-MM-ddTHH:mm:ssSS\ZXX -> 2006-01-02T15:04:05.00Z-0700

## INDEXOF

Expand Down
6 changes: 6 additions & 0 deletions pkg/cast/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ func convertFormat(f string) (string, error) {
out := ""
for i := 0; i < len(formatRune); i++ {
switch r := formatRune[i]; r {
case '\\':
i = i + 1
if i >= len(formatRune) {
return "", fmt.Errorf("%s is invalid", f)
}
out += string(formatRune[i])
case 'Y', 'y':
j := 1
for ; i+j < lenFormat && j <= 4; j++ {
Expand Down
9 changes: 9 additions & 0 deletions pkg/cast/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,12 @@ func TestInterfaceToUnixMilli(t *testing.T) {
assert.Equal(t, tt.want, got)
}
}

func TestConvertFormat(t *testing.T) {
s, err := convertFormat("yyyy-MM-ddTHH:mm:ssSS\\ZXX")
require.NoError(t, err)
require.Equal(t, "2006-01-02T15:04:05.00Z-0700", s)

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

0 comments on commit 8aacb1b

Please sign in to comment.