1

I am trying to parse a timestamp that is generated in python and posted over to here to be stored in cassandra and I am getting a weird error.

parsing time ""2015-11-05 14:14:32-0700"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 14:14:32-0700"" as "T"

I don't want to modify the time given to me from the python in anyway, just want to pass it right into cassandra but the driver says I need to have it be time.Time (not string) to do so.

Here is the code

type OurCustomType struct {
    TimeStamp   time.Time `json:"timestamp"`
}


func extractJsonBody(r *http.Request) []byte {
    body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
    if err != nil {
        //...
    }
    if err := r.Body.Close(); err != nil {
         //....
    }
    return body
}

func View(w http.ResponseWriter, r *http.Request) {
    var variable OurCustomType

    body := extractJsonBody(r)

    if err := json.Unmarshal(body, &variable); err != nil {
        //..
    } else {
        //...
    }
}

Edit: I have implemented my own time.Time type and implemented a custom UnmarshalJSON to attempt to fix this issue.

type PythonTime struct {
    time.Time
}

func (self *PythonTime) UnmarshalJSON(b []byte) (err error) {
    self.Time, err = time.Parse("2006-01-02 15:04:05-0700", string(b))
    return
}

But I am not getting this error:

parsing time ""2015-11-05 14:14:32-0700"" as "2006-01-02 15:04:05-0700": cannot parse ""2015-11-05 14:14:32-0700"" as "2006"
Jared Mackey
  • 3,998
  • 4
  • 31
  • 50
  • 1
    The time class requires a format string, in your case it would be `2006-01-02 15:04:05-0700`. I believe you can't supply that to the unmashaller directly so you have to implement `UnmarshalJSON` for your type to deal with it there or unmarshal into a string and convert it later. More on the format string stuff here; http://stackoverflow.com/questions/25845172/parsing-date-string-in-golang and here's an example implement MarshalJSON, what you'll need to do is similar, just for unmarshalling http://stackoverflow.com/questions/20475321/override-the-layout-used-by-json-marshal-to-format-time-time – evanmcdonnal Nov 18 '15 at 22:14
  • @evanmcdonnal Thanks for the comment, this has gotten me really close but I am still getting an error I am unsure about. See most recent edit – Jared Mackey Nov 18 '15 at 22:23
  • I'm not sure how to help you on that one. I did a quick test here https://play.golang.org/p/fZ7soeHJhj and it should work. I generally suggest printing/inspecting the value of `string(b)` here cause the formats aren't matching for some reason however the error makes it seem like `string(b)` is producing the expected value. – evanmcdonnal Nov 18 '15 at 22:34
  • Ok I got it working, but I needed to modify the library I was using to do so. I don't think that is right and should be able to make them think my struct IS a `time.Time` type. Is that possible? – Jared Mackey Nov 18 '15 at 23:10
  • If you define `UnmarshalJSON` for the other type ( `OurCustomType` ) then you wouldn't need that `PythonTime` wrapper you could also declare it like `type PythonTime time.Time` so you're type is an alias for `time.Time` rather than a wrapper around it. – evanmcdonnal Nov 18 '15 at 23:52
  • 2
    The last error you are getting is due to the presence of the `"` marks in the beggining and in the end of byte array that represents your time. Since you are reading directly from the json structure you need to trim the `"` to have a valid time. – hbejgel Nov 19 '15 at 13:08

1 Answers1

0

The problem here is that you've got quotations in the date being passed from Python.

In the first error that you posted, you can see that it failed at reading "T". That's because you were using a different format in Go than you were returning from Python.

parsing time ""2015-11-05 14:14:32-0700"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 14:14:32-0700"" as "T"

You used ""2006-01-02T15:04:05Z07:00"" when the format from Python is ""2006-01-02 15:04:05-0700"".

The same is true in your last error, except that you dropped the one pair of the quotations in the date format verses what you have returned in Python.

parsing time ""2015-11-05 14:14:32-0700"" as "2006-01-02 15:04:05-0700": cannot parse ""2015-11-05 14:14:32-0700"" as "2006"

This is saying that you provided "2006-01-02 15:04:05-0700" but it's expecting ""2006-01-02 15:04:05-0700"" due to the extra quotes in the date returned by Python.

Sly
  • 1,145
  • 7
  • 19