ruby-on-rails – Rails cron作业每天运行2年后创建数据库记录.
发布时间:2020-12-17 02:20:46 所属栏目:百科 来源:网络整理
导读:基本上我有一个应用程序,用户可以从现在到2年后的任何一天选择一个时间段.我正在创建一个rake任务,每天运行一次,从现在开始向我的数据库添加一条记录,所以明天仍然会有2年的时间段选择. 根据我目前的逻辑,我很好奇当闰年会发生什么,并且有没有办法让这个更加
基本上我有一个应用程序,用户可以从现在到2年后的任何一天选择一个时间段.我正在创建一个rake任务,每天运行一次,从现在开始向我的数据库添加一条记录,所以明天仍然会有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 解决方法
答案是,如果“从现在起两年后”包括跨越闰年的第29天,那么“备用”日将包含在该备份中以计入日历中2月29日的“额外”日.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |