Parsing json date and time

I am trying to parse Datetime in revel from a json request. The query looks something like this:

{
  "startedAt": "2017-06-01 10:39",
  ...
}

The structure that it decodes is as follows:

type MyStruct struct {
  StartedAt time.Time `json:"startedAt" bson:"startedAt"`
  ...
}

The decoding line is as follows:

json.NewDecoder(c.Request.Body).Decode(&MyStruct)

Revel returns this error:

interface conversion: error is *time.ParseError, not *errors.Error

According to revel documentation, https://revel.imtqy.com/manual/parameters.html

The SQL standard date time formats of 2006-01-02, 2006-01-02 15:04 are built in.

In the same document, they also say that you can add formats like this:

func init() {
    revel.TimeFormats = append(revel.TimeFormats, "01/02/2006")
}

To verify that the format was in an array, I tried this:

func init() {
    revel.TimeFormats = append(revel.TimeFormats, "2016-06-01 12:12")
}

In my last despair, I tried to present it in the same format as revel would return json once in:

{
  "startedAt": "2017-06-22T12:22:16.265618819-05:00",
  ...
}

At this moment, I'm not sure where to go with this. Could anyone enjoy using Datetime analysis?


Update: I tried the RickyA solution below, but now there is a parsing error.

parsing time ""Mon, 02 Jan 2006 15:04:05 -0700"" as "Mon, 02 Jan 2006 15:04:05 -0700": cannot parse ""Mon, 02 Jan 2006 15:04:05 -0700"" as "Mon"

, , interrum. ToX, . , UnmarshalJSON, .

, , :

func (t *AnyTime) UnmarshalJSON(b []byte) error {
  fmt.Println(time.RFC1123Z)
  fmt.Println(string(b))
  fmt.Println(reflect.TypeOf(time.RFC1123Z))
  fmt.Println(reflect.TypeOf(string(b)))
  ...

:

Mon, 02 Jan 2006 15:04:05 -0700
"Mon, 02 Jan 2006 15:04:05 -0700"
string
string

, . , , , UnmarshalJSON, .


. , , . UnmarshalJSON , ":" ",", . , , . , :

func (t *AnyTime) UnmarshalJSON(b []byte) error {
  var err error
  sTime := string(b)
  sTime = strings.TrimSuffix(sTime, "\"")
  sTime = strings.TrimPrefix(sTime, "\"")
  t.Time, err = time.Parse(time.RFC1123Z, sTime)
  ...
+1
1

, time.Time

package genericfields

import (
    "encoding/json"
    "time"
    "strings"

    "gopkg.in/mgo.v2/bson"
)

// ANYTIME //

// AnyTime accepts any time format for its unmarshaling //
type AnyTime struct{ time.Time }

func (t AnyTime) MarshalJSON() ([]byte, error) {
    return json.Marshal(t.Time)
}

func (t *AnyTime) UnmarshalJSON(b []byte) error {
    err := json.Unmarshal(b, &t.Time)
    if err != nil { //assume non tz time input
        bstr := strings.Trim(string(b), `"`)
        t.Time, err = time.Parse("2006-01-02T15:04:05", bstr)
        if err != nil {
            return err //TODO add more formats to try
        }
    }
    return nil
}

func (t AnyTime) GetBSON() (interface{}, error) {
    return t.Time, nil
}

func (t *AnyTime) SetBSON(raw bson.Raw) error {
    var tm time.Time
    err := raw.Unmarshal(&tm)
    if err != nil {
        return err
    }
    t.Time = tm.UTC()
    return nil
}

func (t AnyTime) ToTime() time.Time {
    return t.Time
}

:

type MyStruct struct {
  StartedAt genericfields.AnyTime `json:"startedAt" bson:"startedAt"`
  ...
}

, time.Parse.

+1

Source: https://habr.com/ru/post/1682218/


All Articles