Skip to content

Commit

Permalink
feat: add timetz parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 29, 2021
1 parent cf07e55 commit 6e415c4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
12 changes: 11 additions & 1 deletion internal/dbtest/pg_test.go
Expand Up @@ -313,7 +313,7 @@ func TestPGDate(t *testing.T) {
defer db.Close()

var str string
err := db.NewSelect().ColumnExpr("'2021-09-15'::DATE").Scan(ctx, &str)
err := db.NewSelect().ColumnExpr("'2021-09-15'::date").Scan(ctx, &str)
require.NoError(t, err)
require.Equal(t, "2021-09-15", str)

Expand All @@ -332,3 +332,13 @@ func TestPGDate(t *testing.T) {
require.NoError(t, err)
require.False(t, nullTime.IsZero())
}

func TestPGTimetz(t *testing.T) {
db := pg(t)
defer db.Close()

var tm time.Time
err := db.NewSelect().ColumnExpr("now()::timetz").Scan(ctx, &tm)
require.NoError(t, err)
require.NotZero(t, tm)
}
54 changes: 37 additions & 17 deletions internal/time.go
Expand Up @@ -8,34 +8,54 @@ import (
const (
dateFormat = "2006-01-02"
timeFormat = "15:04:05.999999999"
timetzFormat1 = "15:04:05.999999999-07:00:00"
timetzFormat2 = "15:04:05.999999999-07:00"
timetzFormat3 = "15:04:05.999999999-07"
timestampFormat = "2006-01-02 15:04:05.999999999"
timestamptzFormat = "2006-01-02 15:04:05.999999999-07:00:00"
timestamptzFormat1 = "2006-01-02 15:04:05.999999999-07:00:00"
timestamptzFormat2 = "2006-01-02 15:04:05.999999999-07:00"
timestamptzFormat3 = "2006-01-02 15:04:05.999999999-07"
)

func ParseTime(s string) (time.Time, error) {
switch l := len(s); {
case l < len("15:04:05"):
return time.Time{}, fmt.Errorf("bun: can't parse time=%q", s)
case l <= len(timeFormat):
if s[2] == ':' {
return time.ParseInLocation(timeFormat, s, time.UTC)
}
return time.ParseInLocation(dateFormat, s, time.UTC)
default:
if s[10] == 'T' {
l := len(s)

if l >= len("2006-01-02 15:04:05") {
switch s[10] {
case ' ':
if c := s[l-6]; c == '+' || c == '-' {
return time.Parse(timestamptzFormat2, s)
}
if c := s[l-3]; c == '+' || c == '-' {
return time.Parse(timestamptzFormat3, s)
}
if c := s[l-9]; c == '+' || c == '-' {
return time.Parse(timestamptzFormat1, s)
}
return time.ParseInLocation(timestampFormat, s, time.UTC)
case 'T':
return time.Parse(time.RFC3339Nano, s)
}
if c := s[l-9]; c == '+' || c == '-' {
return time.Parse(timestamptzFormat, s)
}
}

if l >= len("15:04:05-07") {
if c := s[l-6]; c == '+' || c == '-' {
return time.Parse(timestamptzFormat2, s)
return time.Parse(timetzFormat2, s)
}
if c := s[l-3]; c == '+' || c == '-' {
return time.Parse(timestamptzFormat3, s)
return time.Parse(timetzFormat3, s)
}
return time.ParseInLocation(timestampFormat, s, time.UTC)
if c := s[l-9]; c == '+' || c == '-' {
return time.Parse(timetzFormat1, s)
}
}

if l < len("15:04:05") {
return time.Time{}, fmt.Errorf("bun: can't parse time=%q", s)
}

if s[2] == ':' {
return time.ParseInLocation(timeFormat, s, time.UTC)
}
return time.ParseInLocation(dateFormat, s, time.UTC)
}

0 comments on commit 6e415c4

Please sign in to comment.