bahai_date 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5d42db0fd17b26fc7407fea6e278734d8159a2a2
4
+ data.tar.gz: 819bc3a41f306df700094d1859cf2b5acfd1c5f9
5
+ SHA512:
6
+ metadata.gz: e375bc85d651c6aab277dd6faa2bd44f6b244ce6bddc641c79545170f1a665a926a904ed9b9601fcfa06a1ab85eae555407a9ce158dfbaf96f336170edf95472
7
+ data.tar.gz: e4399e7b2f57506dccbf46a68ede1bfd5b2f2e987b264287dbca219074fdd76c1b102b199fc7ae9cadd6c675e173fdbf8aaeea1bd3f726a0532434b26d249fff
data/LICENSE.md ADDED
@@ -0,0 +1,25 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
25
+
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ Baha'i Date Ruby Gem
2
+ ====================
3
+ A Ruby gem for conversion between the Gregorian calendar and the Baha'i (or Badi) calendar.
4
+
5
+
6
+ The Baha'i calendar
7
+ -------------------
8
+ In the Baha'i (or Badi) calendar there are 19 months each having 19 days, with 4 or 5 days left aside as intercalery days. Each weekday, day of the month and month of the year have a name, named after the attributes of God (such as Glory, Beauty, etc). The names have been transliterated into English from the original Arabic, and a translation is also provided.
9
+
10
+ Each year also has a name, as part of a 19 year cycle called a Vahid (meaning "unity"). Every 19 Vahids (i.e. 361 years) constitute a Kull-i-Shay (meaning "all things").
11
+
12
+ The calendar begins on the vernal equinox, normally on the 20th or 21st of March. The first year is 1844.
13
+
14
+ More information about this calendar is on the [Wikipedia entry](http://en.wikipedia.org/wiki/Bah%C3%A1'%C3%AD_calendar).
15
+
16
+
17
+ Functionality
18
+ -------------
19
+ A BahaiDate instance can be created either from a Gregorian date or supplying the year, month and day in the Baha'i calendar.
20
+
21
+ The instance then exposes:
22
+ - **weekday**
23
+ - **number**: e.g. 6
24
+ - **title**: e.g. Istijlal
25
+ - **translation**: e.g. Majesty
26
+ - **html**: e.g. Istijlál
27
+ - **english_equivalent**: e.g. Thursday
28
+ - **day**
29
+ - **number**: e.g. 1
30
+ - **title**: e.g. Baha
31
+ - **translation**: e.g. Splendour
32
+ - **html**: e.g. Bahá
33
+ - **month**
34
+ - **number**: e.g. 1
35
+ - **title**: e.g. Baha
36
+ - **translation**: e.g. Splendour
37
+ - **html**: e.g. Bahá
38
+ - **year**
39
+ - **bahai_era**: e.g. 172
40
+ - **number**: e.g. 1
41
+ - **title**: e.g. Alif
42
+ - **translation**: e.g. A
43
+ - **html**: e.g. Alif
44
+ - **vahid**: e.g. 10
45
+ - **kull_i_shay**: e.g. 1
46
+ - **gregorian_date**: e.g. 2015-3-21 (using the ruby Date class)
47
+ - **occasions**: array of Occasion objects:
48
+ - **type**: e.g. holy
49
+ - **work_suspended?**: e.g. true
50
+ - **title**: e.g. Naw-Ruz (New Year)
51
+ - **short_title**: e.g. Naw-Ruz
52
+ - **title_html**: e.g. Naw-Rúz (New Year)
53
+ - **short_title_html**: e.g. Naw-Rúz
54
+
55
+ A calendar can also be obtained using the YearCalendar class and providing a year. It is then populated with details about that year, each month and each day in it.
56
+
57
+
58
+ Installation
59
+ ------------
60
+ Add this line to your application's Gemfile:
61
+
62
+ `gem 'bahai_date'`
63
+
64
+ And then execute:
65
+
66
+ `$ bundle install`
67
+
68
+ Or, install it yourself:
69
+ `$ gem install bahai_date`
70
+
71
+
72
+ Example Usage
73
+ -------------
74
+ Creating an instance:
75
+ ```ruby
76
+ require "bahai_date"
77
+
78
+ today = BahaiDate.new(date: Date.today)
79
+
80
+ #or
81
+
82
+ nawruz = BahaiDate.new(year: 171, month: 1, day: 1)
83
+ ```
84
+
85
+ Attributes:
86
+ ```ruby
87
+ puts "to_s: " + today.to_s
88
+ puts "long_format: " + today.long_format
89
+ puts "gregorian_date: " + today.gregorian_date.to_s
90
+ ```
91
+ Outputs:
92
+ ```
93
+ to_s: 171.11.8
94
+ long_format: Jalal 8 Mashiyyat 171 B.E.
95
+ gregorian_date: 2014-10-04
96
+ ```
97
+
98
+ Calendar:
99
+ ```ruby
100
+ calendar = YearCalendar.new(171)
101
+ puts "Year: " + calendar.bahai_era.to_s
102
+ puts "Month 1:" + calendar.months[1].title
103
+ puts " Day 1 in Month 1:" + calendar.months[1].days[1].title
104
+ puts " (weekday):" + calendar.months[1].days[1].weekday.title
105
+ puts " (occasions):" + calendar.months[1].days[1].occasions[0].title
106
+ puts " " + calendar.months[1].days[1].occasions[1].title
107
+ ```
108
+ Outputs:
109
+ ```
110
+ Year: 171
111
+ Month 1:Baha
112
+ Day 1 in Month 1:Baha
113
+ (weekday):Istiqlal
114
+ (occasions):Naw-Ruz (New Year)
115
+ Nineteen Day Feast of the month of Baha (Splendour)
116
+ ```
117
+
118
+
119
+ License
120
+ -------
121
+ This is free and unencumbered software released into the public domain.
122
+
123
+ For more information, please refer to <http://unlicense.org/>
124
+
@@ -0,0 +1,118 @@
1
+ require 'date'
2
+ require_relative 'year'
3
+ require_relative 'month'
4
+ require_relative 'day'
5
+ require_relative 'weekday'
6
+ require_relative 'year_data'
7
+ require_relative 'occasion_factory'
8
+
9
+ module BahaiDate
10
+
11
+ class BahaiDate
12
+
13
+ AYYAM_I_HA = -1
14
+
15
+ attr_reader :weekday, :day, :month, :year, :gregorian_date, :occasions
16
+
17
+ def initialize(params)
18
+ if params[:date]
19
+ @gregorian_date = params[:date]
20
+ year, month, day = from_gregorian
21
+ @year = Year.new(year)
22
+ @month = Month.new(month)
23
+ @day = Day.new(day)
24
+ elsif params[:year] and params[:month] and params[:day]
25
+ @year = Year.new(params[:year])
26
+ @month = Month.new(params[:month])
27
+ @day = Day.new(params[:day])
28
+ validate_ayyam_i_ha
29
+ @gregorian_date = to_gregorian
30
+ else
31
+ raise ArgumentError, "Invalid arguments. Use a hash with :date or with :year, :month, and :day."
32
+ end
33
+ @weekday = Weekday.new(weekday_from_gregorian)
34
+ @occasions = OccasionFactory.new(@year.bahai_era, @month.number, @day.number).occasions
35
+ end
36
+
37
+ def to_s
38
+ "#{@year.bahai_era}.#{@month.number}.#{@day.number}"
39
+ end
40
+
41
+ def long_format
42
+ "#{@weekday} #{@day.number} #{@month} #{@year.bahai_era} B.E."
43
+ end
44
+
45
+ def next_day!
46
+ initialize(date: @gregorian_date + 1)
47
+ end
48
+
49
+ private
50
+
51
+ def validate_ayyam_i_ha
52
+ if @month.number == AYYAM_I_HA && @day.number > ayyam_i_ha_days
53
+ raise ArgumentError, "'#{@day.number}' is not a valid day for Ayyam-i-Ha in the year #{@year.bahai_era}"
54
+ end
55
+ end
56
+
57
+ def ayyam_i_ha_days(year = @year.bahai_era)
58
+ YearData.is_leap?(year) ? 5 : 4
59
+ end
60
+
61
+ def to_gregorian
62
+ year_gregorian = @year.bahai_era + 1844 - 1
63
+ nawruz = YearData.nawruz_for(year_gregorian)
64
+ nawruz + days_from_nawruz
65
+ end
66
+
67
+ def from_gregorian
68
+ nawruz = YearData.nawruz_for(@gregorian_date.year)
69
+
70
+ year = @gregorian_date.year - 1844
71
+ if @gregorian_date >= nawruz then
72
+ year += 1
73
+ days = (@gregorian_date - nawruz).to_i
74
+ else
75
+ days = (@gregorian_date - YearData.nawruz_for(@gregorian_date.year - 1)).to_i
76
+ end
77
+
78
+ # determine day and month, taking into account ayyam-i-ha
79
+ if days >= 342
80
+ if days < (342 + ayyam_i_ha_days(year))
81
+ month = AYYAM_I_HA
82
+ day = days - 342
83
+ else
84
+ month = 19
85
+ day = days - (342 + ayyam_i_ha_days(year))
86
+ end
87
+ else
88
+ month, day = (days).divmod(19)
89
+ month += 1
90
+ end
91
+ day += 1
92
+ [year, month, day]
93
+ end
94
+
95
+ def weekday_from_gregorian
96
+ # saturday (6 in ruby) is the first day of the week
97
+ @gregorian_date.wday == 6 ? 1 : @gregorian_date.wday + 2
98
+ end
99
+
100
+ def days_from_nawruz
101
+ days = @day.number - 1
102
+
103
+ full_months = @month.number - 1
104
+ if @month.number == AYYAM_I_HA
105
+ full_months = 18
106
+ end
107
+ days += full_months * 19
108
+
109
+ if @month.number == 19
110
+ days += ayyam_i_ha_days
111
+ end
112
+
113
+ days
114
+ end
115
+
116
+ end
117
+
118
+ end
@@ -0,0 +1,54 @@
1
+ module BahaiDate
2
+
3
+ class Day
4
+ TITLES = %w[Baha Jalal Jamal Azamat Nur Rahmat Kalimat Kamal Asma Izzat Mashiyyat Ilm Qudrat Qawl Masail Sharaf Sultan Mulk Ala]
5
+ TITLES_HTML = %w[Bahá Jalál Jamál ‘Aẓamat Núr Raḥmat Kalimát Kamál Asmá’ ‘Izzat Ma<u>sh</u>íyyat ‘Ilm Qudrat Qawl Masá’il <u>Sh</u>araf Sulṭán Mulk ‘Alá’]
6
+ TITLES_EN = %w[Splendour Glory Beauty Grandeur Light Mercy Words Perfection Names Might Will Knowledge Power Speech Questions Honour Sovereignty Dominion Loftiness]
7
+
8
+ attr_reader :number, :weekday, :occasions
9
+
10
+ def initialize(number_arg)
11
+ validate number_arg
12
+ @number = number_arg.to_i
13
+ end
14
+
15
+ def to_s
16
+ title
17
+ end
18
+
19
+ def title
20
+ TITLES[title_index]
21
+ end
22
+
23
+ def translation
24
+ TITLES_EN[title_index]
25
+ end
26
+
27
+ def html
28
+ TITLES_HTML[title_index]
29
+ end
30
+
31
+ def set_weekday(weekday)
32
+ @weekday = weekday
33
+ end
34
+
35
+ def set_occasions(occasions)
36
+ @occasions = occasions
37
+ end
38
+
39
+ private
40
+
41
+ def title_index
42
+ @number - 1
43
+ end
44
+
45
+ def validate(number_arg)
46
+ number = number_arg.to_i
47
+ if (number < 1 || number > 19)
48
+ raise ArgumentError, "'#{number}' is not a valid day. Please use 1 to 19."
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,57 @@
1
+ module BahaiDate
2
+
3
+ class Month
4
+ TITLES = %w[Baha Jalal Jamal Azamat Nur Rahmat Kalimat Kamal Asma Izzat Mashiyyat Ilm Qudrat Qawl Masail Sharaf Sultan Mulk Ala Ayyam-i-Ha]
5
+ TITLES_HTML = %w[Bahá Jalál Jamál ‘Aẓamat Núr Raḥmat Kalimát Kamál Asmá’ ‘Izzat Ma<u>sh</u>íyyat ‘Ilm Qudrat Qawl Masá’il <u>Sh</u>araf Sulṭán Mulk ‘Alá’ Ayyám-i-Há]
6
+ TITLES_EN = %w[Splendour Glory Beauty Grandeur Light Mercy Words Perfection Names Might Will Knowledge Power Speech Questions Honour Sovereignty Dominion Loftiness Ayyam-i-Ha]
7
+
8
+ attr_reader :number, :days
9
+
10
+ def initialize(number_arg)
11
+ validate number_arg
12
+ @number = number_arg.to_i
13
+ @days = {}
14
+ end
15
+
16
+ def to_s
17
+ title
18
+ end
19
+
20
+ def title
21
+ TITLES[title_index]
22
+ end
23
+
24
+ def translation
25
+ TITLES_EN[title_index]
26
+ end
27
+
28
+ def html
29
+ TITLES_HTML[title_index]
30
+ end
31
+
32
+ def set_day(day_number)
33
+ unless @days[day_number]
34
+ @days[day_number] = Day.new(day_number)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def title_index
41
+ if @number == -1
42
+ 19 #20th element of the array
43
+ else
44
+ @number - 1
45
+ end
46
+ end
47
+
48
+ def validate(number_arg)
49
+ number = number_arg.to_i
50
+ if (number < -1 || number == 0 || number > 19 )
51
+ raise ArgumentError, "'#{number}' is not a valid month. Please use 1 to 19 or -1 for Ayyam-i-Ha."
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,24 @@
1
+ module BahaiDate
2
+
3
+ class Occasion
4
+
5
+ attr_reader :type,
6
+ :work_suspended,
7
+ :title,
8
+ :short_title,
9
+ :title_html,
10
+ :short_title_html
11
+ alias_method :work_suspended?, :work_suspended
12
+
13
+ def initialize(opts = {})
14
+ @type = opts[:type]
15
+ @work_suspended = opts[:work_suspended]
16
+ @title = opts[:title]
17
+ @short_title = opts[:short_title]
18
+ @title_html = opts[:title_html]
19
+ @short_title_html = opts[:short_title_html]
20
+ end
21
+
22
+ end
23
+
24
+ end