Permalink
Browse files

Got Japanese calendar working

  • Loading branch information...
brymck committed Sep 30, 2011
1 parent f10f2fd commit c414575bc40bef304e2dce3572fe81816157f1e7
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
@@ -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
@@ -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
@@ -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
@@ -15,15 +15,15 @@ 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:
#
# 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
Oops, something went wrong.

0 comments on commit c414575

Please sign in to comment.