finnish-holidays 0.2.2 → 0.3.0
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.
- checksums.yaml +4 -4
- data/.gitignore +0 -2
- data/bin/finnish-holidays +2 -1
- data/lib/classes/calendar.rb +5 -8
- data/lib/classes/year.rb +28 -75
- data/lib/utils/date-utils.rb +102 -19
- data/lib/version.rb +1 -1
- metadata +2 -3
- data/data/.gitkeep +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2f98f959c3e3d413358ca9b43c1fb0d1da649207
|
|
4
|
+
data.tar.gz: ee529c556d7589b7abb33a898e84d58afbafed05
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dd21bfc4682c22986478b89020c6c7edc507fbf18c9f423bb3469102531330daba673370f609e83eb74e892cd0dc5b83c81ef810f4ba85ab35a6367b6a355ffa
|
|
7
|
+
data.tar.gz: dece5244f589cfdd0546f16721d0032ca2504aad5233832ae3b5b29a96caec6f6443cac91aa9000fde9f83b87c8a4416b939a3a451470c4279423de43d47cf42
|
data/.gitignore
CHANGED
data/bin/finnish-holidays
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
# encoding: UTF-8
|
|
3
3
|
|
|
4
|
+
require 'date'
|
|
4
5
|
require 'optparse'
|
|
5
6
|
require_relative '../lib/finnish-holidays'
|
|
6
7
|
require_relative '../lib/version'
|
|
@@ -54,7 +55,7 @@ else
|
|
|
54
55
|
end
|
|
55
56
|
|
|
56
57
|
holidays.each do |holiday|
|
|
57
|
-
t =
|
|
58
|
+
t = Date.new(holiday['year'], holiday['month'], holiday['day'])
|
|
58
59
|
date = t.strftime('%a, %b %e, %Y')
|
|
59
60
|
puts "#{date} #{holiday['description']}"
|
|
60
61
|
end
|
data/lib/classes/calendar.rb
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
require 'json'
|
|
2
1
|
require 'time'
|
|
3
2
|
require_relative 'year'
|
|
4
3
|
require_relative '../utils/date-utils'
|
|
@@ -22,15 +21,13 @@ class Calendar
|
|
|
22
21
|
holidays = []
|
|
23
22
|
|
|
24
23
|
while holidays.length < count
|
|
25
|
-
month_index = @m.to_s
|
|
26
|
-
|
|
27
24
|
if !include_weekends
|
|
28
25
|
@year.discard_weekends()
|
|
29
26
|
end
|
|
30
27
|
|
|
31
|
-
if (defined? @year.holidays[
|
|
32
|
-
@year.holidays[
|
|
33
|
-
if (holidays.length < count) && (holiday['day']
|
|
28
|
+
if (defined? @year.holidays[@m]) && (@year.holidays[@m].is_a? Array)
|
|
29
|
+
@year.holidays[@m].each do |holiday|
|
|
30
|
+
if (holidays.length < count) && (holiday['day'] >= @d)
|
|
34
31
|
holidays.push(holiday)
|
|
35
32
|
end
|
|
36
33
|
end
|
|
@@ -67,8 +64,8 @@ class Calendar
|
|
|
67
64
|
|
|
68
65
|
holidays = []
|
|
69
66
|
|
|
70
|
-
if @year.holidays[month
|
|
71
|
-
@year.holidays[month
|
|
67
|
+
if @year.holidays[month].is_a? Array
|
|
68
|
+
@year.holidays[month].each do |holiday|
|
|
72
69
|
holidays.push(holiday)
|
|
73
70
|
end
|
|
74
71
|
end
|
data/lib/classes/year.rb
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require 'open-uri'
|
|
3
|
-
require 'json'
|
|
4
|
-
require 'time'
|
|
1
|
+
require 'date'
|
|
5
2
|
require_relative '../utils/date-utils'
|
|
6
3
|
|
|
7
4
|
class Year
|
|
8
5
|
def initialize(year)
|
|
9
|
-
if
|
|
10
|
-
@year = year
|
|
6
|
+
if year > 0
|
|
7
|
+
@year = year
|
|
11
8
|
@holidays = {}
|
|
12
|
-
|
|
9
|
+
load_holidays()
|
|
13
10
|
else
|
|
14
11
|
raise "Invalid year: #{year}"
|
|
15
12
|
end
|
|
@@ -39,54 +36,32 @@ class Year
|
|
|
39
36
|
|
|
40
37
|
private
|
|
41
38
|
|
|
42
|
-
def
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
def load_holidays
|
|
40
|
+
# Holidays must be added in order
|
|
41
|
+
add_holiday(Date.new(@year, 1, 1), "New Year's Day")
|
|
42
|
+
add_holiday(Date.new(@year, 1, 6), 'Epiphany')
|
|
43
|
+
add_holiday(DateUtils.good_friday(@year), 'Good Friday')
|
|
44
|
+
add_holiday(DateUtils.easter_sunday(@year), 'Easter Sunday')
|
|
45
|
+
add_holiday(DateUtils.easter_monday(@year), 'Easter Monday')
|
|
46
|
+
add_holiday(Date.new(@year, 1, 1), 'May Day')
|
|
47
|
+
add_holiday(DateUtils.ascension_day(@year), 'Ascension Day')
|
|
48
|
+
add_holiday(DateUtils.pentecost(@year), 'Pentecost')
|
|
49
|
+
add_holiday(DateUtils.midsummer_eve(@year), 'Midsummer Eve (unofficial)')
|
|
50
|
+
add_holiday(DateUtils.midsummer_day(@year), 'Midsummer Day')
|
|
51
|
+
add_holiday(DateUtils.all_saints_day(@year), "All Saints' Day")
|
|
52
|
+
add_holiday(Date.new(@year, 12, 6), 'Independence Day')
|
|
53
|
+
add_holiday(Date.new(@year, 12, 24), 'Christmas Eve (unofficial)')
|
|
54
|
+
add_holiday(Date.new(@year, 12, 25), 'Christmas Day')
|
|
55
|
+
add_holiday(Date.new(@year, 12, 26), "St. Stephen's Day")
|
|
52
56
|
end
|
|
53
57
|
|
|
54
|
-
def
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def load_from_web
|
|
60
|
-
page = Nokogiri::HTML(open('http://www.webcal.fi/fi-FI/pyhat.php?y=' + @year.to_s))
|
|
61
|
-
|
|
62
|
-
page.css('table.basic tr').each do |el|
|
|
63
|
-
if el.css('th:last-child').text != 'Päivämäärä' # TODO: Improve check.
|
|
64
|
-
date_el = el.css('td:nth-child(4)')
|
|
65
|
-
description_el = el.css('td:nth-child(2)')
|
|
66
|
-
|
|
67
|
-
month = DateUtils.parse_month(date_el.text)
|
|
68
|
-
day = DateUtils.parse_day(date_el.text)
|
|
69
|
-
description = description_el.text
|
|
58
|
+
def add_holiday(date, description)
|
|
59
|
+
year = DateUtils.get_year_from_date(date)
|
|
60
|
+
month = DateUtils.get_month_from_date(date)
|
|
61
|
+
day = DateUtils.get_day_from_date(date)
|
|
70
62
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def load_additional_holidays
|
|
77
|
-
add_holiday(@year, 12, 24, 'Jouluaatto (unofficial)', true)
|
|
78
|
-
add_holiday(@year, 6, DateUtils.get_midsummer_eve(@year), 'Juhannusaatto (unofficial)', true)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def add_holiday(year, month, day, description, find_position = false)
|
|
82
|
-
year = year.to_i
|
|
83
|
-
month = month.to_i
|
|
84
|
-
day = day.to_i
|
|
85
|
-
|
|
86
|
-
month_index = month.to_s
|
|
87
|
-
|
|
88
|
-
if !@holidays[month_index].is_a? Array
|
|
89
|
-
@holidays[month_index] = []
|
|
63
|
+
if !@holidays[month].is_a? Array
|
|
64
|
+
@holidays[month] = []
|
|
90
65
|
end
|
|
91
66
|
|
|
92
67
|
holiday = {
|
|
@@ -96,28 +71,6 @@ private
|
|
|
96
71
|
'description' => description
|
|
97
72
|
}
|
|
98
73
|
|
|
99
|
-
|
|
100
|
-
found = false
|
|
101
|
-
|
|
102
|
-
@holidays[month_index].each_with_index do |h, index|
|
|
103
|
-
if !found
|
|
104
|
-
if day.to_i < h['day'].to_i
|
|
105
|
-
@holidays[month_index].insert(index, holiday)
|
|
106
|
-
found = true
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
else
|
|
111
|
-
@holidays[month_index].push(holiday)
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
def cache
|
|
116
|
-
File.write(get_cache_file_path(), @holidays.to_json)
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
def get_cache_file_path
|
|
120
|
-
cwd = File.dirname(__FILE__)
|
|
121
|
-
"#{cwd}/../../data/#{@year}.json"
|
|
74
|
+
@holidays[month].push(holiday)
|
|
122
75
|
end
|
|
123
76
|
end
|
data/lib/utils/date-utils.rb
CHANGED
|
@@ -1,41 +1,124 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
|
|
1
3
|
class DateUtils
|
|
2
4
|
FRIDAY = 5
|
|
3
5
|
SATURDAY = 6
|
|
4
6
|
SUNDAY = 7
|
|
5
7
|
|
|
6
|
-
def self.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
def self.is_weekend(year, month, day)
|
|
9
|
+
t = Date.new(year, month, day)
|
|
10
|
+
day_of_week = t.strftime('%u').to_i
|
|
11
|
+
[SATURDAY, SUNDAY].include? day_of_week
|
|
12
|
+
end
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
def self.easter_sunday(year)
|
|
15
|
+
a = year % 19
|
|
16
|
+
b = year / 100
|
|
17
|
+
c = year % 100
|
|
18
|
+
d = b / 4
|
|
19
|
+
e = b % 4
|
|
20
|
+
f = (b + 8) / 25
|
|
21
|
+
g = (b - f + 1) / 3
|
|
22
|
+
h = (19 * a + b - d - g + 15) % 30
|
|
23
|
+
i = c / 4
|
|
24
|
+
k = c % 4
|
|
25
|
+
l = (32 + 2 * e + 2 * i - h - k) % 7
|
|
26
|
+
m = (a + 11 * h + 22 * l) / 451
|
|
27
|
+
x = h + l - 7 * m + 114
|
|
28
|
+
month = x / 31
|
|
29
|
+
day = (x % 31) + 1
|
|
30
|
+
Date.new(year, month, day)
|
|
12
31
|
end
|
|
13
32
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
33
|
+
# Day after Easter Sunday
|
|
34
|
+
def self.easter_monday(year)
|
|
35
|
+
self.easter_sunday(year).next_day
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Friday before Easter Sunday
|
|
39
|
+
def self.good_friday(year)
|
|
40
|
+
date = self.easter_sunday(year)
|
|
41
|
+
day_of_week = nil
|
|
42
|
+
|
|
43
|
+
while day_of_week != FRIDAY
|
|
44
|
+
day_of_week = date.strftime('%u').to_i
|
|
45
|
+
|
|
46
|
+
if (day_of_week != FRIDAY)
|
|
47
|
+
date = date.prev_day
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
date
|
|
17
52
|
end
|
|
18
53
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
54
|
+
# 39 days after Easter Sunday
|
|
55
|
+
def self.ascension_day(year)
|
|
56
|
+
easter_sunday = self.easter_sunday(year)
|
|
57
|
+
easter_sunday.next_day(39)
|
|
22
58
|
end
|
|
23
59
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
60
|
+
# 49 days after Easter Sunday
|
|
61
|
+
def self.pentecost(year)
|
|
62
|
+
easter_sunday = self.easter_sunday(year)
|
|
63
|
+
easter_sunday.next_day(49)
|
|
28
64
|
end
|
|
29
65
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
66
|
+
# Friday between June 19-25
|
|
67
|
+
def self.midsummer_eve(year)
|
|
68
|
+
month = 6
|
|
69
|
+
|
|
70
|
+
(19..25).to_a.each do |day|
|
|
71
|
+
day_of_week = Date.new(year, month, day).strftime('%u').to_i
|
|
72
|
+
|
|
33
73
|
if day_of_week == FRIDAY
|
|
34
|
-
return day
|
|
74
|
+
return Date.new(year, month, day)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Saturday between June 20-26
|
|
80
|
+
def self.midsummer_day(year)
|
|
81
|
+
month = 6
|
|
82
|
+
|
|
83
|
+
(20..26).to_a.each do |day|
|
|
84
|
+
day_of_week = Date.new(year, month, day).strftime('%u').to_i
|
|
85
|
+
|
|
86
|
+
if day_of_week == SATURDAY
|
|
87
|
+
return Date.new(year, month, day)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Saturday between October 31 and November 6
|
|
93
|
+
def self.all_saints_day(year)
|
|
94
|
+
october_31 = Date.new(year, 10, 31)
|
|
95
|
+
second_month = 11
|
|
96
|
+
|
|
97
|
+
if october_31.strftime('%u').to_i == SATURDAY
|
|
98
|
+
return october_31
|
|
99
|
+
else
|
|
100
|
+
(1..6).to_a.each do |day|
|
|
101
|
+
day_of_week = Date.new(year, second_month, day).strftime('%u').to_i
|
|
102
|
+
|
|
103
|
+
if day_of_week == SATURDAY
|
|
104
|
+
return Date.new(year, second_month, day)
|
|
105
|
+
end
|
|
35
106
|
end
|
|
36
107
|
end
|
|
37
108
|
end
|
|
38
109
|
|
|
110
|
+
def self.get_year_from_date(date)
|
|
111
|
+
date.strftime('%Y').to_i
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def self.get_month_from_date(date)
|
|
115
|
+
date.strftime('%-m').to_i
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def self.get_day_from_date(date)
|
|
119
|
+
date.strftime('%-d').to_i
|
|
120
|
+
end
|
|
121
|
+
|
|
39
122
|
def self.zerofy(num)
|
|
40
123
|
num = num.to_i
|
|
41
124
|
|
data/lib/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: finnish-holidays
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Eric Nishio
|
|
@@ -84,7 +84,6 @@ files:
|
|
|
84
84
|
- README.md
|
|
85
85
|
- Rakefile
|
|
86
86
|
- bin/finnish-holidays
|
|
87
|
-
- data/.gitkeep
|
|
88
87
|
- finnish-holidays.gemspec
|
|
89
88
|
- lib/classes/calendar.rb
|
|
90
89
|
- lib/classes/year.rb
|
|
@@ -112,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
112
111
|
version: '0'
|
|
113
112
|
requirements: []
|
|
114
113
|
rubyforge_project:
|
|
115
|
-
rubygems_version: 2.0.
|
|
114
|
+
rubygems_version: 2.0.6
|
|
116
115
|
signing_key:
|
|
117
116
|
specification_version: 4
|
|
118
117
|
summary: A tool for displaying Finnish national holidays.
|
data/data/.gitkeep
DELETED
|
File without changes
|