hijri 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a98ec623ad77ab2b54f166fdbcc38ef4eeb97f2
4
- data.tar.gz: d052e589d43a0b18880b65e4c631d46ecccd4d5a
3
+ metadata.gz: b39edec389003c4f6316089abb8d05064dc358e0
4
+ data.tar.gz: a5b1e9d6c3befcf5c844d45e16a84ba05c0882e5
5
5
  SHA512:
6
- metadata.gz: 720df8d0d28ebb2feb1236ecdf36a69a611eb8a9672bb1d4c79f8923dbcedc8433d034e9b32eef147b97168c70b9a1e268e9cfcded8fcd60f775ff56ce698da2
7
- data.tar.gz: ba4885f4f7e184715c3b03fa90c20ce5a68b76895dbc1a10fd9666592ea017d63fec6ef5c1dc30a8593c945a825a2f4cef937d5999ebdabea735d9e2abbeddb8
6
+ metadata.gz: 84c845014dfa450e82fe2e249577bf22a15f1a115c38a64591149fda133bdee8d13bd773d7c5b9ad957129d07b330978e16a4852748abc6a06d7dd5c444f3eb0
7
+ data.tar.gz: 87df415c8902d0c828ba5f5afb78d41c28b19eaeb27e097489ddb0776d86cac1f8816ef2a1d49b5a03f649a856f14e0fe95fb49141bd3bd3f2d43d76a33ac9ea
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.1.0 / 2014-12-09
2
+
3
+ * Add Hijri::DateTime class.
4
+ * Add ability to convert Hijri::DateTime to and from DateTime.
5
+ * Add Hijri::DateTime.now.
6
+
1
7
  === 0.0.5 / 2014-12-08
2
8
 
3
9
  * Clean up unwanted code.
data/README.md CHANGED
@@ -5,7 +5,7 @@ hijri is full Islamic Hijri calendar lib for ruby. The way it work is to convert
5
5
  ### Hijri Calendar (from Wikipedia)
6
6
  The Islamic calendar or Muslim calendar or Hijri calendar: is a lunar calendar consisting of 12 lunar months in a year of 354 or 355 days. It is used to date events in many Muslim countries (concurrently with the Gregorian calendar), and used by Muslims everywhere to determine the proper day on which to celebrate Islamic holy days and festivals. The first year was the year during which the emigration of the Islamic prophet Muhammad from Mecca to Medina, known as the Hijra, occurred. Each numbered year is designated either H for Hijra or AH for the Latin anno Hegirae (in the year of the Hijra).[1] A limited number of years before Hijra (BH) are used to date events related to Islam, such as the birth of Muhammad in 53 BH.[2] The current Islamic year is 1431 AH, from approximately 18 December 2009 (evening) to 6 December 2010 (evening).
7
7
 
8
- (read more in wikipedia)[http://en.wikipedia.org/wiki/Islamic_calendar]
8
+ http://en.wikipedia.org/wiki/Islamic_calendar
9
9
 
10
10
 
11
11
 
@@ -25,17 +25,28 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
- DateTime.now.to_hijri
29
- hijri = Hijri.new 1430, 1, 3
30
- hijri.to_greo
28
+ ```ruby
29
+ require 'hijri'
30
+
31
+ # you can create hijri date from stdlib Date class.
32
+ h = Date.today.to_hijri
33
+ # or you can initialize new one.
34
+ hijri = Hijri::Date.new 1430, 1, 3
35
+ # or you get today hijri date directly.
36
+ toady = Hijri::Date.today
37
+ # and you can convert hijri date to greogian date.
38
+ hijri.to_greo
39
+ # and there is DateTime too
40
+ date_and_time = Hijri::DateTime.now
41
+ ```
31
42
 
32
43
 
33
44
  ## TODO
34
45
 
35
- - [ ] Add Hijri::Date and Hijri::DateTime.
36
- - [ ] Accept test error with one day range.
37
- - [ ] Add Hijri.now to create Hijri::DateTime object.
38
- - [ ] Add Hijri.today to create Hijri::Date object.
46
+ - [x] Add Hijri::Date and Hijri::DateTime.
47
+ - [x] Accept test error with one day range.
48
+ - [x] Add Hijri::DateTime.now to create Hijri::DateTime object.
49
+ - [x] Add Hijri::Date.today to create Hijri::Date object with today date.
39
50
  - [ ] Implement strftime method.
40
51
  - [ ] Add Comparable for Hijri::Date.
41
52
 
data/lib/hijri.rb CHANGED
@@ -3,7 +3,8 @@ module Hijri
3
3
  end
4
4
 
5
5
  require 'hijri/version'
6
- require 'hijri/hijri'
7
6
  require 'hijri/date'
7
+ require 'hijri/datetime'
8
+ require 'hijri/gregorian'
8
9
  require 'hijri/converter'
9
10
 
@@ -1,11 +1,19 @@
1
1
  module Hijri
2
2
  module Converter
3
3
  def self.hijri_to_greo hijri
4
- absolute_to_greo(hijri_to_absolute(hijri.year, hijri.month, hijri.day))
4
+ results = absolute_to_greo(hijri_to_absolute(hijri.year, hijri.month, hijri.day))
5
+ if hijri.is_a? DateTime
6
+ results.push hijri.hour, hijri.minute, hijri.second, hijri.zone
7
+ end
8
+ results
5
9
  end
6
10
 
7
11
  def self.greo_to_hijri greg
8
- absolute_to_hijri(greo_to_absolute(greg.year, greg.month, greg.day))
12
+ results = absolute_to_hijri(greo_to_absolute(greg.year, greg.month, greg.day))
13
+ if greg.is_a? ::DateTime
14
+ results.push greg.hour, greg.minute, greg.second, greg.zone
15
+ end
16
+ results
9
17
  end
10
18
 
11
19
  module_function
data/lib/hijri/date.rb CHANGED
@@ -1,22 +1,46 @@
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)
1
+ module Hijri
2
+ class Date
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
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)
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
+ date = ::Date.today
41
+ date.to_hijri
42
+ end
19
43
  end
20
- return (@d + 365 * (year - 1) + (year -1) / 4.0 - (year - 1) / 100.0 + (year - 1) / 400.0).to_i
44
+
21
45
  end
22
- end
46
+ end
@@ -0,0 +1,37 @@
1
+ module Hijri
2
+ class DateTime < Date
3
+
4
+ attr_reader :hour, :minute, :second, :zone
5
+ def initialize(year=1, month=1, day=1, hour=0, minute=0, second=0, zone="00:00")
6
+ super(year, month, day)
7
+ @hour = hour
8
+ @minute = minute
9
+ @second = second
10
+ @zone = zone
11
+ end
12
+
13
+ def change(kargs)
14
+ @hour = kargs.fetch :hour, hour
15
+ @minute = kargs.fetch :minute, minute
16
+ @second = kargs.fetch :second, second
17
+ @zone = kargs.fetch :zone, zone
18
+ end
19
+
20
+ def to_greo
21
+ ::DateTime.new *Converter.hijri_to_greo(self)
22
+ end
23
+
24
+ # TODO to_s
25
+
26
+ class << self
27
+ def now
28
+ datetime = ::DateTime.now
29
+ hijri = datetime.to_hijri
30
+ hijri.change hour: datetime.hour, minute: datetime.minute, second: datetime.second
31
+ hijri
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,28 @@
1
+ class Date
2
+ def to_hijri
3
+ Hijri::Date.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
23
+
24
+ class DateTime
25
+ def to_hijri
26
+ Hijri::DateTime.new *Hijri::Converter.greo_to_hijri(self)
27
+ end
28
+ end
data/lib/hijri/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hijri
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
data/test/test_hijri.rb CHANGED
@@ -10,7 +10,7 @@ class TestHijri < MiniTest::Unit::TestCase
10
10
  end
11
11
 
12
12
  def test_hijri_to_string
13
- date = Hijri::Hijri.new 1433, 9, 18
13
+ date = Hijri::Date.new 1433, 9, 18
14
14
  assert_equal "1433-09-18", date.to_s
15
15
  end
16
16
 
@@ -24,12 +24,34 @@ class TestHijri < MiniTest::Unit::TestCase
24
24
  end
25
25
 
26
26
  def test_hijri_to_greo
27
- h = Hijri::Hijri.new 1430, 1, 1
28
- g = Date.new 2008, 12, 29
27
+ h = Hijri::Date.new 1430, 1, 1
28
+ g = Date.new 2008, 12, 29
29
29
  assert_equal(g , h.to_greo)
30
30
  end
31
31
 
32
- # TODO test hijri.now
33
- # TODO test Hijri::Date
34
- # TODO test Hijri::DateTime
32
+ def test_hijri_date_today
33
+ gdate = Date.today
34
+ hdate = Hijri::Date.today
35
+ assert_equal gdate, hdate.to_greo
36
+ end
37
+
38
+ def test_hijri_datetime_now_create_datetime_object_with_now_date_and_time
39
+ g_datetime = DateTime.now
40
+ h_datetime = Hijri::DateTime.now
41
+ exact_date = h_datetime.to_greo
42
+
43
+ # I assert it one by one because there is a different in n variable in
44
+ # DateTime and I couldn't find it.
45
+ # -#<DateTime: 2014-12-09T13:46:30+03:00 ((2457001j,38790s,467109000n),+10800s,2299161j)>
46
+ # +#<DateTime: 2014-12-09T13:46:30+03:00 ((2457001j,38790s,0n),+10800s,2299161j)>
47
+ assert_equal g_datetime.year, exact_date.year
48
+ assert_equal g_datetime.month, exact_date.month
49
+ assert_equal g_datetime.day, exact_date.day
50
+ assert_equal g_datetime.hour, exact_date.hour
51
+ assert_equal g_datetime.minute, exact_date.minute
52
+ assert_equal g_datetime.second, exact_date.second
53
+ assert_equal g_datetime.zone, exact_date.zone
54
+ end
55
+
56
+ # TODO test hijri.now
35
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hijri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdulaziz AlShetwi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-08 00:00:00.000000000 Z
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,7 +72,8 @@ files:
72
72
  - lib/hijri/absolute.rb
73
73
  - lib/hijri/converter.rb
74
74
  - lib/hijri/date.rb
75
- - lib/hijri/hijri.rb
75
+ - lib/hijri/datetime.rb
76
+ - lib/hijri/gregorian.rb
76
77
  - lib/hijri/version.rb
77
78
  - test/helper.rb
78
79
  - test/test_hijri.rb
data/lib/hijri/hijri.rb DELETED
@@ -1,45 +0,0 @@
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