如何将json解析时间与ruby对象时间进行比较?(How to compare json parsed time with ruby object time?)

编程入门 行业动态 更新时间:2024-10-19 22:42:18
如何将json解析时间与ruby对象时间进行比较?(How to compare json parsed time with ruby object time?)

我有一些控制器测试,我想检查响应值。 我无法将JSON.parse(...["start_time"])与foo.start_time与以下堆栈跟踪进行比较:

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

我尝试了一堆解析技术,但它们要么返回不同的格式,要么不返回准确的值。 有没有一种简单的方法可以将这些值用于相同的格式以比较相等性?

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?

最满意答案

您正在尝试比较两种不同的数据类型。 解析的JSON是一个字符串。 foo.start_time属性是Ruby日期时间。

请执行下列操作:

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

这会将字符串转换为日期时间,然后可以将其与foo.start_time进行比较。

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.

更多推荐

本文发布于:2023-08-01 16:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   如何将   对象   time   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!