douxin20081125 2017-06-22 17:24 采纳率: 0%
浏览 34
已采纳

在Revel中解析JSON日期时间

I'm attempting to parse a Datetime in revel from a json request. The request looks something like this:

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

The struct it's being decoded into looks like this:

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

The decode line looks like this:

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

Revel returns this error:

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

According to revel's documentation here, https://revel.github.io/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 you can append formats like this:

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

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

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

In a final act of desperation I tried submitting it in the same format that revel will return json times in:

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

At this point I'm not sure where to go with this. Has anyone been able to get revel to parse a Datetime?


Update: I tried RickyA's 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"

What's even stranger is that I implemented a bit of a hack to get this working in the interrum. I changed the request time field to a string, and gave it a ToX function which converted it. That function works, but when it's moved into the UnmarshalJSON function it fails.

Also, I can't tell if this is a bug or not:

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

This outputs this:

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

Notice that only 1 of the string has double quotes. This is leading me to believe that some how the byte array passed to UnmarshalJSON has double quotes within its string.


Final Update: So I'm pretty sure the double quotes are intentional, and in a way it makes sense. The UnmarshalJSON is passing everything from the ':' through the ',' which includes the double quotes. I'm guessing this is done so that it can also support integers and booleans. I don't like the solution, but this is how I fixed it:

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条回答 默认 最新

  • dtxa49711 2017-06-22 18:33
    关注

    I had the same problem and ended up creating a custom type for 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
    }
    

    Usage:

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

    You might need to tweak the input for time.Parse somewhat.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程