gpt4 book ai didi

ruby-on-rails - 使用 Chronic 会损害您的时间感吗?

转载 作者:行者123 更新时间:2023-12-03 10:05:25 24 4
gpt4 key购买 nike

哈哈..

我正在使用 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

最佳答案

首先,我不太确定你在问什么,因为看起来代码是故意按照你描述的那样做的......当有时间提供时,日期字段被分配 nil .而且我认为这不是慢性病的罪魁祸首,因为这就是您的代码的工作方式。

不知道你的设计(为什么有单独的日期和时间字段)、字段的类型等,我建议从这样的小杂文开始:

if time_provided?(self.natural_date)
self.time = Chronic.parse(self.natural_date)
self.date = self.time.to_date

或者:
self.end_date = Chronic.parse(self.natural_date).to_date
if time_provided?(self.natural_date)
self.time = Chronic.parse(self.natural_date)
end

或者问题可能出在您提供的代码之外:在负责“因为没有日期,所以不会在结果中显示”行为的部分?也许你应该让条件更灵活?

关于ruby-on-rails - 使用 Chronic 会损害您的时间感吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2821813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com