基本上我有一个应用程序,用户可以在其中选择从现在到 2 年后的任何一天的时间段。我正在创建一个每天运行的 rake 任务,以便从现在起 2 年内向我的数据库中添加一条记录,这样明天,仍然有 2 年的时间段选择。
以我目前的逻辑,我很好奇闰年会发生什么,有没有办法让它更健壮地正确处理闰年?我担心我要么以完全错过的一天结束,要么以重复的一天结束。
task :generate_timeslots => :environment do
startDate = Time.now.to_date + 2.years
endDate = Time.now.to_date + 2.years
startDate.upto(endDate).each do |d|
5.times do
timeslot = Timeslot.create
timeslot.location_id = 1
timeslot.timeslot = "#{d} 09:00:00"
timeslot.save
timeslot = Timeslot.create
timeslot.location_id = 1
timeslot.timeslot = "#{d} 11:00:00"
timeslot.save
timeslot = Timeslot.create
timeslot.location_id = 1
timeslot.timeslot = "#{d} 13:00:00"
timeslot.save
timeslot = Timeslot.create
timeslot.location_id = 1
timeslot.timeslot = "#{d} 15:00:00"
timeslot.save
end
end
end