Joe Essey Joe Essey - 2 months ago 10
Ruby Question

How to compare json parsed time with ruby object time?

I have some controller tests and I want to check the response values. I am unable to compare

JSON.parse(...["start_time"])
with
foo.start_time
with the following stack trace:

expected: 2016-09-17 10:50:00.000000000 +0000
got: "2016-09-17T10:50:00.000Z"


I tried a bunch of parsing techniques but they either return different formats, or do not return accurate values. Is there a simple way to get these values in to the same format to compare equality?

Answer

You're trying to compare two different data types. The parsed JSON is a string. The foo.start_time attribute is a Ruby datetime.

Do the following:

(JSON.parse(...["start_time"])).to_datetime

That will convert the string into a datetime, which can then be compared to foo.start_time.