I think many will agree that working with dates in almost any programming language is an incredible headache. Date and time are not decimal, time zones - my God, why they cannot be just integers - and of course, countless formats of date and time. You can, of course, argue that they say there is ISO 8601 and what else do you need a fool for, but let's face it, letβs say - how often have you encountered compliance with this standard in third-party APIs? I donβt know how things are going abroad with this, I hope they will tell me in the comments, but in the post-Soviet expanses the situation is to hug and cry. Each uses its own, only convenient for him, format of time and date and deal with it as you want.
I will talk about my own experience and about the solution found.
The problem I was facing did not look so bad, the date format in the API that I had to deal with looked like this: YYYY-MM-DD hh: mm: ss . Looks like normal, doesn't it?
No.
Absolutely abnormal.
Those familiar with the ISO 8601 format have already realized the catch. For clarity:
package main
import (
"encoding/json"
"log"
"time"
)
type Dated struct {
DateTime time.Time
}
func main() {
input := []byte("{\"datetime\": \"1900-01-01 12:00:04\"}")
var d Dated
err := json.Unmarshal(input, &d)
if err != nil {
log.Fatal(err)
}
}
As a result of what? That's right, the error is:
parsing time ""1900-01-01 12:00:04"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 12:00:04"" as "T"
Which is logical, because Go out of the box has a limited number of time and date formats
that it can parse:
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700"
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700"
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
From here: https://golang.org/src/time/format.go
As you can see, the format YYYY-MM-DD hh:mm:ss
is not there. Okay, the trouble is clear, but what to do then?
, , Go json.Unmarshall
. β , JSON, , , UnmasrhallJSON,
.
, ,
, Unmarshaler, UnmasrhallJSON:
type CustomDate struct {
time.Time
}
func (c *CustomDate) UnmarshalJSON(b []byte) (err error) {
layout := "2006-01-02 15:04:05"
s := strings.Trim(string(b), "\"")
if s == "null" {
return
}
c.Time, err = time.Parse(layout, s)
return
}
type Dated struct {
DateTime CustomDate
}
, , API, , , . :
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
)
type CustomDate struct {
time.Time
}
const layout = "2006-01-02 15:04:05"
func (c *CustomDate) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), `"`)
if s == "null" {
return
}
c.Time, err = time.Parse(layout, s)
return
}
func (c CustomDate) MarshalJSON() ([]byte, error) {
if c.Time.IsZero() {
return nil, nil
}
return []byte(fmt.Sprintf(`"%s"`, c.Time.Format(layout))), nil
}
type Dated struct {
DateTime CustomDate
}
func main() {
input := []byte("{\"datetime\": \"1900-01-01 12:00:04\"}")
var d Dated
err := json.Unmarshal(input, &d)
if err != nil {
log.Fatal(err)
}
log.Println("Unmarshal:")
log.Println(d.DateTime)
b, err := json.Marshal(d)
if err != nil {
log.Fatal(err)
}
log.Println("Marshal:")
log.Println(string(b))
}
Marshal/Unmarshal. β MarchalJSON , UnmarshalJSON β
:
Unmarshal:
1900-01-01 12:00:04 +0000 UTC
Marshal:
{"DateTime":"1900-01-01T12:00:04Z"}
, , .
Go β , , /, , ; β .
If you are ready to offer other interesting ways to solve this problem - I will be glad to see them in the comments. Well, in conclusion, I want to wish you less to deal with non-standard formats of anything.
Thanks to all.