diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2009-08-23 02:14:13 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2011-06-30 08:04:23 +0200 |
commit | 847cb204b3081c485d19bae31c83c561bd416956 (patch) | |
tree | 108b664c05e747d138738829f9d26c14c353d48d /lib | |
parent | b765e18034f5df6169bba2d723a649425d19eede (diff) | |
download | ayk-847cb204b3081c485d19bae31c83c561bd416956.zip ayk-847cb204b3081c485d19bae31c83c561bd416956.tar.gz |
DateTime#age + spec
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ayk/age.rb | 37 |
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 + |