Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: module Redmine Chris@1296: module Utils Chris@1296: class << self Chris@1296: # Returns the relative root url of the application Chris@1296: def relative_url_root Chris@1296: ActionController::Base.respond_to?('relative_url_root') ? Chris@1296: ActionController::Base.relative_url_root.to_s : Chris@1296: ActionController::Base.config.relative_url_root.to_s Chris@1296: end Chris@1296: Chris@1296: # Sets the relative root url of the application Chris@1296: def relative_url_root=(arg) Chris@1296: if ActionController::Base.respond_to?('relative_url_root=') Chris@1296: ActionController::Base.relative_url_root=arg Chris@1296: else Chris@1296: ActionController::Base.config.relative_url_root = arg Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: # Generates a n bytes random hex string Chris@1296: # Example: Chris@1296: # random_hex(4) # => "89b8c729" Chris@1296: def random_hex(n) Chris@1296: SecureRandom.hex(n) Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: module Shell Chris@1296: def shell_quote(str) Chris@1296: if Redmine::Platform.mswin? Chris@1296: '"' + str.gsub(/"/, '\\"') + '"' Chris@1296: else Chris@1296: "'" + str.gsub(/'/, "'\"'\"'") + "'" Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: module DateCalculation Chris@1296: # Returns the number of working days between from and to Chris@1296: def working_days(from, to) Chris@1296: days = (to - from).to_i Chris@1296: if days > 0 Chris@1296: weeks = days / 7 Chris@1296: result = weeks * (7 - non_working_week_days.size) Chris@1296: days_left = days - weeks * 7 Chris@1296: start_cwday = from.cwday Chris@1296: days_left.times do |i| Chris@1296: unless non_working_week_days.include?(((start_cwday + i - 1) % 7) + 1) Chris@1296: result += 1 Chris@1296: end Chris@1296: end Chris@1296: result Chris@1296: else Chris@1296: 0 Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: # Adds working days to the given date Chris@1296: def add_working_days(date, working_days) Chris@1296: if working_days > 0 Chris@1296: weeks = working_days / (7 - non_working_week_days.size) Chris@1296: result = weeks * 7 Chris@1296: days_left = working_days - weeks * (7 - non_working_week_days.size) Chris@1296: cwday = date.cwday Chris@1296: while days_left > 0 Chris@1296: cwday += 1 Chris@1296: unless non_working_week_days.include?(((cwday - 1) % 7) + 1) Chris@1296: days_left -= 1 Chris@1296: end Chris@1296: result += 1 Chris@1296: end Chris@1296: next_working_date(date + result) Chris@1296: else Chris@1296: date Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: # Returns the date of the first day on or after the given date that is a working day Chris@1296: def next_working_date(date) Chris@1296: cwday = date.cwday Chris@1296: days = 0 Chris@1296: while non_working_week_days.include?(((cwday + days - 1) % 7) + 1) Chris@1296: days += 1 Chris@1296: end Chris@1296: date + days Chris@1296: end Chris@1296: Chris@1296: # Returns the index of non working week days (1=monday, 7=sunday) Chris@1296: def non_working_week_days Chris@1296: @non_working_week_days ||= begin Chris@1296: days = Setting.non_working_week_days Chris@1296: if days.is_a?(Array) && days.size < 7 Chris@1296: days.map(&:to_i) Chris@1296: else Chris@1296: [] Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end