Hebruby 1.2.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.
Files changed (4) hide show
  1. data/README +15 -0
  2. data/lib/hebruby.rb +251 -0
  3. data/test/hebruby_tests.rb +152 -0
  4. metadata +48 -0
data/README ADDED
@@ -0,0 +1,15 @@
1
+ # Hebruby is a Ruby library to convert julian dates to hebrew dates, and vice-versa.
2
+ # Written by Ron Evans (ron dot evans at gmail dot com or http://www.deadprogrammersociety.com).
3
+ # Additional code contributed by Joshua Harvey.
4
+ # Based on Javascript code from John Walker (http://www.fourmilab.ch/documents/calendar/).
5
+ # Usage:
6
+ # For julian to hebrew:
7
+ # @hb = Hebruby::HebrewDate.new(Date.new(2010, 1, 1))
8
+ # assert_equal(10, @hb.month, "Wrong month.")
9
+ # assert_equal("Tevet", @hb.month_name, "Wrong month name.")
10
+ # assert_equal(5770, @hb.year, "Wrong year.")
11
+ # assert_equal(15, @hb.day, "Wrong day.")
12
+ #
13
+ # For hebrew to julian:
14
+ # @hb = Hebruby::HebrewDate.new1(15,10,5770)
15
+ # assert_equal(Date.new(2010, 1, 1).jd, @hb.jd, "Wrong Julian date.")
data/lib/hebruby.rb ADDED
@@ -0,0 +1,251 @@
1
+ # Hebruby is a Ruby library to convert julian dates to hebrew dates, and vice-versa.
2
+ # Written by Ron Evans (ron dot evans at gmail dot com or http://www.deadprogrammersociety.com).
3
+ # Additional code contributed by Joshua Harvey.
4
+ # Based on Javascript code from John Walker (http://www.fourmilab.ch/documents/calendar/).
5
+ # Usage:
6
+ # For julian to hebrew:
7
+ # @hb = Hebruby::HebrewDate.new(Date.new(2010, 1, 1))
8
+ # assert_equal(10, @hb.month, "Wrong month.")
9
+ # assert_equal("Tevet", @hb.month_name, "Wrong month name.")
10
+ # assert_equal(5770, @hb.year, "Wrong year.")
11
+ # assert_equal(15, @hb.day, "Wrong day.")
12
+ #
13
+ # For hebrew to julian:
14
+ # @hb = Hebruby::HebrewDate.new1(15,10,5770)
15
+ # assert_equal(Date.new(2010, 1, 1).jd, @hb.jd, "Wrong Julian date.")
16
+
17
+ require 'jcode'
18
+ $KCODE = 'u' # Always use UTF-8 internally!
19
+ require 'date'
20
+
21
+ module Hebruby
22
+
23
+ class HebrewDate
24
+ HEBREW_EPOCH = 347995.5
25
+ MONTH_NAMES = %w{none Nissan Iyar Sivan Tamuz Av Elul Tishrei Chesvan Kislev Tevet Shvat Adar Veadar}
26
+ HEB_MONTH_NAMES = [ nil, 'ניסן', 'אייר', 'סיון', 'תמוז', 'אב', 'אלול', 'תשרי',
27
+ 'חשון', 'כסלו', 'טבת', 'שבט', 'אדר', 'אדר א\'', 'אדר ב\'']
28
+ HEB_DAYS = [ nil, 'א\'', 'ב\'', 'ג\'', 'ד\'', 'ה\'', 'ו\'', 'ז\'', 'ח\'', 'ט\'',
29
+ 'י\'', 'י"א', 'י"ב', 'י"ג', 'י"ד', 'ט"ו', 'ט"ז', 'י"ז', 'י"ח', 'י"ח',
30
+ 'י"ט', 'כ\'' , 'כ"א', 'כ"ב', 'כ"ג', 'כ"ד', 'כ"ה', 'כ"ו', 'כ"ז', 'כ"ט', 'ל\'' ]
31
+ ONES = [ '', 'א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ז', 'ח', 'ט' ]
32
+ TENS = [ '', 'י', 'כ', 'ל', 'מ', 'נ', 'ס', 'ע', 'פ', 'צ' ]
33
+
34
+
35
+ # Accessors for base Hebrew day, month, and year
36
+ attr_accessor :hd, :hm, :hy
37
+
38
+ # standard constructor from julian to hebrew date
39
+ def initialize(jd=nil)
40
+ if jd
41
+ @jd = jd
42
+ convert_from_julian
43
+ end
44
+ end
45
+
46
+ # additional constructor from hebrew date to julian
47
+ def self.new1(hd, hm, hy)
48
+ me = new
49
+ me.hd = hd
50
+ me.hm = hm
51
+ me.hy = hy
52
+ me.convert_from_hebrew
53
+ return me
54
+ end
55
+
56
+ def day
57
+ return @hd
58
+ end
59
+
60
+ # Provide correct Hebrew transliterated month display name
61
+ def month
62
+ return @hm
63
+ end
64
+
65
+ # return Hebrew year converted from julian date
66
+ def year
67
+ return @hy
68
+ end
69
+
70
+ # return julian date converted from hebrew date
71
+ def jd
72
+ return @jd + 1
73
+ end
74
+
75
+ # Provide correct Hebrew transliterated month display name
76
+ def month_name
77
+ return month_display_name(@hm)
78
+ end
79
+
80
+ # Provide correct Hebrew month display name
81
+ def heb_month_name
82
+ return heb_month_display_name(@hm)
83
+ end
84
+
85
+ # Provide correct Hebrew day display name
86
+ def heb_day_name
87
+ return HEB_DAYS[@hd]
88
+ end
89
+
90
+ # Provide correct Hebrew year display name
91
+ def heb_year_name
92
+ year = @hy
93
+ raise RangeError, "only 5700 - 5899 supported" if year < 5700 || year >= 5900
94
+ prefix = year / 100 == 57 ? "התש" : "התת"
95
+ suffix = heb_number(year % 100)
96
+ full = prefix + suffix
97
+ end
98
+
99
+ # Provide correct complete Hebrew display date
100
+ def heb_date
101
+ return heb_day_name + " ב" + heb_month_name + " " + heb_year_name
102
+ end
103
+
104
+ # Provide correct Hebrew number display
105
+ def heb_number(num)
106
+ return 'ט"ו' if num == 15
107
+ return 'ט"ז' if num == 16
108
+ if num < 10
109
+ return '"' + ONES[ num % 10 ]
110
+ elsif num % 10 == 0
111
+ return '"' + TENS[ num / 10 ]
112
+ else
113
+ return TENS[ num / 10 ] + '"' + ONES[ num % 10 ]
114
+ end
115
+ end
116
+
117
+ # Provide correct Hebrew transliterated month display name
118
+ def month_display_name(month)
119
+ return MONTH_NAMES[month]
120
+ end
121
+
122
+ # Provide correct Hebrew month display name
123
+ def heb_month_display_name(month)
124
+ return HEB_MONTH_NAMES[month]
125
+ end
126
+
127
+ def convert_from_julian
128
+ dateArray = jd_to_hebrew(@jd)
129
+ @hy = dateArray[0]
130
+ @hm = dateArray[1]
131
+ @hd = dateArray[2]
132
+ end
133
+
134
+ def convert_from_hebrew
135
+ @jd = to_jd(@hy, @hm, @hd).jd
136
+ end
137
+
138
+ # Is a given Hebrew year a leap year ?
139
+ def leap?(year)
140
+ return ((year * 7) + 1).modulo(19) < 7
141
+ end
142
+
143
+ # How many months are there in a Hebrew year (12 = normal, 13 = leap)
144
+ def year_months(year)
145
+ return leap?(year) ? 13 : 12
146
+ end
147
+
148
+ # How many days are in a Hebrew year ?
149
+ def year_days(year)
150
+ return to_jd(year + 1, 7, 1) - to_jd(year, 7, 1)
151
+ end
152
+
153
+ # How many days are in a given month of a given year
154
+ def month_days(year, month)
155
+ # First of all, dispose of fixed-length 29 day months
156
+ case
157
+ when (month == 2 || month == 4 || month == 6 || month == 10 || month == 13)
158
+ return 29
159
+
160
+ # If it's not a leap year, Adar has 29 days
161
+ when (month == 12 && !leap?(year)) then
162
+ return 29
163
+
164
+ # If it's Cheshvan, days depend on length of year
165
+ when month == 8 && year_days(year).modulo(10) != 5 then
166
+ return 29
167
+
168
+ # Similarly, Kislev varies with the length of year
169
+ when (month == 9 && (year_days(year).modulo(10) == 3)) then
170
+ return 29
171
+
172
+ # Nope, it's a 30 day month
173
+ else
174
+ return 30
175
+ end
176
+ end
177
+
178
+ private
179
+ # Test for delay of start of new year and to avoid
180
+ # Sunday, Wednesday, and Friday as start of the new year.
181
+ def delay_1(year)
182
+ months = (((235 * year) - 234) / 19).floor
183
+ parts = 12084 + (13753 * months)
184
+ day = (months * 29) + (parts / 25920).floor
185
+
186
+ if ((3 * (day + 1)).modulo(7) < 3)
187
+ day += 1
188
+ end
189
+ return day
190
+ end
191
+
192
+ # Check for delay in start of new year due to length of adjacent years
193
+ def delay_2(year)
194
+ last = delay_1(year - 1)
195
+ present = delay_1(year)
196
+ nextone = delay_1(year + 1)
197
+
198
+ return ((nextone - present) == 356) ? 2 : (((present - last) == 382) ? 1 : 0)
199
+ end
200
+
201
+ # Convert hebrew date to julian date
202
+ def to_jd(year, month, day)
203
+ months = year_months(year)
204
+ jd = HEBREW_EPOCH + delay_1(year) + delay_2(year) + day + 1
205
+
206
+ if (month < 7) then
207
+ for mon in 7..months
208
+ jd += month_days(year, mon)
209
+ end
210
+
211
+ for mon in 1..(month - 1)
212
+ jd += month_days(year, mon)
213
+ end
214
+ else
215
+ for mon in 7..(month - 1)
216
+ jd += month_days(year, mon)
217
+ end
218
+ end
219
+
220
+ return Object::Date.new1(jd)
221
+ end
222
+
223
+ # Convert Julian date to Hebrew date
224
+ # This works by making multiple calls to
225
+ # to_jd, and is this very slow
226
+ def jd_to_hebrew(jd)
227
+ myjd = jd.jd
228
+ count = (((myjd - HEBREW_EPOCH) * 98496.0) / 35975351.0).floor
229
+ year = count - 1
230
+
231
+ i = count
232
+ while myjd >= to_jd(i, 7, 1).jd
233
+ year += 1
234
+ i += 1
235
+ end
236
+
237
+ first = (myjd < to_jd(year, 1, 1).jd) ? 7 : 1
238
+ i = month = first
239
+
240
+ while myjd > to_jd(year, i, month_days(year, i)).jd
241
+ month += 1
242
+ i += 1
243
+ end
244
+
245
+ day = (myjd - to_jd(year, month, 1).jd) # + 1
246
+ return [year, month, day]
247
+ end
248
+
249
+ end # class
250
+
251
+ end # module
@@ -0,0 +1,152 @@
1
+ # Unit tests for Hebruby module to convert julian dates to hebrew dates, and vice-versa
2
+ # Written by Ron Evans
3
+ # Additional code contributed by Joshua Harvey
4
+ # Based on Javascript code from John Walker (http://www.fourmilab.ch/documents/calendar/)
5
+ require 'test/unit'
6
+ require 'hebruby'
7
+ require 'date'
8
+
9
+ class TC_MyTest2 < Test::Unit::TestCase
10
+ # def setup
11
+ # end
12
+
13
+ # def teardown
14
+ # end
15
+
16
+ #
17
+ # hebrew to julian tests
18
+ #
19
+
20
+ def test_h2j_1
21
+ @hb = Hebruby::HebrewDate.new(Date.new(2010, 1, 1))
22
+ assert_equal(10, @hb.month, "Wrong month.")
23
+ assert_equal("Tevet", @hb.month_name, "Wrong month name.")
24
+ assert_equal(5770, @hb.year, "Wrong year.")
25
+ assert_equal(15, @hb.day, "Wrong day.")
26
+ end
27
+
28
+ def test_h2j_2
29
+ @hb = Hebruby::HebrewDate.new(Date.new(2005, 1, 15))
30
+ assert_equal(11, @hb.month, "Wrong month.")
31
+ assert_equal("Shvat", @hb.month_name, "Wrong month name.")
32
+ assert_equal(5765, @hb.year, "Wrong year.")
33
+ assert_equal(5, @hb.day, "Wrong day.")
34
+ end
35
+
36
+ def test_h2j_3
37
+ @hb = Hebruby::HebrewDate.new(Date.new(2005, 4, 10))
38
+ assert_equal(1, @hb.month, "Wrong month.")
39
+ assert_equal("Nissan", @hb.month_name, "Wrong month name.")
40
+ assert_equal(5765, @hb.year, "Wrong year.")
41
+ assert_equal(1, @hb.day, "Wrong day.")
42
+ end
43
+
44
+ def test_h2j_4
45
+ @hb = Hebruby::HebrewDate.new(Date.new(1966, 4, 10))
46
+ assert_equal(1, @hb.month, "Wrong month.")
47
+ assert_equal("Nissan", @hb.month_name, "Wrong month name.")
48
+ assert_equal(5726, @hb.year, "Wrong year.")
49
+ assert_equal(20, @hb.day, "Wrong day.")
50
+ end
51
+
52
+ def test_h2j_5
53
+ @hb = Hebruby::HebrewDate.new(Date.new(1998, 12, 22))
54
+ assert_equal(10, @hb.month, "Wrong month.")
55
+ assert_equal("Tevet", @hb.month_name, "Wrong month name.")
56
+ assert_equal(5759, @hb.year, "Wrong year.")
57
+ assert_equal(3, @hb.day, "Wrong day.")
58
+ end
59
+
60
+ def test_h2j_6
61
+ @hb = Hebruby::HebrewDate.new(Date.new(1968, 6, 28))
62
+ assert_equal(4, @hb.month, "Wrong month.")
63
+ assert_equal("Tamuz", @hb.month_name, "Wrong month name.")
64
+ assert_equal(5728, @hb.year, "Wrong year.")
65
+ assert_equal(2, @hb.day, "Wrong day.")
66
+ end
67
+
68
+ def test_h2j_7
69
+ @hb = Hebruby::HebrewDate.new(Date.new(1941, 12, 10))
70
+ assert_equal(9, @hb.month, "Wrong month.")
71
+ assert_equal("Kislev", @hb.month_name, "Wrong month name.")
72
+ assert_equal(5702, @hb.year, "Wrong year.")
73
+ assert_equal(20, @hb.day, "Wrong day.")
74
+ end
75
+
76
+ #
77
+ # julian to hebrew tests
78
+ #
79
+
80
+ def test_j2h_1
81
+ @hb = Hebruby::HebrewDate.new1(15,10,5770)
82
+ assert_equal(15, @hb.day, "Wrong day.")
83
+ assert_equal(10, @hb.month, "Wrong month.")
84
+ assert_equal("Tevet", @hb.month_name, "Wrong month name.")
85
+ assert_equal(5770, @hb.year, "Wrong year.")
86
+ assert_equal('התש"ע', @hb.heb_year_name, "Wrong year.")
87
+ assert_equal(Date.new(2010, 1, 1).jd, @hb.jd, "Wrong Julian date.")
88
+ end
89
+
90
+ def test_j2h_2
91
+ @hb = Hebruby::HebrewDate.new1(5,11,5765)
92
+ assert_equal(5, @hb.day, "Wrong day.")
93
+ assert_equal(11, @hb.month, "Wrong month.")
94
+ assert_equal("Shvat", @hb.month_name, "Wrong month name.")
95
+ assert_equal(5765, @hb.year, "Wrong year.")
96
+ assert_equal(Date.new(2005, 1, 15).jd, @hb.jd, "Wrong Julian date.")
97
+ end
98
+
99
+ def test_j2h_3
100
+ @hb = Hebruby::HebrewDate.new1(1,1,5765)
101
+ assert_equal(1, @hb.day, "Wrong day.")
102
+ assert_equal(1, @hb.month, "Wrong month.")
103
+ assert_equal("Nissan", @hb.month_name, "Wrong month name.")
104
+ assert_equal(5765, @hb.year, "Wrong year.")
105
+ assert_equal(Date.new(2005, 4, 10).jd, @hb.jd, "Wrong Julian date.")
106
+ end
107
+
108
+ def test_j2h_4
109
+ @hb = Hebruby::HebrewDate.new1(20,1,5726)
110
+ assert_equal(20, @hb.day, "Wrong day.")
111
+ assert_equal(1, @hb.month, "Wrong month.")
112
+ assert_equal(5726, @hb.year, "Wrong year.")
113
+ assert_equal("Nissan", @hb.month_name, "Wrong month name.")
114
+ assert_equal(Date.new(1966, 4, 10).jd, @hb.jd, "Wrong Julian date.")
115
+ end
116
+
117
+ def test_j2h_5
118
+ @hb = Hebruby::HebrewDate.new1(3,10,5759)
119
+ assert_equal(3, @hb.day, "Wrong day.")
120
+ assert_equal(10, @hb.month, "Wrong month.")
121
+ assert_equal("Tevet", @hb.month_name, "Wrong month name.")
122
+ assert_equal(5759, @hb.year, "Wrong year.")
123
+ assert_equal(Date.new(1998, 12, 22).jd, @hb.jd, "Wrong Julian date.")
124
+ assert_equal(%q{ג' בטבת התשנ"ט}, @hb.heb_date)
125
+ end
126
+
127
+ def test_j2h_6
128
+ @hb = Hebruby::HebrewDate.new1(2,4,5728)
129
+ assert_equal(2, @hb.day, "Wrong day.")
130
+ assert_equal(4, @hb.month, "Wrong month.")
131
+ assert_equal("Tamuz", @hb.month_name, "Wrong month name.")
132
+ assert_equal("תמוז", @hb.heb_month_name, "Wrong month name.")
133
+ assert_equal(5728, @hb.year, "Wrong year.")
134
+ assert_equal('התשכ"ח', @hb.heb_year_name, "Wrong year.")
135
+ assert_equal(Date.new(1968, 6, 28).jd, @hb.jd, "Wrong Julian date.")
136
+ end
137
+
138
+ def test_j2h_7
139
+ @hb = Hebruby::HebrewDate.new1(20,9,5702)
140
+ assert_equal(20, @hb.day, "Wrong day.")
141
+ assert_equal(9, @hb.month, "Wrong month.")
142
+ assert_equal("Kislev", @hb.month_name, "Wrong month name.")
143
+ assert_equal("כסלו", @hb.heb_month_name, "Wrong month name.")
144
+ assert_equal(5702, @hb.year, "Wrong year.")
145
+ assert_equal('התש"ב', @hb.heb_year_name, "Wrong year.")
146
+ assert_equal(Date.new(1941, 12, 10).jd, @hb.jd, "Wrong Julian date.")
147
+ end
148
+
149
+
150
+
151
+ end
152
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: Hebruby
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.2.0
7
+ date: 2006-11-25 00:00:00 -08:00
8
+ summary: Hebruby is a Ruby library to convert julian dates to hebrew dates, and vice-versa.
9
+ require_paths:
10
+ - lib
11
+ email: ron dot evans at gmail dot com
12
+ homepage: http://www.deadprogrammersociety.com
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: hebruby
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Ron Evans
31
+ files:
32
+ - README
33
+ - lib/hebruby.rb
34
+ - test/hebruby_tests.rb
35
+ test_files:
36
+ - test/hebruby_tests.rb
37
+ rdoc_options: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ requirements: []
46
+
47
+ dependencies: []
48
+