1

What would be a good way of storing date/time data with various precisions in a single field or attribute. For example, if I had a Movie class with a release_date property, and sometimes it would be a specific date like 2012-12-24, and other times it may just be 1948, with no month or day. Ruby doesn't allow for zero-value date parts.

My only good idea so far has been to store the data polymorphicly with another field storing the time as either Number or Time, and the date value would always be stored as an integer, but even that seems really ugly. Other than that all I can think of is storing each data part separately, but that sounds like it would make sorting a nightmare.

Perhaps there's a common way of doing this that I'm unaware of.

4

1 回答 1

0

使用部分日期gem。

一个简单的日期类,可用于将部分日期值存储在单个列/属性中。示例用例将包括完整日期未知的档案或目录条目。年份是可选的,可以是负值。月份和日期是可选的,但月份必须在日期之前设置

PartialDate::Date 使用 30 位寄存器作为日期实例的后备存储,并使用位摆弄来获取或设置年、月和日值。

于 2012-07-14T23:20:10.740 回答