summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2009-08-23 02:14:13 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2011-06-30 08:04:23 +0200
commit847cb204b3081c485d19bae31c83c561bd416956 (patch)
tree108b664c05e747d138738829f9d26c14c353d48d
parentb765e18034f5df6169bba2d723a649425d19eede (diff)
downloadayk-847cb204b3081c485d19bae31c83c561bd416956.zip
ayk-847cb204b3081c485d19bae31c83c561bd416956.tar.gz
DateTime#age + spec
-rw-r--r--lib/ayk/age.rb37
-rw-r--r--spec/age.rb88
2 files changed, 125 insertions, 0 deletions
diff --git a/lib/ayk/age.rb b/lib/ayk/age.rb
new file mode 100644
index 0000000..fc1aeb3
--- /dev/null
+++ b/lib/ayk/age.rb
@@ -0,0 +1,37 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+
+#----------------------------------------------------------------------------
+#
+# File : age.rb
+# Author : Jérémy Zurcher <jeremy@asynk.ch>
+# Date : 22/08/09
+#
+#----------------------------------------------------------------------------
+
+require 'date'
+
+class DateTime
+ # returns an array representing years,months,days,hours,minutes,seconds
+ # between self and futur DateTime at
+ def age at=DateTime.now
+ return [nil]*6 if at<self
+ # years
+ years = at.year-self.year
+ years -= 1 if ( self>>(12*years) ) > at
+ x = self>>(12*years)
+ # months
+ months = at.month-x.month+(12*(at.year-x.year))
+ months -= 1 if ( x>>months ) > at
+ x >>=months
+ # days
+ days = (at-x).to_i
+ x += days
+ # hours
+ hours,r = (at.to_time-x.to_time).to_i.divmod 3600
+ mins,secs = r.divmod 60
+ # return
+ [years,months,days,hours,mins,secs]
+ end
+end
+
diff --git a/spec/age.rb b/spec/age.rb
new file mode 100644
index 0000000..99a09c7
--- /dev/null
+++ b/spec/age.rb
@@ -0,0 +1,88 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+
+require 'ayk/age'
+#
+def age_spec(birth,dy,dm,dd)
+ at = ((birth>>(12*dy))>>dm)+dd
+# puts at.strftime "%d.%m.%Y"
+ y,m,d,h,i,s = birth.age(at)
+# puts "#{y} #{m} #{d}"
+ y.should eql dy
+ m.should eql dm
+ d.should eql dd
+end
+#
+describe DateTime do
+ #
+ before(:all) do
+ @b = DateTime.civil 2009,7,27,4,40,0 # Róisín Mannion-Zurcher [IRL|CH] )
+ end
+ it "should pass time checks" do
+ y,m,d,h,i,s = @b.age( DateTime.civil(2009,7,27,4,40,0) )
+ h.should eql 0
+ i.should eql 0
+ s.should eql 0
+ y,m,d,h,i,s = @b.age( DateTime.civil(2009,7,27,7,2,2) )
+ h.should eql 2
+ i.should eql 22
+ s.should eql 2
+ end
+ it "simple checks" do
+ age_spec(@b,0,0,0)
+ age_spec(@b,0,0,1)
+ age_spec(@b,0,1,0)
+ age_spec(@b,0,1,1)
+ age_spec(@b,1,0,0)
+ age_spec(@b,1,0,1)
+ age_spec(@b,1,1,0)
+ age_spec(@b,1,1,1)
+ end
+ it "should pass day computations" do
+ at = ((@b>>(12*0))>>15)+3
+ y,m,d,h,i,s = @b.age(at)
+ y.should eql 1
+ m.should eql 3
+ d.should eql 3
+ at = ((@b>>(12*0))>>1)+366
+ y,m,d,h,i,s = @b.age(at)
+ y.should eql 1
+ m.should eql 1
+ d.should eql 1
+ at = ((@b>>(12*5))>>10)+15
+ y,m,d,h,i,s = @b.age(at)
+ y.should eql 5
+ m.should eql 10
+ d.should eql 15
+ at = ((@b>>(12*0))>>0)+750
+ y,m,d,h,i,s = @b.age(at)
+ y.should eql 2
+ m.should eql 0
+ d.should eql 20
+ end
+ it "should pass more complicated one" do
+ at = DateTime.civil 2010,7,13,4,40,0 # Corina Mannion
+ y,m,d,h,i,s = @b.age(at)
+ y.should eql 0
+ m.should eql 11
+ d.should eql 16
+ h.should eql 0
+ i.should eql 0
+ s.should eql 0
+ at = DateTime.civil 2010,7,13,0,0,0 # Corina Mannion
+ y,m,d,h,i,s = @b.age(at)
+ y.should eql 0
+ m.should eql 11
+ d.should eql 15
+ h.should eql 19
+ i.should eql 20
+ s.should eql 0
+ end
+end
+
+
+#(1..365).each do |n|
+# dt = birth.age( birth+n)
+# puts "#{n} => #{dt[1]} #{dt[2]}"
+#end
+