hijri 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d85b27d3b44ff6c7d07d0c694b00e0ff7aa8b9c
4
- data.tar.gz: 82fb98fc862f70e0d04477e12f74a77408979500
3
+ metadata.gz: 43396ddd0542fcf4246247863d340194bbf09f52
4
+ data.tar.gz: 7ef4ea0f4d761bdd723f0cf9cbeec3154249a2a3
5
5
  SHA512:
6
- metadata.gz: cb74e3acfc640014bbd3499a74122f8b01d753fac32942015d4d3928f5dcde7133fdc8c34bfb6212c2fea76aa901b884f4132a80c1c1e2d735548925bfcc358f
7
- data.tar.gz: 91fc784276f15e1a6b0e39adedf1830a15ee1fdf338a4cdd2337f747e52567eaea8d7440811d72a078ca88f3dc77a164f59286a7e0935aae1a4e12e3f2f99f70
6
+ metadata.gz: 3d027e1a118336127fa816ad25feda65fccc622b1449088cb8b7d378b80b994714fdf2fce28f5be9189231324351c8fffa6037a8035d23e794b9f32c45ae4e62
7
+ data.tar.gz: 53e4700a6e0615180881bef35d1af164158fb678cb4b26ec10193e214b09b28b6d62ce97217e62f25ac2fc02bb604c0d2d906e9c0fe9fc58bf7632800194c98b
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.5.0 / 2015-02-26
2
+
3
+ * Add Hijri::Date.change
4
+ * Add Hijri::DateTime.change
5
+ * Raise ArgumentError if one of arguments not correct.
6
+
1
7
  === 0.4.0 / 2015-01-11
2
8
 
3
9
  * Add Hijri::DateTime.to_s
data/README.md CHANGED
@@ -50,16 +50,16 @@ h = Date.today.to_hijri # => #<Hijri::Date:0x007f875e8c84e8 @year=1436, @month=2
50
50
  # or you can initialize new one.
51
51
  hijri = Hijri::Date.new 1430, 1, 3 # => #<Hijri::Date:0x007f875e8dbb38 @year=1430, @month=1, @day=3>
52
52
 
53
- # or you get today hijri date directly.
53
+ # or you can get today hijri date directly.
54
54
  toady = Hijri::Date.today # => #<Hijri::Date:0x007f875e8d8410 @year=1436, @month=2, @day=16>
55
55
 
56
- # and you can convert hijri date to greogian date.
56
+ # and you can convert hijri date to greogian date also.
57
57
  hijri.to_greo # => #<Date: 2009-01-01 ((2454833j,0s,0n),+0s,2299161j)>
58
58
 
59
59
  # and there is DateTime too
60
60
  date_and_time = Hijri::DateTime.now # => #<Hijri::DateTime:0x007f875e8eac00 @year=1436, @month=2, @day=16, @hour=14, @minute=14, @second=39, @zone="+03:00">
61
61
 
62
- # hijri support strftime method with the same option as Greogian date format
62
+ # hijri support strftime method with the same options as Greogian date format method
63
63
  Hijri::DateTime.now.strftime('%c') # => "Ahad Rabia I 20 16:00:25 1436"
64
64
  ```
65
65
 
@@ -72,7 +72,9 @@ Hijri::DateTime.now.strftime('%c') # => "Ahad Rabia I 20 16:00:25 1436"
72
72
  - [x] Add Hijri::Date.today to create Hijri::Date object with today date.
73
73
  - [x] Add Comparable for Hijri::Date.
74
74
  - [x] Implement strftime method.
75
- - [ ] Add Hijri::Date.change method.
75
+ - [x] Add Hijri::Date.change method.
76
+ - [x] Raise ArgumentError when one of arguments has an invalid date value.
77
+ - [ ] Implement Hijri::Date.strptime method.
76
78
 
77
79
 
78
80
  ## Contributing
@@ -4,6 +4,14 @@ module Hijri
4
4
  ISLAMIC_EPOCH = 227014
5
5
  AVERAGE_YEARS_DAYS = 354.367056
6
6
  AVERAGE_MONTH_DAYS = 29.530588
7
+
8
+ INFINITY = +1.0/0.0
9
+
10
+ if RUBY_VERSION < '1.8.8'
11
+ class ::Range
12
+ alias :cover? :include?
13
+ end
14
+ end
7
15
  end
8
16
 
9
17
  require 'hijri/version'
@@ -5,10 +5,8 @@ module Hijri
5
5
 
6
6
  attr_reader :day, :month, :year
7
7
 
8
- # TODO change mon to month in format.rb.
9
8
  alias :mon :month
10
9
  alias :mday :day
11
-
12
10
 
13
11
  MONTHNAMES = [nil] + %w(Muharram Safar Rabia-Awwal Rabia-Thani Jumaada-Awal Jumaada-Thani Rajab Sha'ban Ramadan Shawwal Dhul-Qi'dah Dhul-Hijjah)
14
12
  DAYNAMES = %w(as-Sabt al-Ahad al-Ithnayn ath-Thalaathaa al-Arba'aa' al-Khamis al-Jumu'ah)
@@ -36,20 +34,23 @@ module Hijri
36
34
  end
37
35
 
38
36
  def initialize(year=1, month=1, day=1)
39
- @year, @month, @day = year, month, day
40
- end
41
-
42
- def islamic_leap_year?
43
- return (((((11 * self.year) + 14) % 30) < 11) ? true : false)
37
+ if valid_date?(year, month, day)
38
+ @year, @month, @day = year, month, day
39
+ else
40
+ raise ArgumentError, "Invalid Date"
41
+ end
44
42
  end
45
-
46
- def last_day_of_islamic_month
47
- # Last day in month during year on the Islamic calendar.
48
- return ((self.month % 2 == 1) || (self.month == 12 && islamic_leap_year?) ? 30 : 29)
43
+
44
+ def change(kargs)
45
+ # Remove nil values
46
+ kargs.reject!{|k,v| v.nil?}
47
+ @year = kargs.fetch :year , year
48
+ @month = kargs.fetch :month, month
49
+ @day = kargs.fetch :day , day
49
50
  end
50
51
 
51
52
  def to_s
52
- "#{@year}-#{sprintf('%02d', @month)}-#{sprintf('%02d', @day)}"
53
+ format('%.4d-%02d-%02d', year, month, day)
53
54
  end
54
55
 
55
56
  def <=>(date)
@@ -57,7 +58,9 @@ module Hijri
57
58
  date = date.to_hijri
58
59
  if self.to_s == date.to_s
59
60
  return 0
60
- elsif @year > date.year || (@year == date.year && @month > date.month) || (@year == date.year && @month == date.month && @day > date.day)
61
+ elsif @year > date.year ||
62
+ (@year == date.year && @month > date.month) ||
63
+ (@year == date.year && @month == date.month && @day > date.day)
61
64
  return 1
62
65
  else
63
66
  return -1
@@ -82,6 +85,13 @@ module Hijri
82
85
  def to_hijri
83
86
  self
84
87
  end
88
+
89
+ def valid_date?(year, month, day)
90
+ return false unless (1..INFINITY).cover?(year)
91
+ return false unless (1..12).cover?(month)
92
+ return false unless (1..30).cover?(day)
93
+ return true
94
+ end
85
95
 
86
96
  def yday
87
97
  (((month - 1) * AVERAGE_MONTH_DAYS) + day).floor
@@ -1,35 +1,47 @@
1
1
  module Hijri
2
2
  class DateTime < Date
3
-
3
+
4
4
  attr_reader :hour, :minute, :second, :offset, :zone
5
5
 
6
6
  alias :min :minute
7
7
  alias :sec :second
8
-
9
8
 
10
9
  def initialize(year=1, month=1, day=1, hour=0, minute=0, second=0, zone="00:00")
11
10
  super(year, month, day)
12
- @hour = hour
13
- @minute = minute
14
- @second = second
15
- @offset = zone.to_f / 24
16
- @zone = zone
11
+ if valid_time?(hour, minute, second, zone)
12
+ @hour = hour
13
+ @minute = minute
14
+ @second = second
15
+ @offset = zone.to_f / 24
16
+ @zone = zone
17
+ else
18
+ raise ArgumentError, "Invalid Date"
19
+ end
17
20
  end
18
-
21
+
19
22
  def change(kargs)
23
+ super(kargs)
20
24
  @hour = kargs.fetch :hour, hour
21
25
  @minute = kargs.fetch :minute, minute
22
26
  @second = kargs.fetch :second, second
23
27
  @zone = kargs.fetch :zone, zone
24
28
  end
25
-
29
+
26
30
  def to_greo
27
31
  ::DateTime.new *Converter.hijri_to_greo(self)
28
32
  end
29
-
33
+
30
34
  def to_s
31
35
  zone_str = (@zone == '00:00' ? "+#{@zone}" : @zone)
32
- "#{super}T#{sprintf('%02d', @hour)}:#{sprintf('%02d', @minute)}:#{sprintf('%02d', @second)}#{zone_str}"
36
+ format('%.4d-%02d-%02dT%02d:%02d:%02d%s',
37
+ year, mon, mday, hour, min, sec, zone_str)
38
+ end
39
+
40
+ def valid_time?(hour, minute, second, zone)
41
+ return false unless (0..23).cover?(hour)
42
+ return false unless (0..59).cover?(minute)
43
+ return false unless (0..59).cover?(second)
44
+ return true
33
45
  end
34
46
 
35
47
  class << self
@@ -1,3 +1,3 @@
1
1
  module Hijri
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -90,8 +90,8 @@ class TestHijri < MiniTest::Unit::TestCase
90
90
  end
91
91
 
92
92
  def test_hijri_date_strftime_with_no_input
93
- date = Hijri::Date.new 1433, 9, 18
94
- assert_equal "1433-09-18", date.strftime
93
+ date = Hijri::Date.new 1433, 12, 18
94
+ assert_equal "1433-12-18", date.strftime
95
95
  end
96
96
 
97
97
  def test_hijri_datetime_strftime_with_no_input
@@ -131,10 +131,48 @@ class TestHijri < MiniTest::Unit::TestCase
131
131
 
132
132
  def test_hijri_week_number_of_the_year
133
133
  date = Hijri::Date.new 1436, 3, 20
134
- assert_equal("11", date.strftime('%U'))
135
- assert_equal("11", date.strftime('%W'))
134
+ assert_equal "11", date.strftime('%U')
135
+ assert_equal "11", date.strftime('%W')
136
+ end
137
+
138
+ def test_date_change
139
+ date = Hijri::Date.new 1436, 3, 20
140
+ date_to_be = Hijri::Date.new 1435, 4, 10
141
+
142
+ date.change :year => 1435, :month => 4, :day => 10
143
+ assert_equal date_to_be, date
144
+ end
145
+
146
+ def test_date_change_with_nil_arguments
147
+ date = Hijri::Date.new 1436, 3, 20
148
+ date_to_be = Hijri::Date.new 1435, 4, 20
149
+
150
+ date.change :year => 1435, :month => 4, :day => nil
151
+ assert_equal date_to_be, date
136
152
  end
137
153
 
138
- # TODO add Hijri::Date.change and test it.
154
+ def test_datetime_change
155
+ datetime = Hijri::DateTime.new(1436, 3, 20, 10, 15, 30)
156
+ datetime_to_be = Hijri::DateTime.new(1435, 4, 10, 9, 30, 0)
157
+
158
+ datetime.change :year => 1435, :month => 4, :day => 10, :hour => 9, :minute => 30, :second => 0
159
+ assert_equal datetime_to_be, datetime
160
+ end
161
+
162
+ def test_datetime_change_with_nil_arguments
163
+ datetime = Hijri::DateTime.new(1436, 3, 20, 10, 15, 30)
164
+ datetime_to_be = Hijri::DateTime.new(1435, 4, 10, 9, 30, 30)
165
+
166
+ datetime.change :year => 1435, :month => 4, :day => 10, :hour => 9, :minute => 30, :second => nil
167
+ assert_equal datetime_to_be, datetime
168
+ end
169
+
170
+ def test_initilize_date_with_invalid_arguments
171
+ assert_raises(ArgumentError) { Hijri::Date.new(1430, 30, 10) }
172
+ end
173
+
174
+ def test_initilize_datetime_with_invalid_arguments
175
+ assert_raises(ArgumentError) { Hijri::DateTime.new(1430, 12, 10, 30, 70) }
176
+ end
139
177
 
140
178
  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.4.0
4
+ version: 0.5.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: 2015-01-11 00:00:00.000000000 Z
11
+ date: 2015-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler