banking_date_tools 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.autotest +10 -0
- data/.gitignore +6 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/README +0 -0
- data/Rakefile +2 -0
- data/date_tools.gemspec +23 -0
- data/lib/banking_date_tools/banking_date_tools.rb +125 -0
- data/lib/banking_date_tools/version.rb +3 -0
- data/lib/banking_date_tools.rb +9 -0
- data/spec/lib/banking_date_tools_spec.rb +83 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +10 -0
- metadata +83 -0
data/.autotest
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
data/date_tools.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "banking_date_tools/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "banking_date_tools"
|
7
|
+
s.version = BankingDateTools::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Joshua T Mckinney"]
|
10
|
+
s.email = ["joshmckin@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Banking Date Tools}
|
13
|
+
s.description = %q{Banking Date Tools}
|
14
|
+
|
15
|
+
s.add_development_dependency 'rspec'
|
16
|
+
s.add_development_dependency 'mocha'
|
17
|
+
s.rubyforge_project = "banking_date_tools"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'date'
|
2
|
+
module BankingDateTools
|
3
|
+
def banking_day?(date=Date.today, options={})
|
4
|
+
date = Date.today if date.nil?
|
5
|
+
# Set defaults
|
6
|
+
options = {
|
7
|
+
:holidays => [],
|
8
|
+
:force_allow_days => []
|
9
|
+
}.merge!(options)
|
10
|
+
|
11
|
+
unless options[:force_allow_days].include?(date)
|
12
|
+
return false if options[:no_holidays] && options[:holidays].include?(date)
|
13
|
+
return false if options[:no_weekends] && self.weekend?(date)
|
14
|
+
return false if options[:no_saturdays] && self.saturday?(date)
|
15
|
+
return false if options[:no_sundays] && self.sunday?(date)
|
16
|
+
end
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def get_banking_day(date, options={})
|
22
|
+
options[:padding] ||= 0
|
23
|
+
adjusted_padding = (options[:padding].abs + 1)
|
24
|
+
increment = (options[:previous] ? -1 : 1)
|
25
|
+
adjusted_padding.times do |p|
|
26
|
+
date = (date + increment) unless ((p + 1) == adjusted_padding)
|
27
|
+
while !banking_day?(date, options)
|
28
|
+
date = (date + increment )
|
29
|
+
end
|
30
|
+
end
|
31
|
+
date
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_next_banking_day(date, options={})
|
35
|
+
options[:previous] = false
|
36
|
+
get_banking_day(date, options)
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_previous_banking_day(date, options={})
|
40
|
+
options[:previous] = true
|
41
|
+
get_banking_day(date, options)
|
42
|
+
end
|
43
|
+
|
44
|
+
def weekend?(date)
|
45
|
+
date.wday == 0 || date.wday == 6
|
46
|
+
end
|
47
|
+
|
48
|
+
def sunday?(date)
|
49
|
+
date.wday == 0
|
50
|
+
end
|
51
|
+
|
52
|
+
def saturday?(date)
|
53
|
+
date.wday == 6
|
54
|
+
end
|
55
|
+
|
56
|
+
def safe_date(date)
|
57
|
+
if date.blank?
|
58
|
+
return Date.today
|
59
|
+
end
|
60
|
+
if date.class == String
|
61
|
+
if date.include?("/") || date.include?("-")
|
62
|
+
split = date.split("/") if date.include?("/")
|
63
|
+
split = date.split("-") if date.include?("-")
|
64
|
+
time = ""
|
65
|
+
if split[2].include?(":")
|
66
|
+
year_time = split[2].split(" ")
|
67
|
+
split[2] = year_time[0]
|
68
|
+
time = year_time[1]
|
69
|
+
end
|
70
|
+
safe = "#{split[2]}-#{split[0]}-#{split[1]}"
|
71
|
+
safe << " #{time}" unless time.blank?
|
72
|
+
return safe if split.length == 3 && split[2].length == 4
|
73
|
+
end
|
74
|
+
end
|
75
|
+
date
|
76
|
+
end
|
77
|
+
|
78
|
+
# creates a date integer formated like a MS.Net date tick
|
79
|
+
def tick(date=nil)
|
80
|
+
if date.blank?
|
81
|
+
date = Time.now
|
82
|
+
else
|
83
|
+
date = safe_date(date).to_time
|
84
|
+
end
|
85
|
+
((date.to_f) *1000).to_i
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
class Usage
|
90
|
+
include BankingDateTools
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.banking_day?(date, holidays={}, options={})
|
94
|
+
Usage.banking_day?(date, holidays, options)
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.get_next_banking_day(date, holidays={},options={})
|
98
|
+
Usage.get_next_banking_day(date, holidays,options)
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.get_previous_banking_day(date, holidays={},options={})
|
102
|
+
Usage.get_previous_banking_day(date, holidays,options)
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.weekend?(date)
|
106
|
+
Usage.weekend?(date)
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.sunday?(date)
|
110
|
+
Usage.sunday?(date)
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.saturday?(date)
|
114
|
+
Usage.saturday?(date)
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.safe_date(date)
|
118
|
+
Usage.safe_date(date)
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.tick(date=nil)
|
122
|
+
Usage.tick(date)
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
include BankingDateTools
|
3
|
+
describe BankingDateTools, "get_banking_day" do
|
4
|
+
|
5
|
+
context "padding == 0" do
|
6
|
+
|
7
|
+
context "no_weekends == true" do
|
8
|
+
it "should role date to following banking day if on weekend and no_weekends are true" do
|
9
|
+
options = {:padding => 0, :no_weekends => true}
|
10
|
+
get_banking_day(Date.parse("2012-03-31"),options).should eql(Date.parse("2012-04-02"))
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should role date to following banking day if on weekend and no_weekends are true" do
|
14
|
+
options = {:padding => 0, :no_weekends => true, :no_holidays => true, :holidays => [Date.parse("2012-04-02")]}
|
15
|
+
get_banking_day(Date.parse("2012-03-31"),options).should eql(Date.parse("2012-04-03"))
|
16
|
+
end
|
17
|
+
|
18
|
+
context "previous == true" do
|
19
|
+
it "should role date to previous banking day if on weekend and no_weekends are true" do
|
20
|
+
options = {:previous => true, :padding => 0, :no_weekends => true}
|
21
|
+
get_previous_banking_day(Date.parse("2012-03-31"),options).should eql(Date.parse("2012-03-30"))
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should role date to previous banking day if on weekend and no_weekends are true" do
|
25
|
+
options = {:previous => true,:padding => 0, :no_weekends => true, :no_holidays => true, :holidays => [Date.parse("2012-03-30")]}
|
26
|
+
get_previous_banking_day(Date.parse("2012-03-31"),options).should eql(Date.parse("2012-03-29"))
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should role date to previous banking day if padding = 1" do
|
30
|
+
options = {:previous => true,:padding => 1}
|
31
|
+
get_previous_banking_day(Date.parse("2012-03-27"),options).should eql(Date.parse("2012-03-26"))
|
32
|
+
end
|
33
|
+
it "should role date to 2 banking days ago padding = 2" do
|
34
|
+
options = {:previous => true,:padding => 2}
|
35
|
+
get_previous_banking_day(Date.parse("2012-03-27"),options).should eql(Date.parse("2012-03-25"))
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
it "should desc" do
|
42
|
+
options = {:padding => 1, :no_weekends => true}
|
43
|
+
get_banking_day(Date.parse("2012-03-27"),options).should eql(Date.parse("2012-03-28"))
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
it "should desc" do
|
48
|
+
options = {:padding => 2, :no_weekends => true}
|
49
|
+
get_banking_day(Date.parse("2012-03-27"),options).should eql(Date.parse("2012-03-29"))
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should desc" do
|
53
|
+
options = {:padding => 4, :no_weekends => true}
|
54
|
+
get_banking_day(Date.parse("2012-03-27"),options).should eql(Date.parse("2012-04-02"))
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
describe BankingDateTools, "get_next_banking_day" do
|
59
|
+
it "should desc" do
|
60
|
+
options = {:no_weekends => true}
|
61
|
+
get_next_banking_day(Date.parse("2012-03-27"),options).should eql(Date.parse("2012-03-27"))
|
62
|
+
end
|
63
|
+
it "should desc" do
|
64
|
+
options = {:padding => 1, :no_weekends => true}
|
65
|
+
get_next_banking_day(Date.parse("2012-03-27"),options).should eql(Date.parse("2012-03-28"))
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe BankingDateTools, "get_previous_banking_day" do
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
it "should desc" do
|
74
|
+
options = {:no_weekends => true}
|
75
|
+
get_previous_banking_day(Date.parse("2012-03-27"),options).should eql(Date.parse("2012-03-27"))
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should desc" do
|
79
|
+
options = {:padding => 1, :no_weekends => true}
|
80
|
+
get_previous_banking_day(Date.parse("2012-03-27"),options).should eql(Date.parse("2012-03-26"))
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: banking_date_tools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Joshua T Mckinney
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-30 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70315950609100 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70315950609100
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mocha
|
27
|
+
requirement: &70315950608600 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70315950608600
|
36
|
+
description: Banking Date Tools
|
37
|
+
email:
|
38
|
+
- joshmckin@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .autotest
|
44
|
+
- .gitignore
|
45
|
+
- .rspec
|
46
|
+
- Gemfile
|
47
|
+
- README
|
48
|
+
- Rakefile
|
49
|
+
- date_tools.gemspec
|
50
|
+
- lib/banking_date_tools.rb
|
51
|
+
- lib/banking_date_tools/banking_date_tools.rb
|
52
|
+
- lib/banking_date_tools/version.rb
|
53
|
+
- spec/lib/banking_date_tools_spec.rb
|
54
|
+
- spec/spec.opts
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
homepage: ''
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project: banking_date_tools
|
76
|
+
rubygems_version: 1.8.6
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Banking Date Tools
|
80
|
+
test_files:
|
81
|
+
- spec/lib/banking_date_tools_spec.rb
|
82
|
+
- spec/spec.opts
|
83
|
+
- spec/spec_helper.rb
|