【发布时间】:2010-05-12 18:47:55
【问题描述】:
哈哈..
我正在使用 Chronic 来解析用户在日历中添加的时间。在代码工作并实现正确时间的地方,最终结果是,如果用户添加时间,则它没有日期,并且因为它没有日期,所以它不会显示在结果中。有什么想法吗?
def set_dates
unless self.natural_date.blank? || Chronic.parse(self.natural_date).blank?
# check if we are dealing with a date or a date + time
if time_provided?(self.natural_date)
self.date = nil
self.time = Chronic.parse(self.natural_date)
else
self.date = Chronic.parse(self.natural_date).to_date
self.time = nil
end
end
unless self.natural_end_date.blank? || Chronic.parse(self.natural_end_date).blank?
# check if we are dealing with a date or a date + time
if time_provided?(self.natural_end_date)
self.end_date = nil
self.end_time = Chronic.parse(self.natural_end_date)
else
self.end_date = Chronic.parse(self.natural_end_date).to_date
self.end_time = nil
end
end
end
编辑:
这里是time_provided?方法:
def time_provided?(natural_date_string)
date_span = Chronic.parse(natural_date_string, :guess => false)
(date_span.last - date_span.first).to_i == 1
end
【问题讨论】:
-
我必须承认,当我读到这个标题时,它让我笑了。
-
哈!我也是,我还以为是巨魔
-
+1 起初我是 wtf。然后我笑了。然后我哭了。然后我就兴奋起来了。
-
当这一切都说完了,这可能是关于 SO 提出的最好的问题。
-
@Alan 我正要回答这个问题,但后来我兴奋了..
标签: ruby-on-rails chronic