hijri 0.0.1
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 +7 -0
- data/.document +3 -0
- data/.gitignore +4 -0
- data/.yardopts +1 -0
- data/CHANGELOG +4 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +49 -0
- data/Rakefile +16 -0
- data/hijri.gemspec +24 -0
- data/lib/format.rb +1313 -0
- data/lib/hijri.rb +9 -0
- data/lib/hijri/absolute.rb +72 -0
- data/lib/hijri/converter.rb +101 -0
- data/lib/hijri/date.rb +22 -0
- data/lib/hijri/hijri.rb +45 -0
- data/lib/hijri/version.rb +3 -0
- data/test/helper.rb +24 -0
- data/test/test_hijri.rb +31 -0
- metadata +120 -0
data/lib/hijri.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
module Hijri
|
2
|
+
class Absolute
|
3
|
+
attr_accessor :absolute
|
4
|
+
|
5
|
+
def initialize abs
|
6
|
+
self.absolute = abs
|
7
|
+
end
|
8
|
+
|
9
|
+
# def greo_to_hijri(date)
|
10
|
+
# hijri = absolute2islamic(gregorian2absolute(date.day, date.month, date.year))
|
11
|
+
# Hijri.new hijri[0], hijri[1], hijri[2]
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# def hijri_to_greo(date)
|
15
|
+
# greo = absolute2gregorian(islamic2absolute(date.day,date.month,date.year))
|
16
|
+
# Date.new greo[0], greo[1], greo[2]
|
17
|
+
# end
|
18
|
+
|
19
|
+
def gregorian2absolute(day, month, year)
|
20
|
+
# # Computes the absolute date from the Gregorian date.
|
21
|
+
@d = day
|
22
|
+
@m = month - 1
|
23
|
+
@m.downto(1) do |m|
|
24
|
+
@d += last_day_of_gregorian_month(@m, year)
|
25
|
+
end
|
26
|
+
return (@d + 365 * (year - 1) + (year -1) / 4.0 - (year - 1) / 100.0 + (year - 1) / 400.0).to_i
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_greo
|
30
|
+
# Computes the Gregorian date from the absolute date.
|
31
|
+
# Search forward year by year from approximate year
|
32
|
+
puts self.absolute
|
33
|
+
year = (self.absolute / 366.0 + 0.5).to_i
|
34
|
+
while (self.absolute >= gregorian2absolute(1, 1, year + 1))
|
35
|
+
year += 1
|
36
|
+
end
|
37
|
+
# Search forward month by month from January
|
38
|
+
month = 1
|
39
|
+
while (self.absolute > gregorian2absolute(last_day_of_gregorian_month(month, year), month, year))
|
40
|
+
month += 1
|
41
|
+
end
|
42
|
+
day = self.absolute - gregorian2absolute(1, month, year) + 1
|
43
|
+
return [year, month, day]
|
44
|
+
end
|
45
|
+
|
46
|
+
# TODO
|
47
|
+
def to_hijri
|
48
|
+
# Computes the Islamic date from the absolute date.
|
49
|
+
# puts "abs #{abs} and islamicEpoch #{IslamicEpoch}"
|
50
|
+
if (absolute <= Hijri::ISLAMIC_EPOCH)
|
51
|
+
# Date is pre-Islamic
|
52
|
+
month = 0
|
53
|
+
day = 0
|
54
|
+
year = 0
|
55
|
+
elsif
|
56
|
+
# Search forward year by year from approximate year
|
57
|
+
year = ((absolute - Hijri::ISLAMIC_EPOCH) / 355.0).to_i
|
58
|
+
while (absolute >= islamic2absolute(1,1,year+1))
|
59
|
+
year += 1
|
60
|
+
end
|
61
|
+
# Search forward month by month from Muharram
|
62
|
+
month = 1
|
63
|
+
while (absolute > islamic2absolute(last_day_of_islamic_month(month,year), month, year))
|
64
|
+
month += 1
|
65
|
+
end
|
66
|
+
day = absolute - islamic2absolute(1, month, year) + 1
|
67
|
+
end
|
68
|
+
return [year, month, day]
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Hijri
|
2
|
+
module Converter
|
3
|
+
def self.hijri_to_greo hijri
|
4
|
+
absolute_to_greo(hijri_to_absolute(hijri.year, hijri.month, hijri.day))
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.greo_to_hijri greg
|
8
|
+
absolute_to_hijri(greo_to_absolute(greg.year, greg.month, greg.day))
|
9
|
+
end
|
10
|
+
|
11
|
+
module_function
|
12
|
+
|
13
|
+
# Hijri Methods
|
14
|
+
def islamic_leap_year?(year)
|
15
|
+
return (((((11 * year) + 14) % 30) < 11) ? true : false)
|
16
|
+
end
|
17
|
+
|
18
|
+
def last_day_of_islamic_month(month, year)
|
19
|
+
# Last day in month during year on the Islamic calendar.
|
20
|
+
return ((month % 2 == 1) || (month == 12 && islamic_leap_year?(year)) ? 30 : 29)
|
21
|
+
end
|
22
|
+
|
23
|
+
def hijri_to_absolute(year, month, day)
|
24
|
+
month_days = 29 * (month - 1) # days on this year
|
25
|
+
nonleap_year_days = 354 * (year - 1)
|
26
|
+
leap_year_days = (3 + (11 * year)) / 30.0
|
27
|
+
this_year = (month / 2.0).to_i
|
28
|
+
|
29
|
+
return (day + month_days + this_year + nonleap_year_days + leap_year_days + ISLAMIC_EPOCH).to_i
|
30
|
+
end
|
31
|
+
|
32
|
+
# Gregorian Methods
|
33
|
+
def last_day_of_gregorian_month(month, year)
|
34
|
+
# Compute the last date of the month for the Gregorian calendar.
|
35
|
+
if month == 2
|
36
|
+
return 29 if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
|
37
|
+
end
|
38
|
+
return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]
|
39
|
+
end
|
40
|
+
|
41
|
+
# Absolute Methods
|
42
|
+
def greo_to_absolute(year, month, day)
|
43
|
+
# Computes the absolute date from the Gregorian date.
|
44
|
+
d = day
|
45
|
+
(month - 1).downto(1) do |m|
|
46
|
+
d += last_day_of_gregorian_month(m, year)
|
47
|
+
end
|
48
|
+
|
49
|
+
return (d + # days this year
|
50
|
+
365 * (year - 1) + # days in previous years ignoring leap days
|
51
|
+
(year - 1) / 4.0 - # Julian leap days before this year...
|
52
|
+
(year - 1) / 100.0 + # ...minus prior century years...
|
53
|
+
(year - 1) / 400.0 # ...plus prior years divisible by 400
|
54
|
+
).to_i
|
55
|
+
end
|
56
|
+
|
57
|
+
def absolute_to_greo(abs)
|
58
|
+
# Computes the Gregorian date from the absolute date.
|
59
|
+
# Search forward year by year from approximate year
|
60
|
+
year = (abs / (366.0 + 0.5)).to_i
|
61
|
+
while abs >= greo_to_absolute(year + 1, 1, 1)
|
62
|
+
year += 1
|
63
|
+
end
|
64
|
+
# Search forward month by month from January
|
65
|
+
month = 1
|
66
|
+
while abs > greo_to_absolute(year, month, last_day_of_gregorian_month(month, year))
|
67
|
+
month += 1
|
68
|
+
end
|
69
|
+
day = abs - greo_to_absolute(year, month, 1) + 1
|
70
|
+
|
71
|
+
return [year, month, day]
|
72
|
+
end
|
73
|
+
|
74
|
+
def absolute_to_hijri(abs)
|
75
|
+
# Computes the Islamic date from the absolute date.
|
76
|
+
if abs <= ISLAMIC_EPOCH
|
77
|
+
# Date is pre-Islamic
|
78
|
+
month = 0
|
79
|
+
day = 0
|
80
|
+
year = 0
|
81
|
+
elsif
|
82
|
+
|
83
|
+
# Search forward year by year from approximate year
|
84
|
+
year = ((abs - ISLAMIC_EPOCH) / 355.0).to_i
|
85
|
+
while abs >= hijri_to_absolute(year+1, 1, 1)
|
86
|
+
year += 1
|
87
|
+
end
|
88
|
+
|
89
|
+
# Search forward month by month from Muharram
|
90
|
+
month = 1
|
91
|
+
while abs > hijri_to_absolute(year, month, last_day_of_islamic_month(month, year))
|
92
|
+
month += 1
|
93
|
+
end
|
94
|
+
|
95
|
+
day = abs - hijri_to_absolute(year, month, 1) - 1
|
96
|
+
end
|
97
|
+
|
98
|
+
return [year, month, day]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/lib/hijri/date.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class Date
|
2
|
+
def to_hijri
|
3
|
+
Hijri::Hijri.new *Hijri::Converter.greo_to_hijri(self)
|
4
|
+
end
|
5
|
+
|
6
|
+
def last_day_of_gregorian_month(month)
|
7
|
+
# Compute the last date of the month for the Gregorian calendar.
|
8
|
+
if month == 2
|
9
|
+
return 29 if (self.year % 4 == 0 && self.year % 100 != 0) || (self.year % 400 == 0)
|
10
|
+
end
|
11
|
+
return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_abs
|
15
|
+
@d = self.day
|
16
|
+
@m = self.month - 1
|
17
|
+
@m.downto(1) do |m|
|
18
|
+
@d += last_day_of_gregorian_month(@m, year)
|
19
|
+
end
|
20
|
+
return (@d + 365 * (year - 1) + (year -1) / 4.0 - (year - 1) / 100.0 + (year - 1) / 400.0).to_i
|
21
|
+
end
|
22
|
+
end
|
data/lib/hijri/hijri.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Hijri
|
2
|
+
class Hijri
|
3
|
+
|
4
|
+
attr_accessor :day, :month, :year
|
5
|
+
MONTHNAMES_EN = %w(Muharram Safar Rabia-Awwal Rabia-Thani Jumaada-Awal Jumaada-Thani Rajab Sha'ban Ramadan Shawwal Dhul-Qi'dah Dhul-Hijjah)
|
6
|
+
DAYNAMES = %w(as-Sabt al-Ahad al-Ithnayn ath-Thalaathaa al-Arba'aa' al-Khamis al-Jumu'ah)
|
7
|
+
|
8
|
+
def initialize(year=1, month=1, day=1)
|
9
|
+
@year, @month, @day = year, month, day
|
10
|
+
end
|
11
|
+
|
12
|
+
def islamic_leap_year?
|
13
|
+
return (((((11 * self.year) + 14) % 30) < 11) ? true : false)
|
14
|
+
end
|
15
|
+
|
16
|
+
def last_day_of_islamic_month
|
17
|
+
# Last day in month during year on the Islamic calendar.
|
18
|
+
return ((self.month % 2 == 1) || (self.month == 12 && islamic_leap_year?) ? 30 : 29)
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
"#{@year}-#{sprintf('%02d', @month)}-#{sprintf('%02d', @day)}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_abs
|
26
|
+
month_days = 29 * (month - 1) # days on this year
|
27
|
+
nonleap_year_days = 354 * (year - 1)
|
28
|
+
leap_year_days = (3 + (11 * year)) / 30.0
|
29
|
+
this_year = (month / 2.0).to_i
|
30
|
+
|
31
|
+
return (day + month_days + this_year + nonleap_year_days + leap_year_days + Hijri::ISLAMIC_EPOCH).to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_greo
|
35
|
+
Date.new *Converter.hijri_to_greo(self)
|
36
|
+
end
|
37
|
+
|
38
|
+
class << self
|
39
|
+
def today
|
40
|
+
d = Date.send(:now)
|
41
|
+
# Hijri.new.greo_to_hijri(d)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler'
|
5
|
+
rescue LoadError => e
|
6
|
+
STDERR.puts e.message
|
7
|
+
STDERR.puts "Run `gem install bundler` to install Bundler."
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
Bundler.setup(:default, :development, :test)
|
13
|
+
rescue Bundler::BundlerError => e
|
14
|
+
STDERR.puts e.message
|
15
|
+
STDERR.puts "Run `bundle install` to install missing gems."
|
16
|
+
exit e.status_code
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'minitest/unit'
|
20
|
+
|
21
|
+
class MiniTest::Unit::TestCase
|
22
|
+
end
|
23
|
+
|
24
|
+
MiniTest::Unit.autorun
|
data/test/test_hijri.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'hijri'
|
3
|
+
|
4
|
+
class TestHijri < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def test_version
|
7
|
+
version = Hijri.const_get('VERSION')
|
8
|
+
|
9
|
+
assert(!version.empty?, 'should have a VERSION constant')
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_hijri_to_string
|
13
|
+
date = Hijri::Hijri.new 1433, 9, 18
|
14
|
+
assert_equal "1433-09-18", date.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_greo_date_to_hijri
|
18
|
+
date = Date.new 2012, 8, 6
|
19
|
+
assert_equal "1433-09-18", date.to_hijri.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_hijri_to_greo
|
23
|
+
h = Hijri::Hijri.new 1430, 1, 1
|
24
|
+
g = Date.new 2008, 12, 29
|
25
|
+
assert_equal(g , h.to_greo)
|
26
|
+
end
|
27
|
+
|
28
|
+
# TODO test hijri.now
|
29
|
+
# TODO test Hijri::Date
|
30
|
+
# TODO test Hijri::DateTime
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hijri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Abdulaziz AlShetwi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.8'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubygems-tasks
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.8'
|
69
|
+
description: hijri is full Islamic Hijri calendar lib for ruby.
|
70
|
+
email: ecleeld@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- ".document"
|
76
|
+
- ".gitignore"
|
77
|
+
- ".yardopts"
|
78
|
+
- CHANGELOG
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- hijri.gemspec
|
84
|
+
- lib/format.rb
|
85
|
+
- lib/hijri.rb
|
86
|
+
- lib/hijri/absolute.rb
|
87
|
+
- lib/hijri/converter.rb
|
88
|
+
- lib/hijri/date.rb
|
89
|
+
- lib/hijri/hijri.rb
|
90
|
+
- lib/hijri/version.rb
|
91
|
+
- test/helper.rb
|
92
|
+
- test/test_hijri.rb
|
93
|
+
homepage: https://rubygems.org/gems/hijri
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.2.2
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Hijri Date Library for Ruby
|
117
|
+
test_files:
|
118
|
+
- test/helper.rb
|
119
|
+
- test/test_hijri.rb
|
120
|
+
has_rdoc:
|