mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-21 20:36:03 +03:00
31 lines
668 B
Go
31 lines
668 B
Go
|
package retailcrm
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type SystemTime time.Time
|
||
|
|
||
|
const systemTimeLayout = "2006-01-02 15:04:05"
|
||
|
|
||
|
// UnmarshalJSON parses time.Time from system format.
|
||
|
func (st *SystemTime) UnmarshalJSON(b []byte) (err error) {
|
||
|
s := strings.Trim(string(b), `"`)
|
||
|
nt, err := time.Parse(systemTimeLayout, s)
|
||
|
*st = SystemTime(nt)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// MarshalJSON will marshal time.Time to system format.
|
||
|
func (st SystemTime) MarshalJSON() ([]byte, error) {
|
||
|
return []byte(st.String()), nil
|
||
|
}
|
||
|
|
||
|
// String returns the time in the custom format
|
||
|
func (st *SystemTime) String() string {
|
||
|
t := time.Time(*st)
|
||
|
return fmt.Sprintf("%q", t.Format(systemTimeLayout))
|
||
|
}
|