Module: BankingDateTools

Defined in:
lib/banking_date_tools.rb,
lib/banking_date_tools/version.rb,
lib/banking_date_tools/banking_date_tools.rb

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
# File 'lib/banking_date_tools.rb', line 9

def self.included(base)
  base.extend(BankingDateTools)
end

.rootObject



5
6
7
# File 'lib/banking_date_tools.rb', line 5

def self.root
  File.expand_path(File.dirname(File.dirname(__FILE__)))
end

Instance Method Details

#banking_day?(date = Date.today, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/banking_date_tools/banking_date_tools.rb', line 4

def banking_day?(date=Date.today, options={})
  date = Date.today if date.nil?
  # Set defaults
  options = {
    :holidays => [],
    :force_allow_days => []
  }.merge!(options)
      
  unless options[:force_allow_days].include?(date)
    return false if options[:no_holidays] && options[:holidays].include?(date)
    if options[:no_weekends] 
      return false if (date.saturday? || date.sunday?)
    else
      return false if options[:no_saturdays] && date.saturday?
      return false if options[:no_sundays] && date.sunday?
    end
  end
  true
end

#get_banking_day(date, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/banking_date_tools/banking_date_tools.rb', line 24

def get_banking_day(date, options={})
  options = ({:padding => 0, :previous => false}.merge(options))
  adjusted_padding = (options[:padding].abs + 1)
  increment = (options[:previous] ? -1 : 1)
  adjusted_padding.times do |p|
    date = (date + increment ) if p != 0 
    while !banking_day?(date, options)
      date = (date + increment )    
    end
  end
  date
end

#get_next_banking_day(date, options = {}) ⇒ Object



37
38
39
40
# File 'lib/banking_date_tools/banking_date_tools.rb', line 37

def get_next_banking_day(date, options={})
  options = ({:padding => 1, :previous => false}.merge(options))
  get_banking_day(date, options)
end

#get_previous_banking_day(date, options = {}) ⇒ Object



42
43
44
45
# File 'lib/banking_date_tools/banking_date_tools.rb', line 42

def get_previous_banking_day(date, options={})
  options = ({:padding => 1, :previous => true}.merge(options))
  get_banking_day(date, options)
end

#safe_date(date) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/banking_date_tools/banking_date_tools.rb', line 47

def safe_date(date)
  if date.blank?
    return Date.today
  end
#    if date.class == String
#      if date.include?("/") || date.include?("-")
#        split = date.split("/") if date.include?("/")
#        split = date.split("-") if date.include?("-")
#        time = ""
#        if split[2] && split[2].include?(":")
#          year_time = split[2].split(" ")
#          split[2] = year_time[0]
#          time = year_time[1]
#        end
#        safe = "#{split[2]}-#{split[0]}-#{split[1]}"
#        safe << " #{time}" unless time.blank?
#        return safe if split.length == 3 && split[2].length == 4
#      end
#    end
  date.to_date
end

#tick(date = nil) ⇒ Object

creates a date integer formated like a MS.Net date tick



70
71
72
73
74
75
76
77
# File 'lib/banking_date_tools/banking_date_tools.rb', line 70

def tick(date=nil)
  if date.blank?
    date = Time.now
  else
    date = date.to_time
  end
  date.tick
end