hijri_date 1.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 +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/hijri_date.gemspec +23 -0
- data/lib/hijri_date/constants.rb +9 -0
- data/lib/hijri_date/month_names.rb +29 -0
- data/lib/hijri_date/version.rb +3 -0
- data/lib/hijri_date.rb +116 -0
- data/test/hijri_date_test.rb +73 -0
- metadata +85 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
!binary "U0hBMQ==":
|
|
3
|
+
metadata.gz: !binary |-
|
|
4
|
+
YTc2YTA1NGVjZTAyOTFiZDZmOThiMjNkMDJhNDgyNTRhZGM0MTk4ZA==
|
|
5
|
+
data.tar.gz: !binary |-
|
|
6
|
+
NDkxZTMxZGI0MjYxMTZhMjgwZDdkNGNiOWFjNDQyMTE0ZGIzM2VmMw==
|
|
7
|
+
!binary "U0hBNTEy":
|
|
8
|
+
metadata.gz: !binary |-
|
|
9
|
+
ODZjODI5ZmYyMGM4NzVmMjE3MWYwNmViNTE5OGFjMzgwYjU3Y2EzZTBhYTlm
|
|
10
|
+
MmExYTc1MWU1NmFiZDQyOTU3MjgyZjI4NDgwN2VjZjIzOTYzYzc3ZGIxNTk3
|
|
11
|
+
MjJmNDNjZjUyZTRlYjA2YmMyYmNkNmM5MzM3OTY5YzJlN2YzNWY=
|
|
12
|
+
data.tar.gz: !binary |-
|
|
13
|
+
ZjkwYTM3NmNmZTVkZTcwNzllZWVkY2I5YjI2Y2I5ZDRmZGFmY2NmMDNhN2Rl
|
|
14
|
+
OGNmMmM3ZTk5YzBhMDUxYTk2ZWMwMzJhZDRkYjEzODllZDU4MmFlYzVkYmVm
|
|
15
|
+
YThhMmU0NmY4M2QxZGQ1NzQ1NzFkNDc2MDE4NjMzMmVmOTAxYjA=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 Murtaza Gulamali
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# HijriDate
|
|
2
|
+
|
|
3
|
+
TODO: Write a gem description
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'hijri_date'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install hijri_date
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
TODO: Write usage instructions here
|
|
22
|
+
|
|
23
|
+
## Contributing
|
|
24
|
+
|
|
25
|
+
1. Fork it
|
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/hijri_date.gemspec
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'hijri_date/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "hijri_date"
|
|
8
|
+
spec.version = HijriDate::VERSION
|
|
9
|
+
spec.authors = ["Murtaza Gulamali"]
|
|
10
|
+
spec.email = ["mygulamali@gmail.com"]
|
|
11
|
+
spec.description = %q{Class to store Hijri dates and convert them to/from Astronomical Julian Date.}
|
|
12
|
+
spec.summary = %q{Hijri date object}
|
|
13
|
+
spec.homepage = "https://github.com/mygulamali/hijri_date"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($/)
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
|
22
|
+
spec.add_development_dependency "rake"
|
|
23
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
module HijriDate
|
|
2
|
+
# number of days in the year per month
|
|
3
|
+
DAYSINYEAR = [30, 59, 89, 118, 148, 177, 207, 236, 266, 295, 325, 355]
|
|
4
|
+
|
|
5
|
+
# number of days in 30-years per year
|
|
6
|
+
DAYSIN30YEARS = [ 354, 708, 1063, 1417, 1771, 2126, 2480, 2834, 3189, 3543,
|
|
7
|
+
3898, 4252, 4606, 4961, 5315, 5669, 6024, 6378, 6732, 7087,
|
|
8
|
+
7441, 7796, 8150, 8504, 8859, 9213, 9567, 9922, 10276, 10631]
|
|
9
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module HijriDate
|
|
2
|
+
MONTHNAMES = [nil,
|
|
3
|
+
'Moharram al-Haraam',
|
|
4
|
+
'Safar al-Muzaffar',
|
|
5
|
+
'Rabi al-Awwal',
|
|
6
|
+
'Rabi al-Aakhar',
|
|
7
|
+
'Jumada al-Ula',
|
|
8
|
+
'Jumada al-Ukhra',
|
|
9
|
+
'Rajab al-Asab',
|
|
10
|
+
'Shabaan al-Karim',
|
|
11
|
+
'Ramadaan al-Moazzam',
|
|
12
|
+
'Shawwal al-Mukarram',
|
|
13
|
+
'Zilqadah al-Haraam',
|
|
14
|
+
'Zilhaj al-Haraam']
|
|
15
|
+
|
|
16
|
+
SHORTMONTHNAMES = [nil,
|
|
17
|
+
'Moharram',
|
|
18
|
+
'Safar',
|
|
19
|
+
'Rabi I',
|
|
20
|
+
'Rabi II',
|
|
21
|
+
'Jumada I',
|
|
22
|
+
'Jumada II',
|
|
23
|
+
'Rajab',
|
|
24
|
+
'Shabaan',
|
|
25
|
+
'Ramadaan',
|
|
26
|
+
'Shawwal',
|
|
27
|
+
'Zilqadah',
|
|
28
|
+
'Zilhaj']
|
|
29
|
+
end
|
data/lib/hijri_date.rb
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require "hijri_date/version"
|
|
2
|
+
require "hijri_date/month_names"
|
|
3
|
+
require "hijri_date/constants"
|
|
4
|
+
|
|
5
|
+
module HijriDate
|
|
6
|
+
class Date
|
|
7
|
+
# create setters and getters
|
|
8
|
+
attr_accessor :year, :month, :day
|
|
9
|
+
|
|
10
|
+
# constructor
|
|
11
|
+
def initialize(year = 1432, month = 4 , day = 20)
|
|
12
|
+
@year = year
|
|
13
|
+
@month = month
|
|
14
|
+
@day = day
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# convert to string object
|
|
18
|
+
def to_s(date = self)
|
|
19
|
+
return "#{date.day} #{HijriDate::MONTHNAMES[date.month]} #{date.year}H"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# is this (or the specified) year a Kabisa year?
|
|
23
|
+
def is_kabisa?(year = self.year)
|
|
24
|
+
for i in [2, 5, 8, 10, 13, 16, 19, 21, 24, 27, 29]
|
|
25
|
+
if year % 30 == i
|
|
26
|
+
return true
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
return false
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# number of days in this (or the specified) month and year
|
|
33
|
+
def days_in_month(month = self.month, year = self.year)
|
|
34
|
+
if (month == 12 and is_kabisa?(year)) or (month % 2 == 1)
|
|
35
|
+
return 30
|
|
36
|
+
end
|
|
37
|
+
return 29
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# day of the year corresponding to this (or specified) Hijri date
|
|
41
|
+
def day_of_year(date = self)
|
|
42
|
+
if date.month == 1
|
|
43
|
+
return date.day
|
|
44
|
+
end
|
|
45
|
+
return HijriDate::DAYSINYEAR[date.month - 2] + date.day
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# return Julian Day number associated with this (or specified) Hijri date
|
|
49
|
+
def jd(date = self)
|
|
50
|
+
y30 = (date.year / 30.0).floor
|
|
51
|
+
if (date.year % 30 == 0)
|
|
52
|
+
return 1948084 + y30*10631 + day_of_year(date)
|
|
53
|
+
else
|
|
54
|
+
return 1948084 + y30*10631 + HijriDate::DAYSIN30YEARS[date.year-y30*30-1] + day_of_year(date)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# comparison operator
|
|
59
|
+
def == (date)
|
|
60
|
+
if date.is_a?(HijriDate::Date)
|
|
61
|
+
if date.year == self.year and date.month == self.month and date.day == self.day
|
|
62
|
+
return true
|
|
63
|
+
end
|
|
64
|
+
return false
|
|
65
|
+
end
|
|
66
|
+
raise TypeError, 'expected HijriDate::Date'
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# return a new HijriDate object that is n days after the current one.
|
|
70
|
+
def + (n)
|
|
71
|
+
case n
|
|
72
|
+
when Numeric; return HijriDate.jd(self.jd + n)
|
|
73
|
+
end
|
|
74
|
+
raise TypeError, 'expected numeric'
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# return a new HijriDate object that is n days before the current one.
|
|
78
|
+
def - (n)
|
|
79
|
+
case n
|
|
80
|
+
when Numeric; return HijriDate.jd(self.jd - n)
|
|
81
|
+
end
|
|
82
|
+
raise TypeError, 'expected numeric'
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# return new Hijri Date object associated with specified Julian Day number
|
|
87
|
+
def HijriDate.jd(jd = 1948084)
|
|
88
|
+
left = (jd - 1948084).to_i
|
|
89
|
+
y30 = (left / 10631.0).floor
|
|
90
|
+
left -= y30 * 10631
|
|
91
|
+
i = 0
|
|
92
|
+
|
|
93
|
+
while left > HijriDate::DAYSIN30YEARS[i]
|
|
94
|
+
i += 1
|
|
95
|
+
end
|
|
96
|
+
year = (y30 * 30.0 + i).to_i
|
|
97
|
+
|
|
98
|
+
if i>0
|
|
99
|
+
left -= HijriDate::DAYSIN30YEARS[i - 1]
|
|
100
|
+
end
|
|
101
|
+
i = 0
|
|
102
|
+
|
|
103
|
+
while left > HijriDate::DAYSINYEAR[i]
|
|
104
|
+
i += 1
|
|
105
|
+
end
|
|
106
|
+
month = (i + 1).to_i
|
|
107
|
+
|
|
108
|
+
if i>0
|
|
109
|
+
day = (left - HijriDate::DAYSINYEAR[i - 1]).to_i
|
|
110
|
+
else
|
|
111
|
+
day = left.to_i
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
return HijriDate::Date.new(year, month, day)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
require 'hijri_date'
|
|
3
|
+
require 'minitest/autorun'
|
|
4
|
+
require 'minitest/pride'
|
|
5
|
+
|
|
6
|
+
class HijriDateTest < MiniTest::Unit::TestCase
|
|
7
|
+
def setup
|
|
8
|
+
@date = HijriDate::Date.new # 20/04/1432H = 25/03/2011AD
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def test_kabisa_year
|
|
12
|
+
refute @date.is_kabisa?
|
|
13
|
+
assert @date.is_kabisa? 1431
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_days_in_month
|
|
17
|
+
assert_equal 29, @date.days_in_month
|
|
18
|
+
assert_equal 29, @date.days_in_month(12)
|
|
19
|
+
assert_equal 30, @date.days_in_month(12, 1431)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_day_of_year
|
|
23
|
+
assert_equal 109, @date.day_of_year
|
|
24
|
+
assert_equal 10, HijriDate::Date.new(1432, 1, 10).day_of_year
|
|
25
|
+
assert_equal 355, HijriDate::Date.new(1431, 12, 30).day_of_year
|
|
26
|
+
assert_equal 354, HijriDate::Date.new(1432, 12, 29).day_of_year
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_conversion_to_jd
|
|
30
|
+
assert_equal 2455646, @date.jd
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_comparison
|
|
34
|
+
assert @date == HijriDate::Date.new(@date.year, @date.month, @date.day)
|
|
35
|
+
refute @date == HijriDate::Date.new(@date.year + 1, @date.month, @date.day)
|
|
36
|
+
refute @date == HijriDate::Date.new(@date.year, @date.month + 1, @date.day)
|
|
37
|
+
refute @date == HijriDate::Date.new(@date.year, @date.month, @date.day + 1)
|
|
38
|
+
|
|
39
|
+
assert_raises TypeError do
|
|
40
|
+
@date == Date.today
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_add
|
|
45
|
+
assert_equal 21, (@date + 1).day
|
|
46
|
+
|
|
47
|
+
date = HijriDate::Date.new(1432, 12, 29) + 1
|
|
48
|
+
assert_equal HijriDate::Date.new(1433, 1, 1), date
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_subtract
|
|
52
|
+
assert_equal 19, (@date - 1).day
|
|
53
|
+
|
|
54
|
+
date = HijriDate::Date.new(1433, 1, 1) - 1
|
|
55
|
+
assert_equal HijriDate::Date.new(1432, 12, 29), date
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_conversion_from_jd
|
|
59
|
+
assert_equal @date, HijriDate.jd(2455646)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_hijri_gregorian_conversion
|
|
63
|
+
date = Date.jd(@date.jd)
|
|
64
|
+
assert_equal 2011, date.year
|
|
65
|
+
assert_equal 3, date.month
|
|
66
|
+
assert_equal 25, date.day
|
|
67
|
+
|
|
68
|
+
date = HijriDate.jd(Date.new(2011, 3, 25).jd)
|
|
69
|
+
assert_equal 1432, date.year
|
|
70
|
+
assert_equal 4, date.month
|
|
71
|
+
assert_equal 20, date.day
|
|
72
|
+
end
|
|
73
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: hijri_date
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Murtaza Gulamali
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-08-15 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.3'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.3'
|
|
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'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ! '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
description: Class to store Hijri dates and convert them to/from Astronomical Julian
|
|
42
|
+
Date.
|
|
43
|
+
email:
|
|
44
|
+
- mygulamali@gmail.com
|
|
45
|
+
executables: []
|
|
46
|
+
extensions: []
|
|
47
|
+
extra_rdoc_files: []
|
|
48
|
+
files:
|
|
49
|
+
- .gitignore
|
|
50
|
+
- Gemfile
|
|
51
|
+
- LICENSE.txt
|
|
52
|
+
- README.md
|
|
53
|
+
- Rakefile
|
|
54
|
+
- hijri_date.gemspec
|
|
55
|
+
- lib/hijri_date.rb
|
|
56
|
+
- lib/hijri_date/constants.rb
|
|
57
|
+
- lib/hijri_date/month_names.rb
|
|
58
|
+
- lib/hijri_date/version.rb
|
|
59
|
+
- test/hijri_date_test.rb
|
|
60
|
+
homepage: https://github.com/mygulamali/hijri_date
|
|
61
|
+
licenses:
|
|
62
|
+
- MIT
|
|
63
|
+
metadata: {}
|
|
64
|
+
post_install_message:
|
|
65
|
+
rdoc_options: []
|
|
66
|
+
require_paths:
|
|
67
|
+
- lib
|
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ! '>='
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '0'
|
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ! '>='
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '0'
|
|
78
|
+
requirements: []
|
|
79
|
+
rubyforge_project:
|
|
80
|
+
rubygems_version: 2.0.6
|
|
81
|
+
signing_key:
|
|
82
|
+
specification_version: 4
|
|
83
|
+
summary: Hijri date object
|
|
84
|
+
test_files:
|
|
85
|
+
- test/hijri_date_test.rb
|