datet 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/datet.gemspec +2 -2
  3. data/lib/datet.rb +79 -21
  4. data/spec/datet_spec.rb +101 -0
  5. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
data/datet.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datet}
8
- s.version = "0.0.5"
8
+ s.version = "0.0.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = %q{2012-07-14}
12
+ s.date = %q{2012-07-15}
13
13
  s.description = %q{A framework for handeling date- and time-related stuff in Ruby.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.extra_rdoc_files = [
data/lib/datet.rb CHANGED
@@ -47,6 +47,9 @@ class Datet
47
47
  #Thanks to ActiveSupport: http://rubydoc.info/docs/rails/2.3.8/ActiveSupport/CoreExtensions/Time/Calculations
48
48
  @@days_in_months = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
49
49
 
50
+ #This date is a monday. It is used to calculate up against, when we calculate 'day_in_week'.
51
+ @@def_date = Datet.new(1970, 1, 4)
52
+
50
53
  #Initializes the object. Default is the current time. A time-object can be given.
51
54
  #=Examples
52
55
  # datet = Datet.new #=> Datet-object with the current date and time.
@@ -392,9 +395,7 @@ class Datet
392
395
  # print "2005 is not a gregorian-leap year."
393
396
  # end
394
397
  def self.gregorian_leap?(y)
395
- if Date.respond_to?(:gregorian_leap?)
396
- return Date.gregorian_leap?(y)
397
- elsif y % 4 == 0 && y % 100 != 0
398
+ if y % 4 == 0 && y % 100 != 0
398
399
  return true
399
400
  elsif y % 400 == 0
400
401
  return true
@@ -418,9 +419,34 @@ class Datet
418
419
  return @@days_in_months[month]
419
420
  end
420
421
 
421
- #Returns the day in the week. Monday being 1 and sunday being 6.
422
+ #Returns the amount of days in the current year.
423
+ def days_in_year
424
+ return Datet.days_in_year(@t_year)
425
+ end
426
+
427
+ #Returns the amount of days in the given year.
428
+ def self.days_in_year(year)
429
+ return 366 if Datet.gregorian_leap?(year)
430
+ return 365
431
+ end
432
+
433
+ #Returns the day in the week. Monday being 0 and sunday being 6.
422
434
  def day_in_week
423
- diw = self.time.strftime("%w").to_i
435
+ #This is a monday - 0. Use this date to calculate up against.
436
+ def_date = Datet.new(1970, 1, 4)
437
+
438
+ if self > def_date
439
+ days = Datet.days_between(def_date, self)
440
+ factor = days.to_f / 7.0
441
+ diw = days - (factor.floor * 7)
442
+ else
443
+ days = Datet.days_between(self, def_date)
444
+ factor = days.to_f / 7.0
445
+ diw = days - (factor.floor * 7)
446
+ diw = 7 - diw
447
+ diw = 0 if diw == 7
448
+ end
449
+
424
450
  if diw == 0
425
451
  diw = 6
426
452
  else
@@ -581,17 +607,38 @@ class Datet
581
607
  end
582
608
  end
583
609
 
610
+ #Turns the given argument into a new Datet-object. Helps compare datet- to time-objects.
611
+ #===Examples
612
+ # time = Datet.arg_to_time(datet) #=> <Datet>-object
613
+ # time = Datet.arg_to_time(Datet.now) #=> <Datet>-object
614
+ def self.arg_to_datet(datet)
615
+ if datet.is_a?(Datet)
616
+ return datet
617
+ elsif datet.is_a?(Time)
618
+ return Datet.new(datet)
619
+ else
620
+ raise "Could not handle object of class: '#{datet.class.name}'."
621
+ end
622
+ end
623
+
624
+ #Make the class compareable. Also to time-objects.
584
625
  include Comparable
585
626
  def <=>(timeobj)
586
- secs = Datet.arg_to_time(timeobj).to_i
627
+ timeobj = Datet.arg_to_datet(timeobj)
587
628
 
588
- if secs > self.to_i
589
- return -1
590
- elsif secs < self.to_i
591
- return 1
592
- else
593
- return 0
629
+ tries = [:year, :month, :day, :hour, :min, :sec, :usec]
630
+ tries.each do |try|
631
+ res1 = timeobj.__send__(try)
632
+ res2 = self.__send__(try)
633
+
634
+ if res1 > res2
635
+ return -1
636
+ elsif res1 < res2
637
+ return 1
638
+ end
594
639
  end
640
+
641
+ return 0
595
642
  end
596
643
 
597
644
  #This method is used for adding values to the object based on the current set mode.
@@ -715,10 +762,10 @@ class Datet
715
762
  # datet.time #=> 2011-08-01 22:51:11 +0200
716
763
  # datet.dbstr #=> "2011-08-01 22:51:11"
717
764
  # datet.dbstr(:time => false) #=> "2011-08-01"
718
- def dbstr(args = {})
765
+ def dbstr(args = nil)
719
766
  str = "#{"%04d" % @t_year}-#{"%02d" % @t_month}-#{"%02d" % @t_day}"
720
767
 
721
- if !args.key?(:time) or args[:time]
768
+ if !args or (!args.key?(:time) or args[:time])
722
769
  str << " #{"%02d" % @t_hour}:#{"%02d" % @t_min}:#{"%02d" % @t_sec}"
723
770
  end
724
771
 
@@ -749,7 +796,7 @@ class Datet
749
796
  # Datet.new.day_str(:short => true) #=> "Mon"
750
797
  def day_str(args = nil)
751
798
  ret = Datet.days_arr[self.time.strftime("%w").to_i]
752
- if args.is_a?(Hash) and args[:short]
799
+ if args and args[:short]
753
800
  ret = ret.slice(0, 3)
754
801
  end
755
802
 
@@ -771,15 +818,26 @@ class Datet
771
818
  yot1 = t1.year
772
819
  yot2 = t2.year
773
820
 
774
- if yot1 == yot2
775
- days_between = doy2 - doy1
776
- return days_between
821
+ days = 0
822
+
823
+ #If there is a years-difference, then neutralize the first year by counting up to next year and starting from 1/1.
824
+ if yot1 < yot2
825
+ diy = Datet.days_in_year(yot1)
826
+ days += diy - doy1 + 1
827
+ doy1 = 1
828
+ yot1 += 1
777
829
  end
778
830
 
779
- upto = 365 - doy1
780
- after = doy2
831
+ #For each year calculate the amount of days in that year and add them to the count.
832
+ while yot1 < yot2
833
+ diy = Datet.days_in_year(yot1)
834
+ days += diy
835
+ yot1 += 1
836
+ end
781
837
 
782
- return upto + after
838
+ #Last calculate the difference between the two dates and add the count to that.
839
+ days_between = doy2 - doy1
840
+ return days_between + days
783
841
  end
784
842
 
785
843
  #Returns a string based on the date and time.
data/spec/datet_spec.rb CHANGED
@@ -1,5 +1,10 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ #To make it gettext-compatible.
4
+ def _(str)
5
+ return str
6
+ end
7
+
3
8
  describe "Datet" do
4
9
  it "should have the same 'to_i' as normal time" do
5
10
  time = Time.now
@@ -173,4 +178,100 @@ describe "Datet" do
173
178
  raise "Expected result: '#{right_res}' but got: '#{res}'." if res != right_res
174
179
  end
175
180
  end
181
+
182
+ it "should return the right leap years" do
183
+ tests = {
184
+ 2006 => false,
185
+ 2007 => false,
186
+ 2008 => true,
187
+ 2009 => false,
188
+ 2010 => false,
189
+ 2011 => false,
190
+ 2012 => true,
191
+ 2013 => false
192
+ }
193
+ tests.each do |year, expected_res|
194
+ res = Datet.gregorian_leap?(year)
195
+ raise "Expected #{expected_res} but got #{res}" if res != expected_res
196
+
197
+ days_in_year = Datet.days_in_year(year)
198
+ if res
199
+ exp = 366
200
+ else
201
+ exp = 365
202
+ end
203
+
204
+ raise "Expected #{exp} but got #{days_in_year}" if days_in_year != exp
205
+ end
206
+ end
207
+
208
+ it "should be able to tell the amount of days between two dates" do
209
+ tests = [
210
+ {:d1 => Datet.new(2006, 1, 1), :d2 => Datet.new(2007, 1, 5), :days_exp => 369},
211
+ {:d1 => Datet.new(2008, 1, 1), :d2 => Datet.new(2009, 1, 5), :days_exp => 370},
212
+ {:d1 => Datet.new(2006, 1, 1), :d2 => Datet.new(2009, 1, 5), :days_exp => 1100},
213
+ {:d1 => Datet.new(2000, 1, 1), :d2 => Datet.new(2010, 1, 5), :days_exp => 3657}
214
+ ]
215
+ tests.each do |data|
216
+ days_betw = Datet.days_between(data[:d1], data[:d2])
217
+ raise "Expected #{data[:days_exp]} but got #{days_betw}" if days_betw != data[:days_exp]
218
+ end
219
+ end
220
+
221
+ it "should be able to calculate the correct week days" do
222
+ tests = [
223
+ [Datet.new(1905, 1, 1), 6],
224
+ [Datet.new(1930, 1, 1), 2],
225
+ [Datet.new(1940, 1, 1), 0],
226
+ [Datet.new(1969, 12, 31), 2],
227
+ [Datet.new(2012, 7, 15), 6],
228
+ [Datet.new(2012, 7, 16), 0],
229
+ [Datet.new(2012, 7, 17), 1]
230
+ ]
231
+ tests.each do |data|
232
+ day_in_week = data[0].day_in_week
233
+ raise "Expected #{data[1]} but got #{day_in_week}" if day_in_week != data[1]
234
+
235
+ diw = data[0].time.strftime("%w").to_i
236
+ if diw == 0
237
+ diw = 6
238
+ else
239
+ diw -= 1
240
+ end
241
+
242
+ raise "Time-method didnt return the same: #{diw}, #{day_in_week}" if diw != day_in_week
243
+ end
244
+ end
245
+
246
+ it "should be compareable" do
247
+ tests = [
248
+ [Datet.new(2012, 6, 1, 14, 1, 1), Datet.new(2010, 6, 1, 14, 1, 1)],
249
+ [Datet.new(2012, 6, 1, 14, 1, 1), Datet.new(2012, 5, 1, 14, 1, 1)],
250
+ [Datet.new(2012, 6, 2, 14, 1, 1), Datet.new(2012, 6, 1, 14, 1, 1)],
251
+ [Datet.new(2012, 6, 1, 14, 1, 1), Datet.new(2012, 6, 1, 13, 1, 1)]
252
+ ]
253
+
254
+ tests.each do |data|
255
+ d1 = Datet.new(data[0])
256
+ d2 = Datet.new(data[1])
257
+
258
+ res_b = data[0] > data[1]
259
+ res_s = data[0] < data[1]
260
+ res_e = data[0] == data[1]
261
+
262
+ raise "Expected 'res_bigger' to be true but it wasnt: #{res_b}" if res_b != true
263
+ raise "Expected 'res_smaller' to be false but it wasnt: #{res_s}" if res_s != false
264
+ raise "Expected 'res_equal' to be false but it wasnt: #{res_e}" if res_e != false
265
+ end
266
+ end
267
+
268
+ it "should be able to return day-strings" do
269
+ datet = Datet.new(1970, 1, 4)
270
+ 6.times do |i|
271
+ day_str = datet.day_str
272
+ exp = Datet.days_arr[i]
273
+ raise "Expected '#{exp}' but got '#{day_str}' for day-no: '#{i}'." if day_str != exp
274
+ datet.days + 1
275
+ end
276
+ end
176
277
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: datet
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.5
5
+ version: 0.0.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-07-14 00:00:00 +02:00
13
+ date: 2012-07-15 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -104,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
104
  requirements:
105
105
  - - ">="
106
106
  - !ruby/object:Gem::Version
107
- hash: -4377164739836727244
107
+ hash: 4324212231909308806
108
108
  segments:
109
109
  - 0
110
110
  version: "0"