This Guy


5,220 Experience
52 Lessons Completed
0 Questions Solved


Posted in Redmine Rails

Here is a version you can play with in the rails console until you get it to do what you want (which I cant quite figure out...).

require 'date'

def get_date_for_redmine( start_hour, end_hour, limit, now )

  puts "Running for #{start_hour}, #{end_hour}, #{limit}, #{now.to_fs(:db)}."

  hour = now.hour
  minute = now.minute

  if hour >= start_hour
    temp_hour = hour + limit
    if temp_hour < end_hour
      puts "Return 1"
      return now.change(hour: temp_hour).to_fs(:db)
    elsif temp_hour == end_hour
      if minute > 0  then
        puts "Return 2"
        return now.change(hour: start_hour, day: 1).to_fs(:db)
        puts "Return 3"
        return now.change(hour: temp_hour).to_fs(:db)
    elsif temp_hour > end_hour
      if  hour > end_hour
        puts "Return 4"
        return get_date_for_redmine(start_hour,end_hour, limit, now.change(hour: 8, day: 1) )
        remains = temp_hour - end_hour
        puts "Return 5"
        return get_date_for_redmine(start_hour,end_hour, remains, now.change(hour: 8, day: 1) )
    puts "Nothing Happened"

puts get_date_for_redmine(8, 16, 4,

Posted in Redmine Rails

hour is never greater than startDay because a Date object converted to a DateTime always has hours / mins / seconds of 0, and startDay is 8. Although I suspect you wanted that to be start_hour ? Anyway that means the code path is always through the last else which returns nil. BTW you have a few syntax errors in here (shown in the highlighting above, such as 'elseif' which should be 'elsif').