[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] timeunits-0.0.0

Ara.T.Howard

9/5/2006 9:44:00 PM

8 Answers

Stefan Lang

9/5/2006 10:13:00 PM

0


On Wednesday, September 06, 2006, at 6:43 AM, wrote:
>
>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/code...
> http://codeforpeople.org...
>
>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

--
Posted with http://De.... Sign up and save your mailbox.

Ara.T.Howard

9/5/2006 10:36:00 PM

0

Logan Capaldo

9/5/2006 11:52:00 PM

0


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.
>>
>> 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'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



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


Ara.T.Howard

9/6/2006 12:53:00 AM

0

Ara.T.Howard

9/6/2006 1:04:00 AM

0

Stefan Lang

9/6/2006 5:58:00 AM

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://De.... Sign up and save your mailbox.

Rimantas Liubertas

9/6/2006 10:13:00 AM

0

<...>
> >>> 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
--
http://rim...

Ara.T.Howard

9/6/2006 2:26:00 PM

0