This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DateOnly struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (d DateOnly) MarshalJSON() ([]byte, error) {
|
||||
if d.IsZero() {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
return json.Marshal(d.Format("2006-01-02"))
|
||||
}
|
||||
|
||||
func (d *DateOnly) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
t, err := time.Parse("2006-01-02", s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.Time = t
|
||||
return nil
|
||||
}
|
||||
|
||||
type KontorTime struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
const layout = "2006-01-02 15:04:05.000000"
|
||||
|
||||
func (kt KontorTime) MarshalJSON() ([]byte, error) {
|
||||
if kt.IsZero() {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
return json.Marshal(kt.Format(layout))
|
||||
}
|
||||
|
||||
func (kt *KontorTime) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
t, err := time.Parse(layout, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kt.Time = t
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user