annotate .svn/pristine/8f/8fa37bd50f73aaaf794ed1e7474b6ffe3abf2008.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 module Redmine
Chris@1296 19 module Utils
Chris@1296 20 class << self
Chris@1296 21 # Returns the relative root url of the application
Chris@1296 22 def relative_url_root
Chris@1296 23 ActionController::Base.respond_to?('relative_url_root') ?
Chris@1296 24 ActionController::Base.relative_url_root.to_s :
Chris@1296 25 ActionController::Base.config.relative_url_root.to_s
Chris@1296 26 end
Chris@1296 27
Chris@1296 28 # Sets the relative root url of the application
Chris@1296 29 def relative_url_root=(arg)
Chris@1296 30 if ActionController::Base.respond_to?('relative_url_root=')
Chris@1296 31 ActionController::Base.relative_url_root=arg
Chris@1296 32 else
Chris@1296 33 ActionController::Base.config.relative_url_root = arg
Chris@1296 34 end
Chris@1296 35 end
Chris@1296 36
Chris@1296 37 # Generates a n bytes random hex string
Chris@1296 38 # Example:
Chris@1296 39 # random_hex(4) # => "89b8c729"
Chris@1296 40 def random_hex(n)
Chris@1296 41 SecureRandom.hex(n)
Chris@1296 42 end
Chris@1296 43 end
Chris@1296 44
Chris@1296 45 module Shell
Chris@1296 46 def shell_quote(str)
Chris@1296 47 if Redmine::Platform.mswin?
Chris@1296 48 '"' + str.gsub(/"/, '\\"') + '"'
Chris@1296 49 else
Chris@1296 50 "'" + str.gsub(/'/, "'\"'\"'") + "'"
Chris@1296 51 end
Chris@1296 52 end
Chris@1296 53 end
Chris@1296 54
Chris@1296 55 module DateCalculation
Chris@1296 56 # Returns the number of working days between from and to
Chris@1296 57 def working_days(from, to)
Chris@1296 58 days = (to - from).to_i
Chris@1296 59 if days > 0
Chris@1296 60 weeks = days / 7
Chris@1296 61 result = weeks * (7 - non_working_week_days.size)
Chris@1296 62 days_left = days - weeks * 7
Chris@1296 63 start_cwday = from.cwday
Chris@1296 64 days_left.times do |i|
Chris@1296 65 unless non_working_week_days.include?(((start_cwday + i - 1) % 7) + 1)
Chris@1296 66 result += 1
Chris@1296 67 end
Chris@1296 68 end
Chris@1296 69 result
Chris@1296 70 else
Chris@1296 71 0
Chris@1296 72 end
Chris@1296 73 end
Chris@1296 74
Chris@1296 75 # Adds working days to the given date
Chris@1296 76 def add_working_days(date, working_days)
Chris@1296 77 if working_days > 0
Chris@1296 78 weeks = working_days / (7 - non_working_week_days.size)
Chris@1296 79 result = weeks * 7
Chris@1296 80 days_left = working_days - weeks * (7 - non_working_week_days.size)
Chris@1296 81 cwday = date.cwday
Chris@1296 82 while days_left > 0
Chris@1296 83 cwday += 1
Chris@1296 84 unless non_working_week_days.include?(((cwday - 1) % 7) + 1)
Chris@1296 85 days_left -= 1
Chris@1296 86 end
Chris@1296 87 result += 1
Chris@1296 88 end
Chris@1296 89 next_working_date(date + result)
Chris@1296 90 else
Chris@1296 91 date
Chris@1296 92 end
Chris@1296 93 end
Chris@1296 94
Chris@1296 95 # Returns the date of the first day on or after the given date that is a working day
Chris@1296 96 def next_working_date(date)
Chris@1296 97 cwday = date.cwday
Chris@1296 98 days = 0
Chris@1296 99 while non_working_week_days.include?(((cwday + days - 1) % 7) + 1)
Chris@1296 100 days += 1
Chris@1296 101 end
Chris@1296 102 date + days
Chris@1296 103 end
Chris@1296 104
Chris@1296 105 # Returns the index of non working week days (1=monday, 7=sunday)
Chris@1296 106 def non_working_week_days
Chris@1296 107 @non_working_week_days ||= begin
Chris@1296 108 days = Setting.non_working_week_days
Chris@1296 109 if days.is_a?(Array) && days.size < 7
Chris@1296 110 days.map(&:to_i)
Chris@1296 111 else
Chris@1296 112 []
Chris@1296 113 end
Chris@1296 114 end
Chris@1296 115 end
Chris@1296 116 end
Chris@1296 117 end
Chris@1296 118 end