Skip to content

Commit

Permalink
returned Date type for proton date (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11ow authored Jan 11, 2023
1 parent 775922b commit 057ab1e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
3 changes: 3 additions & 0 deletions lib/column/column_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions lib/column/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/timeplus-io/proton-go-driver/v2/lib/binary"
"github.com/timeplus-io/proton-go-driver/v2/types"
)

var (
Expand All @@ -39,7 +40,7 @@ func (dt *Date) Type() Type {
}

func (col *Date) ScanType() reflect.Type {
return scanTypeTime
return scanTypeDate
}

func (dt *Date) Rows() int {
Expand All @@ -57,10 +58,10 @@ func (dt *Date) Row(i int, ptr bool) interface{} {
func (dt *Date) ScanRow(dest interface{}, row int) error {
switch d := dest.(type) {
case *time.Time:
*d = dt.row(row)
*d = dt.row(row).Time
case **time.Time:
*d = new(time.Time)
**d = dt.row(row)
**d = dt.row(row).Time
default:
return &ColumnConverterError{
Op: "ScanRow",
Expand Down Expand Up @@ -140,8 +141,8 @@ func (dt *Date) Encode(encoder *binary.Encoder) error {
return dt.values.Encode(encoder)
}

func (dt *Date) row(i int) time.Time {
return time.Unix(int64(dt.values[i])*secInDay, 0).UTC()
func (dt *Date) row(i int) types.Date {
return types.Date{Time: time.Unix(int64(dt.values[i])*secInDay, 0).UTC()}
}

var _ Interface = (*Date)(nil)
11 changes: 6 additions & 5 deletions lib/column/date32.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/timeplus-io/proton-go-driver/v2/lib/binary"
"github.com/timeplus-io/proton-go-driver/v2/types"
)

var (
Expand All @@ -39,7 +40,7 @@ func (dt *Date32) Type() Type {
}

func (col *Date32) ScanType() reflect.Type {
return scanTypeTime
return scanTypeDate
}

func (dt *Date32) Rows() int {
Expand All @@ -57,10 +58,10 @@ func (dt *Date32) Row(i int, ptr bool) interface{} {
func (dt *Date32) ScanRow(dest interface{}, row int) error {
switch d := dest.(type) {
case *time.Time:
*d = dt.row(row)
*d = dt.row(row).Time
case **time.Time:
*d = new(time.Time)
**d = dt.row(row)
**d = dt.row(row).Time
default:
return &ColumnConverterError{
Op: "ScanRow",
Expand Down Expand Up @@ -140,8 +141,8 @@ func (dt *Date32) Encode(encoder *binary.Encoder) error {
return dt.values.Encode(encoder)
}

func (dt *Date32) row(i int) time.Time {
return time.Unix((int64(dt.values[i]) * secInDay), 0).UTC()
func (dt *Date32) row(i int) types.Date {
return types.Date{Time: time.Unix((int64(dt.values[i]) * secInDay), 0).UTC()}
}

func timeToInt32(t time.Time) int32 {
Expand Down
14 changes: 14 additions & 0 deletions types/date.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

import (
"fmt"
"time"
)

type Date struct {
time.Time
}

func (d Date) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", d.Format("2006-01-02"))), nil
}
14 changes: 14 additions & 0 deletions types/datetime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

import (
"fmt"
"time"
)

type Datetime struct {
time.Time
}

func (dt Datetime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", dt.Format("2006-01-02 15:04:05"))), nil
}

0 comments on commit 057ab1e

Please sign in to comment.