holiday_japan 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .config
19
+ *~
20
+ */*~
21
+ */*/*~
22
+ *.bak
23
+ */*.bak
24
+ */*/*.bak
25
+ .#*
26
+ */.#*
27
+ */*/.#*
28
+ rhosts
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in holiday_japan.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Masahiro TANAKA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ # 日本の祝日判定Rubyプログラム
2
+
3
+ ## 特徴
4
+ (date2 の holiday.rb と比較して)
5
+ * 祝日をキャッシュすることにより、大量の日付について祝日判定する場合でも高速に動作
6
+ * 祝日名を返すことが可能
7
+ * 祝日データがテーブルになっているので、法改正による祝日変更への対応が容易
8
+
9
+ ## Rubyスクリプトでの使い方
10
+
11
+ * 準備
12
+
13
+ gem install holiday-japan
14
+
15
+ * または、holiday-japan.rb ファイルを ruby のライブラリパスに置く
16
+ * 自分のスクリプトの初めに次のように書いて、 holiday-japan.rb をロードする
17
+
18
+ require 'holiday-japan'
19
+
20
+ * Dateクラスのオブジェクトによる祝日判定
21
+
22
+ if HolidayJapan.check(Date.today)
23
+ ...
24
+
25
+ または
26
+
27
+ if Date.today.national_holiday?
28
+ ...
29
+
30
+ * 日付が祝日の場合は祝日名を返し、祝日でなければ nil を返す。
31
+
32
+ name = HolidayJapan.name(Date.new(2007,3,22))
33
+
34
+ * ある年について、 [日付, 祝日名] の配列を返す
35
+
36
+ list = HolidayJapan.list_year(2007)
37
+
38
+ * ある年の祝日一覧をプリント
39
+
40
+ HolidayJapan.print_year(2007)
41
+
42
+ ## コマンドラインから祝日一覧を表示
43
+
44
+ $ ruby holiday-japan.rb 2013
45
+ listing year 2013...
46
+ 2013-01-01 Tue 元日
47
+ 2013-01-14 Mon 成人の日
48
+ 2013-02-11 Mon 建国記念の日
49
+ 2013-03-20 Wed 春分の日
50
+ 2013-04-29 Mon 昭和の日
51
+ 2013-05-03 Fri 憲法記念日
52
+ 2013-05-04 Sat みどりの日
53
+ 2013-05-05 Sun こどもの日
54
+ 2013-05-06 Mon 振替休日
55
+ 2013-07-15 Mon 海の日
56
+ 2013-09-16 Mon 敬老の日
57
+ 2013-09-23 Mon 秋分の日
58
+ 2013-10-14 Mon 体育の日
59
+ 2013-11-03 Sun 文化の日
60
+ 2013-11-04 Mon 振替休日
61
+ 2013-11-23 Sat 勤労感謝の日
62
+ 2013-12-23 Mon 天皇誕生日
63
+
64
+ ## 祝日データ
65
+
66
+ * 1948年7月20日(祝日法発令) 以降の祝日に対応
67
+ * 2013年の暦要項まで確認
68
+
69
+ ## Author:
70
+ Masahiro TANAKA
71
+
72
+ ## Copyright:
73
+ (C) Copyright 2003-2007 by Masahiro TANAKA
74
+ This program is free software.
75
+ see LICENSE.txt.
76
+ NO WARRANTY.
77
+
78
+ ## Version:
79
+ 2012-12-23 ver 1.0 Holiday から HolidayJapan に変更
80
+ 2007-08-02 ver 0.9 リファクタリング
81
+ 2007-03-08 ver 0.8 祝日データクラスを統一、データを配列で記述
82
+ 2006-02-06 ver 0.7 平成19年(西暦2007年)の暦要項 反映(祝日法改正)
83
+ Holiday.create_table 修正
84
+ Holiday.list_year 追加
85
+ 2003-10-02 ver 0.6 祝日データ追加
86
+ 2003-09-29 ver 0.5
87
+ 2003-09-22 ver 0.4
88
+ 2003-09-20 ver 0.3
89
+ 2003-09-16 ver 0.2
90
+ 2003-09-15 ver 0.1
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'holiday_japan/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "holiday_japan"
8
+ gem.version = HolidayJapan::VERSION
9
+ gem.authors = ["Masahiro TANAKA"]
10
+ gem.email = ["masa16.tanaka@gmail.com"]
11
+ gem.description = %q{Calculate National Holidays of Japan}
12
+ gem.summary = %q{Calculate National Holidays of Japan}
13
+ gem.homepage = "https://github.com/masa16/holiday_japan"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,145 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "date"
3
+
4
+ module HolidayJapan
5
+
6
+ if !const_defined?("DATA")
7
+ Week1 = 1..7
8
+ Week2 = 8..14
9
+ Week3 = 15..21
10
+ Week4 = 22..28
11
+ Sun,Mon,Tue,Wed,Thu,Fru,Sat = (0..6).to_a
12
+ Inf = 1.0/0.0
13
+
14
+ # 祝日データ: 1948年7月20日以降で有効
15
+ DATA = [
16
+ ["元日", 1949..Inf , 1, 1 ],
17
+ ["成人の日", 1949..1999, 1, 15 ],
18
+ ["成人の日", 2000..Inf , 1, Week2, Mon ],
19
+ ["建国記念の日",1967..Inf , 2, 11 ],
20
+ ["天皇誕生日", 1949..1988, 4, 29 ],
21
+ ["みどりの日", 1989..2006, 4, 29 ],
22
+ ["昭和の日", 2007..Inf , 4, 29 ],
23
+ ["憲法記念日", 1949..Inf , 5, 3 ],
24
+ ["みどりの日", 2007..Inf , 5, 4 ],
25
+ ["こどもの日", 1949..Inf , 5, 5 ],
26
+ ["海の日", 1996..2002, 7, 20 ],
27
+ ["海の日", 2003..Inf , 7, Week3, Mon ],
28
+ ["敬老の日", 1966..2002, 9, 15 ],
29
+ ["敬老の日", 2003..Inf , 9, Week3, Mon ],
30
+ ["体育の日", 1966..1999, 10, 10 ],
31
+ ["体育の日", 2000..Inf , 10, Week2, Mon ],
32
+ ["文化の日", 1948..Inf , 11, 3 ],
33
+ ["勤労感謝の日",1948..Inf , 11, 23 ],
34
+ ["天皇誕生日", 1989..Inf , 12, 23 ],
35
+ ["春分の日", 1949..1979, 3,
36
+ proc{|y|Integer(20.8357+0.242194*(y-1980))-Integer((y-1983)/4.0)} ],
37
+ ["春分の日", 1980..2099, 3,
38
+ proc{|y|Integer(20.8431+0.242194*(y-1980))-Integer((y-1980)/4.0)} ],
39
+ ["秋分の日" , 1948..1979, 9,
40
+ proc{|y|Integer(23.2588+0.242194*(y-1980))-Integer((y-1983)/4.0)} ],
41
+ ["秋分の日" , 1980..2099, 9,
42
+ proc{|y|Integer(23.2488+0.242194*(y-1980))-Integer((y-1980)/4.0)} ],
43
+ ["皇太子明仁親王の結婚の儀", 1959..1959, 4, 10 ],
44
+ ["昭和天皇の大喪の礼", 1989..1989, 2, 24 ],
45
+ ["即位礼正殿の儀", 1990..1990, 11, 2 ],
46
+ ["皇太子徳仁親王の結婚の儀", 1993..1993, 6, 9 ]
47
+ ]
48
+ DATA.each{|x| x[0].freeze; x.freeze }
49
+ DATA.freeze
50
+ TABLE = {}
51
+ FURIKAE_START = Date.new(1973,4,12).freeze
52
+ end
53
+
54
+ module_function
55
+
56
+ def holiday_date(year,data)
57
+ name,year_range,mon,day,wday = data
58
+ if year_range === year
59
+ case day
60
+ when Fixnum
61
+ Date.new( year, mon, day )
62
+ when Range
63
+ wday0 = Date.new(year,mon,day.first).wday
64
+ Date.new( year, mon, day.first+(wday-wday0+7)%7 )
65
+ when Proc
66
+ Date.new( year, mon, day.call(year) )
67
+ end
68
+ end
69
+ end
70
+
71
+ def create_table(y)
72
+ h={}
73
+ a=[]
74
+ # list holidays
75
+ DATA.each do |x|
76
+ if d = holiday_date(y,x)
77
+ h[d] = x[0]
78
+ a << d
79
+ end
80
+ end
81
+ # compensating holiday
82
+ if y >= 2007
83
+ a.each do |d|
84
+ if d.wday==Sun
85
+ d+=1 while h[d]
86
+ h[d] = "振替休日"
87
+ end
88
+ end
89
+ elsif y >= 1973
90
+ a.each do |d|
91
+ if d.wday==Sun and d>=FURIKAE_START
92
+ h[d+1] = "振替休日"
93
+ end
94
+ end
95
+ end
96
+ # consecutive holiday
97
+ if y >= 1986
98
+ a.each do |d|
99
+ if h[d+2] and !h[d+1] and d.wday!=Sat
100
+ h[d+1] = "国民の休日"
101
+ end
102
+ end
103
+ end
104
+ h.freeze
105
+ end
106
+
107
+ def name(date)
108
+ y = date.year
109
+ (TABLE[y] ||= create_table(y))[date]
110
+ end
111
+
112
+ def check(date)
113
+ HolidayJapan.name(date) ? true : false
114
+ end
115
+
116
+ def list_year(year)
117
+ year = Integer(year)
118
+ TABLE[year] ||= create_table(year)
119
+ TABLE[year].sort_by{|x| x[0]}
120
+ end
121
+
122
+ def print_year(year)
123
+ puts "listing year #{year}..."
124
+ list_year(year).each do |y|
125
+ puts "#{y[0].strftime('%Y-%m-%d %a')} #{y[1]}"
126
+ end
127
+ end
128
+
129
+ end
130
+
131
+
132
+ # compatible with Funaba-san's holiday.rb
133
+ class Date
134
+ def national_holiday?
135
+ HolidayJapan.name(self) ? true : false
136
+ end
137
+ end
138
+
139
+ # command line
140
+ if __FILE__ == $0
141
+ # print holiday list of the year
142
+ HolidayJapan.print_year(ARGV[0])
143
+ else
144
+ require "holiday_japan/version"
145
+ end
@@ -0,0 +1,3 @@
1
+ module HolidayJapan
2
+ VERSION = "1.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: holiday_japan
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Masahiro TANAKA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Calculate National Holidays of Japan
15
+ email:
16
+ - masa16.tanaka@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - holiday_japan.gemspec
27
+ - lib/holiday_japan.rb
28
+ - lib/holiday_japan/version.rb
29
+ homepage: https://github.com/masa16/holiday_japan
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.11
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Calculate National Holidays of Japan
53
+ test_files: []