[ANN] timeunits-0.0.0

A

ara.t.howard

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
 
K

Kevin Olbrich

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
--
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
 
A

ara.t.howard

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
 
A

ara.t.howard

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.
% script/console
Loading development environment.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top