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)
...