Timeunits-0.0.0

we’ve all written bits of it before, here it is for that don’t want to:

NAME

timeunits.rb

URIS

http://rubyforge.org/projects/codeforpeople/
http://codeforpeople.org/lib/ruby/

SYNOPSIS

adds methods to Numeric and Time classes to support time units and
time
difference units.

EXAMPLE

 harp:~ > cat a.rb
 require "timeunits"
 require "yaml"

 now = Time::now

 a = now
 y "a" => a

 b = now + 2.hours + 2.minutes
 y "b" => b

 d = b - a
 %w( seconds minutes hours days ).each do |unit|
   y "d.#{ unit }" => d.send(unit)
 end

 harp:~ > ruby a.rb
 a: 2006-09-05 15:33:23.697319 -06:00
 b: 2006-09-05 17:35:23.697319 -06:00
 d.seconds: 7320.0
 d.minutes: 122.0
 d.hours: 2.03333333333333
 d.days: 0.0847222222222222

DOCS

see lib/*

enjoy.

-a

On Wednesday, September 06, 2006, at 6:43 AM, wrote:

http://rubyforge.org/projects/codeforpeople/
require “timeunits”
d = b - a
d.days: 0.0847222222222222
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

FWIW,

Rails already does this, as does Facets, the conversions gem, and the
ruby-units gem.

The API varies a little bit.

_Kevin
www.sciwerks.com

On Wed, 6 Sep 2006, Kevin O. wrote:

FWIW,

Rails already does this, as does Facets, the conversions gem, and the
ruby-units gem.

The API varies a little bit.

they do this?

a = Time.now
b = a + 42.seconds

delta = b - a

p delta.days #=> 0.000486111111111111

i didn’t know they handled time differences too? i’m sure rails does
not do
this and the docs for facets seems to show it does not. are you sure?

i thought the example was pretty clear that the unit methods were
asymetric in
the way they operate on times and time deltas… if not, sorry.

cheers.

-a

On Sep 5, 2006, at 6:36 PM, [email protected] wrote:

a = Time.now
b = a + 42.seconds

delta = b - a

p delta.days #=> 0.000486111111111111

i didn’t know they handled time differences too? i’m sure rails
does not do
this and the docs for facets seems to show it does not. are you sure?

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

On Wed, 6 Sep 2006, Logan C. wrote:

I’m not exactly sure what delta.days means in this case, but rails does the
following with your example:

it’s the difference (delta) in days.

3628800.0
=> nil

right. that is totally wrong. the difference is only 42 seconds,
therefor it
should be a fractional day. eg.

 harp:~ > cat a.rb
 require 'yaml'
 require 'rubygems'
 require 'timeunits'

 a = Time.now.utc
 b = a + 42.seconds

 delta = b - a

 y 'delta.decades' => delta.decades
 y 'delta.years' => delta.years
 y 'delta.months' => delta.months
 y 'delta.days' => delta.days
 y 'delta.minutes' => delta.minutes
 y 'delta.seconds' => delta.seconds



 harp:~ > ruby a.rb
 delta.decades: 1.44675925925926e-07
 delta.years: 1.44675925925926e-06
 delta.months: 1.73611111111111e-05
 delta.days: 0.000486111111111111
 delta.minutes: 0.7
 delta.seconds: 42.0

the src is extremely short - read it for details. hopefully though, this
example show the difference.

-a

On Wed, 6 Sep 2006, Logan C. wrote:

The API varies a little bit.
i didn’t know they handled time differences too? i’m sure rails does not
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

<…>

delta = b - a
=> 42.0

p delta.days
3628800.0
=> nil

right. that is totally wrong. the difference is only 42 seconds, therefor it
should be a fractional day. eg.
<…>

‘days’ and similar methods in Rails are core extensions and return
number of seconds.
So 42.days returns number of seconds in 42 days.

Useful for things like:

10.minutes.ago
=> Wed Sep 06 12:56:52 FLE Daylight Time 2006

3.days.since
=> Sat Sep 09 13:06:58 FLE Daylight Time 2006

Regards,
Rimantas

On Wed, 6 Sep 2006, Rimantas L. wrote:

<…>
=> Sat Sep 09 13:06:58 FLE Daylight Time 2006
thanks, i realize that. this little extension does both:

 harp:~ > cat a.rb
 require 'timeunits'

 a = Time.now.utc

 b = a + 42.days  # like rails, facets, etc.

 p a
 p b

 delta = b - a
 p delta.days     # inverse operation


 harp:~ > ruby a.rb
 Wed Sep 06 14:23:29 UTC 2006
 Wed Oct 18 14:23:29 UTC 2006
 42.0

not that computing ‘days’ on a difference is the inverse operation (/)
as
compared to when doing in a summation (’*’).

regards.

-a

On Wednesday, September 06, 2006, at 10:04 AM, wrote:

ruby-units gem.

delta = b - a
require ‘timeunits’

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