Module: Redmine::Utils::DateCalculation

Included in:
Issue, Helpers::Gantt
Defined in:
lib/redmine/utils.rb

Overview

Since:

  • 2.2.0

Instance Method Summary collapse

Instance Method Details

#add_working_days(date, working_days) ⇒ Object

Adds working days to the given date



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/redmine/utils.rb', line 108

def add_working_days(date, working_days)
  if working_days > 0
    weeks = working_days / (7 - non_working_week_days.size)
    result = weeks * 7
    days_left = working_days - weeks * (7 - non_working_week_days.size)
    cwday = date.cwday
    while days_left > 0
      cwday += 1
      unless non_working_week_days.include?(((cwday - 1) % 7) + 1)
        days_left -= 1
      end
      result += 1
    end
    next_working_date(date + result)
  else
    date
  end
end

#next_working_date(date) ⇒ Object

Returns the date of the first day on or after the given date that is a working day



128
129
130
131
132
133
134
135
# File 'lib/redmine/utils.rb', line 128

def next_working_date(date)
  cwday = date.cwday
  days = 0
  while non_working_week_days.include?(((cwday + days - 1) % 7) + 1)
    days += 1
  end
  date + days
end

#non_working_week_daysObject

Returns the index of non working week days (1=monday, 7=sunday)



138
139
140
141
142
143
144
145
146
147
# File 'lib/redmine/utils.rb', line 138

def non_working_week_days
  @non_working_week_days ||= begin
    days = Setting.non_working_week_days
    if days.is_a?(Array) && days.size < 7
      days.map(&:to_i)
    else
      []
    end
  end
end

#working_days(from, to) ⇒ Object

Returns the number of working days between from and to



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/redmine/utils.rb', line 89

def working_days(from, to)
  days = (to - from).to_i
  if days > 0
    weeks = days / 7
    result = weeks * (7 - non_working_week_days.size)
    days_left = days - weeks * 7
    start_cwday = from.cwday
    days_left.times do |i|
      unless non_working_week_days.include?(((start_cwday + i - 1) % 7) + 1)
        result += 1
      end
    end
    result
  else
    0
  end
end