From: Kevin Olbrich Date: 2006-09-06T14:57:44+09:00 Subject: Re: [ANN] timeunits-0.0.0 On Wednesday, September 06, 2006, at 10:04 AM, wrote: >On Wed, 6 Sep 2006, Logan Capaldo wrote: > >> >> On Sep 5, 2006, at 6:36 PM, ara.t.howard@noaa.gov wrote: >> >>> On Wed, 6 Sep 2006, Kevin Olbrich wrote: >>> >>>> FWIW, >>>> >>>> Rails already does this, as does Facets, the conversions gem, and the >>>> ruby-units gem. >>>> >> I'm not exactly sure what delta.days means in this case, but rails >>does the >> following with your example: >> >> % script/console >> Loading development environment. >>>> a = Time.now >> => Tue Sep 05 19:50:11 -0400 2006 >>>> b = a + 42.seconds >> => Tue Sep 05 19:50:53 -0400 2006 >>>> delta = b - a >> => 42.0 >>>> p delta.days >> 3628800.0 >> => nil > >i just realized this might explain it better > > harp:~ > cat a.rb > require 'yaml' > require 'rubygems' > require 'timeunits' > > a = Time.now.utc > b = a + 42.seconds > > delta = b - a > > y 'delta.days' => delta.days > > pct_day = delta.days > y 'seconds' => (pct_day * 1.day).seconds > > > harp:~ > ruby a.rb > delta.days: 0.000486111111111111 > seconds: 42.0 > >-a >-- >what science finds to be nonexistent, we must accept as nonexistent; >but what >science merely does not find is a completely different matter... it >is quite >clear that there are many, many mysterious things. >- h.h. the 14th dalai lama > Hmm, yeah looks like rails has some issues there, although you can do.. (42.seconds / 1.day).to_f #=> 0.000486111111111111 in rails. I like this better... require 'rubygems' require 'ruby-units' a = "#{Time.now.to_i} s".unit b = a + '42 s' delta = b-a #=> 42 s delta.to("days") #=> 0.000486111 d delta.to("fortnight") #=>3.47222e-05 fortnight delta.to("ms") #=>42000 ms delta.to('century') #=> 1.33093e-08 century I'm sure you can do something similar with Facets. You can also do just about any other unit you can think of with Facets and ruby-units. Disclaimer: there are still a few bugs in ruby-units, mostly related to properly parsing some ambiguous units. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.