Skip to content

Commit

Permalink
Unify timestamp format used by adapter (#12)
Browse files Browse the repository at this point in the history
* Unify timestamp format used by adapter

* Refactor and move time formating to reusable function
  • Loading branch information
lafriks committed Nov 5, 2021
1 parent 873dc18 commit 6b33dfc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ docker run -it --rm -p 5433:5432 -e "POSTGRES_USER=rel" -e "POSTGRES_PASSWORD=te
### Run tests

```console
POSTGRESQL_DATABASE="postgres://rel:test@localhost:5433/rel_test?timezone=Asia/Jakarta" go test ./...
POSTGRESQL_DATABASE="postgres://rel:test@localhost:5433/rel_test" go test ./...
```
2 changes: 1 addition & 1 deletion postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func columnMapper(column *rel.Column) (string, int, int) {
case rel.DateTime:
typ = "TIMESTAMPTZ"
if t, ok := column.Default.(time.Time); ok {
column.Default = t.Format("2006-01-02 15:04:05")
column.Default = FormatTime(t)
}
case rel.Int, rel.BigInt, rel.Text:
column.Limit = 0
Expand Down
2 changes: 1 addition & 1 deletion quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ func (c ValueConvert) ConvertValue(v interface{}) (driver.Value, error) {
default:
return v, nil
case time.Time:
return v.Truncate(time.Microsecond).Format("2006-01-02 15:04:05.999999999Z07:00:00"), nil
return FormatTime(v), nil
}
}
13 changes: 13 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package postgres

import (
"time"
)

// TimeLayout used by PostgreSQL adapter.
const TimeLayout = "2006-01-02 15:04:05.999999999Z07:00:00"

// FormatTime formats time to PostgreSQL format.
func FormatTime(t time.Time) string {
return t.Truncate(time.Microsecond).Format(TimeLayout)
}

0 comments on commit 6b33dfc

Please sign in to comment.