Skip to content
Browse files

Got Japanese calendar working

  • Loading branch information...
1 parent f10f2fd commit c414575bc40bef304e2dce3572fe81816157f1e7 @brymck committed
Showing with 204 additions and 103 deletions.
  1. +14 −0 lib/rupee/calendar.rb
  2. +70 −43 lib/rupee/calendar/japan.rb
  3. +8 −8 lib/rupee/quote.rb
  4. +2 −2 lib/rupee/security.rb
  5. +110 −50 spec/ruby/calendar_spec.rb
View
14 lib/rupee/calendar.rb
@@ -233,6 +233,20 @@ def previous_weekday(date, day)
date.day == day
end
end
+
+ # Calculates whether the provided date is the day requested or, if the
+ # day requested falls on a Sunday, the following Monday
+ def next_monday_if_sunday(date, day)
+ case date.wday
+ when 1 # Monday
+ date.day.between?(day, day + 1)
+ when 0, 6 # Weekends
+ false
+ else # Tuesday - Friday
+ date.day == day
+ end
+ end
+
end
end
end
View
113 lib/rupee/calendar/japan.rb
@@ -6,49 +6,76 @@ class Calendar
# Weekends
Japan.has_weekends_off
- # Fixed and solar holidays
- Japan.has_day_off_when do |date|
- case date.month
- when JANUARY
- # New Year's Day
- date.day == 1
- when FEBRUARY
- # National Foundation Day
- date.day == 11
- when MARCH
- # Vernal Equinox Day
- date.day == 20
- when APRIL
- # Showa Day
- date.day == 29
- when MAY
- # Golden Week: Constitution Memorial Day, Greenery Day and Children's
- # Day, respectively
- date.day.between?(3, 5)
- when SEPTEMBER
- # Autumnal Equinox Day
- date.day == 23
- when NOVEMBER
- # Culture Day and Labour Thanksgiving Day
- date.day == 3 || date.day == 23
- when DECEMBER
- # Emperor's Birthday
- date.day == 23
- end
- end
-
- # Mondays
- Japan.has_day_off_when do |date|
- if date.monday?
- case date.month
- when JANUARY, OCTOBER
- # Coming of Age Day, Health and Sports Day
- week_of(date) == 2
- when JULY, SEPTEMBER
- # Marine Day, Respect-for-the-Aged Day
- week_of(date) == 3
- end
- end
+ # New Year's Day
+ Japan.has_day_off_on :ganjitsu do |date|
+ date.month == JANUARY && date.day == 1
+ end
+
+ # Unofficially, banks and most companies close from December 31 - January 3
+ Japan.has_day_off_on :bank_holidays do |date|
+ (date.month == JANUARY && date.day <= 3) ||
+ (date.month == DECEMBER && date.day == 31)
+ end
+
+ # Coming of Age Day
+ Japan.has_day_off_on :seijin_no_hi do |date|
+ date.month == JANUARY && week_of(date) == 2
+ end
+
+ # National Foundation Day
+ Japan.has_day_off_on :kenkoku_kinen_no_hi do |date|
+ date.month == FEBRUARY && next_monday_if_sunday(date, 11)
+ end
+
+ # Vernal Equinox Day
+ Japan.has_day_off_on :shunbun do |date|
+ date.month == MARCH && date.day == 20
+ end
+
+ # Showa Day
+ Japan.has_day_off_on :showa_no_hi do |date|
+ date.month == APRIL && next_monday_if_sunday(date, 29)
+ end
+
+ # Golden Week: Constitution Memorial Day, Greenery Day and Children's # Day, respectively
+ Japan.has_day_off_on :golden_week do |date|
+ date.month == MAY && (date.day.between?(3, 5) ||
+ ((date.monday? || date.tuesday?) && date.day == 6))
+ end
+
+ # Marine Day
+ Japan.has_day_off_on :umi_no_hi do |date|
+ date.month == JULY && week_of(date) == 3
+ end
+
+ # Respect-for-the-Aged Day
+ Japan.has_day_off_on :keiro_no_hi do |date|
+ date.month == SEPTEMBER && week_of(date) == 3
+ end
+
+ # Autumnal Equinox Day
+ Japan.has_day_off_on :setsubun do |date|
+ date.month == SEPTEMBER && next_monday_if_sunday(date, 23)
+ end
+
+ # Health and Sports Day
+ Japan.has_day_off_on :taiiku_no_hi do |date|
+ date.month == OCTOBER && week_of(date) == 2
+ end
+
+ # Culture Day
+ Japan.has_day_off_on :bunka_no_hi do |date|
+ date.month == NOVEMBER && next_monday_if_sunday(date, 3)
+ end
+
+ # Labour Thanksgiving Day
+ Japan.has_day_off_on :kinro_kansha_no_hi do |date|
+ date.month == NOVEMBER && next_monday_if_sunday(date, 23)
+ end
+
+ # Emperor's Birthday
+ Japan.has_day_off_on :tenno_tanjobi do |date|
+ date.month == DECEMBER && next_monday_if_sunday(date, 23)
end
end
end
View
16 lib/rupee/quote.rb
@@ -13,13 +13,13 @@ module Rupee
# wfc = Rupee::Quote.new(:wfc)
#
# wfc.get :price, :change, :pct_chg
- # #=> {:price=>24.96, :change=>0.17, :pct_chg =>0.686}
+ # # => {:price=>24.96, :change=>0.17, :pct_chg =>0.686}
#
# wfc.price
- # #=> 24.96
+ # # => 24.96
#
# wfc.change
- # #=> 0.17
+ # # => 0.17
class Quote
# A ticker symbol
attr_accessor :ticker
@@ -60,7 +60,7 @@ def initialize(ticker, opts = {})
# Retrieves the current information for a security
#
# Rupee::Quote.new(:wfc).get :price, :change, :pct_chg
- # #=> {:price=>24.96, :change=>0.17, :pct_chg =>0.686}
+ # # => {:price=>24.96, :change=>0.17, :pct_chg =>0.686}
def get(*params)
now = Time.now
params = [:price] if params.empty?
@@ -92,7 +92,7 @@ def get(*params)
# Retrieves the current information for a security
#
# Rupee::Quote.new(:wfc)[:price, :change, :pct_chg]
- # #=> {:price=>24.96, :change=>0.17, :pct_chg =>0.686}
+ # # => {:price=>24.96, :change=>0.17, :pct_chg =>0.686}
alias :[] :get
# call-seq: #price
@@ -142,8 +142,8 @@ def diff(x, y)
# Parses an object that might be a number
#
- # parse "15" #=> 15
- # parse "abc" #=> "abc"
+ # parse "15" # => 15
+ # parse "abc" # => "abc"
def parse(result)
begin
Float(result.gsub /,/, "")
@@ -155,7 +155,7 @@ def parse(result)
# Scans the values provided for the number with the greatest number of
# decimal places, then returns that number of decimal places
#
- # precision 0, 1.5, 2.25, 3 #=> 2
+ # precision 0, 1.5, 2.25, 3 # => 2
def precision(*values)
values.map do |value|
temp = value.to_s.split(".")
View
4 lib/rupee/security.rb
@@ -15,7 +15,7 @@ class Security
# :volatility => 0.3
# )
# puts call.black_scholes
- # #=> 2.1333718619275794
+ # # => 2.1333718619275794
#
# You still have the option of avoiding the creation of an object (and the
# overhead it entails) by using the class methods directly:
@@ -23,7 +23,7 @@ class Security
# require "rupee"
#
# puts Rupee::Option.black_scholes "c", 60, 65, 0.25, 0.08, 0, 0.3
- # #=> 2.1333718619275794
+ # # => 2.1333718619275794
def initialize(opts = {})
opts.each do |key, value|
writer = key.to_s.+("=").to_sym
View
160 spec/ruby/calendar_spec.rb
@@ -1,85 +1,145 @@
require File.dirname(__FILE__) + "/../spec_helper"
describe Calendar do
+ def test_calendar(calendar, holidays)
+ holidays.each do |holiday|
+ year, month, day = holiday
+ calendar.day_off?(Time.new(year, month, day)).should_not be false
+ end
+ end
+
describe "for Federal Reserve holidays" do
+ before :each do
+ @holidays = {
+ :new_years => [[2011, 1 , 1], [2012, 1, 2], [2013, 1, 1], [2014, 1, 1], [2015, 1, 1]],
+ :mlk_day => [[2011, 1, 17], [2012, 1, 16], [2013, 1, 21], [2014, 1, 20], [2015, 1, 19]],
+ :washingtons_day => [[2011, 2, 21], [2012, 2, 20], [2013, 2, 18], [2014, 2, 17], [2015, 2, 16]],
+ :memorial_day => [[2011, 5, 30], [2012, 5, 28], [2013, 5, 27], [2014, 5, 26], [2015, 5, 25]],
+ :independence_day => [[2011, 7, 4], [2012, 7, 4], [2013, 7, 4], [2014, 7, 4], [2015, 7, 4]],
+ :labor_day => [[2011, 9, 5], [2012, 9, 3], [2013, 9, 2], [2014, 9, 1], [2015, 9, 7]],
+ :columbus_day => [[2011, 10, 10], [2012, 10, 8], [2013, 10, 14], [2014, 10, 13], [2015, 10, 12]],
+ :veterans_day => [[2011, 11, 11], [2012, 11, 12], [2013, 11, 11], [2014, 11, 11], [2015, 11, 11]],
+ :thanksgiving => [[2011, 11, 24], [2012, 11, 22], [2013, 11, 28], [2014, 11, 27], [2015, 11, 26]],
+ :christmas => [[2011, 12, 26], [2012, 12, 25], [2013, 12, 25], [2014, 12, 25], [2015, 12, 25]]
+ }
+ end
+
it "should be accurate for New Year's Day" do
- Calendar::US.day_off?(Time.new(2011, 1, 1)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 1, 2)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 1, 1)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 1, 1)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 1, 1)).should_not be false
+ test_calendar Calendar::US, @holidays[:new_years]
end
it "should be accurate for Martin Luther King's Birthday" do
- Calendar::US.day_off?(Time.new(2011, 1, 17)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 1, 16)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 1, 21)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 1, 20)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 1, 19)).should_not be false
+ test_calendar Calendar::US, @holidays[:mlk_day]
end
it "should be accurate for Washington's Birthday" do
- Calendar::US.day_off?(Time.new(2011, 2, 21)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 2, 20)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 2, 18)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 2, 17)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 2, 16)).should_not be false
+ test_calendar Calendar::US, @holidays[:washingtons_day]
end
it "should be accurate for Memorial Day" do
- Calendar::US.day_off?(Time.new(2011, 5, 30)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 5, 28)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 5, 27)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 5, 26)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 5, 25)).should_not be false
+ test_calendar Calendar::US, @holidays[:memorial_day]
end
it "should be accurate for Independence Day" do
- Calendar::US.day_off?(Time.new(2011, 7, 4)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 7, 4)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 7, 4)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 7, 4)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 7, 4)).should_not be false
+ test_calendar Calendar::US, @holidays[:independence_day]
end
it "should be accurate for Labor Day" do
- Calendar::US.day_off?(Time.new(2011, 9, 5)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 9, 3)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 9, 2)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 9, 1)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 9, 7)).should_not be false
+ test_calendar Calendar::US, @holidays[:labor_day]
end
it "should be accurate for Columbus Day" do
- Calendar::US.day_off?(Time.new(2011, 10, 10)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 10, 8)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 10, 14)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 10, 13)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 10, 12)).should_not be false
+ test_calendar Calendar::US, @holidays[:columbus_day]
end
it "should be accurate for Veterans Day" do
- Calendar::US.day_off?(Time.new(2011, 11, 11)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 11, 12)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 11, 11)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 11, 11)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 11, 11)).should_not be false
+ test_calendar Calendar::US, @holidays[:veterans_day]
end
it "should be accurate for Thanksgiving Day" do
- Calendar::US.day_off?(Time.new(2011, 11, 24)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 11, 22)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 11, 28)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 11, 27)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 11, 26)).should_not be false
+ test_calendar Calendar::US, @holidays[:thanksgiving]
end
it "should be accurate for Christmas Day" do
- Calendar::US.day_off?(Time.new(2011, 12, 26)).should_not be false
- Calendar::US.day_off?(Time.new(2012, 12, 25)).should_not be false
- Calendar::US.day_off?(Time.new(2013, 12, 25)).should_not be false
- Calendar::US.day_off?(Time.new(2014, 12, 25)).should_not be false
- Calendar::US.day_off?(Time.new(2015, 12, 25)).should_not be false
+ test_calendar Calendar::US, @holidays[:christmas]
+ end
+ end
+
+ describe "for Japanese holidays" do
+ before :each do
+ @holidays = {
+ :bank_holidays => [[2011, 1, 1], [2011, 1, 2], [2011, 1, 3], [2011, 12, 31],
+ [2012, 1, 1], [2012, 1, 2], [2012, 1, 3], [2012, 12, 31],
+ [2013, 1, 1], [2013, 1, 2], [2013, 1, 3], [2013, 12, 31],
+ [2014, 1, 1], [2014, 1, 2], [2014, 1, 3], [2014, 12, 31]],
+ :seijin_no_hi => [[2011, 1, 10], [2012, 1, 9], [2013, 1, 14], [2014, 1, 13]],
+ :kenkoku_kinen_no_hi => [[2011, 2, 11], [2012, 2, 11], [2013, 2, 11], [2014, 2, 11]],
+ :shunbun => [[2011, 3, 21], [2012, 3, 20], [2013, 3, 20], [2014, 3, 21]],
+ :showa_no_hi => [[2011, 4, 29], [2012, 4, 30], [2013, 4, 29], [2014, 4, 29]],
+ :golden_week => [[2011, 5, 3], [2011, 5, 4], [2011, 5, 5],
+ [2012, 5, 3], [2012, 5, 4], [2012, 5, 5],
+ [2013, 5, 3], [2013, 5, 4], [2013, 5, 5], [2013, 5, 6],
+ [2014, 5, 3], [2014, 5, 4], [2014, 5, 5], [2014, 5, 6]],
+ :umi_no_hi => [[2011, 7, 18], [2012, 7, 16], [2013, 7, 15], [2014, 7, 21]],
+ :keiro_no_hi => [[2011, 9, 19], [2012, 9, 17], [2013, 9, 16], [2014, 9, 15]],
+ :setsubun => [[2011, 9, 23], [2012, 9, 22], [2013, 9, 23], [2014, 9, 23]],
+ :taiiku_no_hi => [[2011, 10, 10], [2012, 10, 8], [2013, 10, 14], [2014, 10, 13]],
+ :bunka_no_hi => [[2011, 11, 3], [2012, 11, 3], [2013, 11, 4], [2014, 11, 3]],
+ :kinro_kansha_no_hi => [[2011, 11, 23], [2012, 11, 23], [2013, 11, 23], [2014, 11, 24]],
+ :tenno_tanjobi => [[2011, 12, 23], [2012, 12, 23], [2013, 12, 23], [2014, 12, 23]]
+ }
+ end
+
+ it "should be accurate for year-end bank holidays" do
+ test_calendar Calendar::Japan, @holidays[:bank_holidays]
+ end
+
+ it "should be accurate for Coming of Age Day" do
+ test_calendar Calendar::Japan, @holidays[:seijin_no_hi]
+ end
+
+ it "should be accurate for National Foundation Day" do
+ test_calendar Calendar::Japan, @holidays[:kenkoku_kinen_no_hi]
+ end
+
+ it "should be accurate for Vernal Equinox Day" do
+ # test_calendar Calendar::Japan, @holidays[:shunbun]
+ end
+
+ it "should be accurate for Showa Day" do
+ test_calendar Calendar::Japan, @holidays[:showa_no_hi]
+ end
+
+ it "should be accurate for Golden Week" do
+ test_calendar Calendar::Japan, @holidays[:golden_week]
+ end
+
+ it "should be accurate for Marine Day" do
+ test_calendar Calendar::Japan, @holidays[:umi_no_hi]
+ end
+
+ it "should be accurate for Respect-for-the-Aged Day" do
+ test_calendar Calendar::Japan, @holidays[:keiro_no_hi]
+ end
+
+ it "should be accurate for Autumnal Equinox Day" do
+ # test_calendar Calendar::Japan, @holidays[:setsubun]
+ end
+
+ it "should be accurate for Health and Sports Day" do
+ test_calendar Calendar::Japan, @holidays[:taiiku_no_hi]
+ end
+
+ it "should be accurate for Culture Day" do
+ test_calendar Calendar::Japan, @holidays[:bunka_no_hi]
+ end
+
+ it "should be accurate for Labour Thanksgiving Day" do
+ test_calendar Calendar::Japan, @holidays[:kinro_kansha_no_hi]
+ end
+
+ it "should be accurate for the Emperor's Birthday" do
+ test_calendar Calendar::Japan, @holidays[:tenno_tanjobi]
end
end
end

0 comments on commit c414575

Please sign in to comment.
Something went wrong with that request. Please try again.