简体   繁体   English

蒙古语中的日期查询

[英]Date Querying in Mongoid

This has been asked a lot, but I'm still facing some issues with it. 这已经被问了很多,但是我仍然面临一些问题。

I have date I receive and was storing as Date. 我有收到的日期,并存储为日期。 I need to query on Greater Than and Less than, so I changed it to Time, tried it again, but I'm getting weird results. 我需要查询“大于”和“小于”,因此将其更改为“时间”,然后再次尝试,但结果却很奇怪。

I'm doing this 我在做这个

Class.where(:event_date.gt => Time.parse(Date.today))

and I'm getting old records, 1940s, 1960s and others. 我得到了1940年代,1960年代等的旧唱片。 I tried converting time by adding .utc at the end, comparing against Date.today only, but nothing sovled the problem so far. 我尝试通过在末尾添加.utc来转换时间,仅与Date.today进行比较,但是到目前为止,没有任何问题可以解决这个问题。 This is the selector being generated by Mongoid 这是由Mongoid生成的选择器

selector: {:date_utc=>{"$gte"=>Sat Sep 10 21:00:00 UTC 2011}},

I receive the date in this format "2011-09-11" and store it in the Time field. 我收到的日期格式为“ 2011-09-11”,并将其存储在“时间”字段中。 Tried parsing that as Time utc as well, no luck. 尝试将其解析为Time utc,也没有运气。

Any idea? 任何的想法? I'm using Mongoid 2.0.2. 我正在使用Mongoid 2.0.2。 Later versions seem incompatible with other extensions I'm using. 更高版本似乎与我使用的其他扩展名不兼容。

[UPDATE] [更新]

So the problem is with the old dates before 1970 apparently. 因此,问题显然出在1970年以前的旧日期上。 How do I deal with them is the question now. 我现在该如何处理它们。

This is a known bug in Mongo. 这是Mongo中的一个已知错误。 See ISSUE 405 问题405

The reason is that Mongo uses an unsigned number to store dates, so anything before the epoch rolls over far into the future. 原因是Mongo使用无符号数字来存储日期,因此在纪元到未来为止的任何事情都是如此。

Affortunately this issue has been fixed for stable version 2.0 released today. 遗憾的是,对于今天发布的稳定版本2.0,此问题已得到修复。 Upgrading to this version should solve your problem. 升级到此版本应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM