mirror of
https://github.com/jackc/pgx.git
synced 2025-05-21 23:09:47 +00:00
Use date as date, not datetime.
Marshal/unmarshal date without time part. Date is postgresql type without time.
This commit is contained in:
parent
2492eae46c
commit
b4c77819da
@ -3,7 +3,7 @@ package pgtype
|
|||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/pgio"
|
"github.com/jackc/pgx/pgio"
|
||||||
@ -212,11 +212,8 @@ func (src *Date) Value() (driver.Value, error) {
|
|||||||
func (src *Date) MarshalJSON() ([]byte, error) {
|
func (src *Date) MarshalJSON() ([]byte, error) {
|
||||||
switch src.Status {
|
switch src.Status {
|
||||||
case Present:
|
case Present:
|
||||||
res, err := src.Time.MarshalJSON()
|
s := fmt.Sprintf("%q", src.Time.Format("2006-01-02"))
|
||||||
if err != nil {
|
return []byte(s), nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return res, nil
|
|
||||||
case Null:
|
case Null:
|
||||||
return []byte("null"), nil
|
return []byte("null"), nil
|
||||||
case Undefined:
|
case Undefined:
|
||||||
@ -227,8 +224,8 @@ func (src *Date) MarshalJSON() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dst *Date) UnmarshalJSON(b []byte) error {
|
func (dst *Date) UnmarshalJSON(b []byte) error {
|
||||||
var n time.Time
|
n, err := time.Parse("\"2006-01-02\"", string(b))
|
||||||
if err := json.Unmarshal(b, &n); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,21 +125,19 @@ func TestMarshalJSON(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(enc) != "\"1900-01-01T00:00:00Z\"" {
|
if string(enc) != "\"1900-01-01\"" {
|
||||||
t.Errorf("Incorrect json marshal")
|
t.Errorf("Incorrect json marshal")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalJSON(t *testing.T) {
|
func TestUnmarshalJSON(t *testing.T) {
|
||||||
var r pgtype.Date
|
var r pgtype.Date
|
||||||
tm := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
|
if err := r.UnmarshalJSON([]byte("\"1900-01-01\"")); err != nil {
|
||||||
|
|
||||||
if err := r.UnmarshalJSON([]byte(`"` + tm.Format(time.RFC3339) + `"`)); err != nil {
|
|
||||||
t.Errorf("%v", err)
|
t.Errorf("%v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if tm != r.Time {
|
if r.Time.Year() != 1900 || r.Time.Month() != 1 || r.Time.Day() != 1 {
|
||||||
t.Errorf("Incorrect json unmarshal")
|
t.Errorf("Incorrect json unmarshal")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user