summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/ayk/age.rb37
1 files changed, 37 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
+