Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Time interval

Reply
Thread Tools

Time interval

 
 
Daniel Berger
Guest
Posts: n/a
 
      09-26-2005
Hi all,

I was just wondering if anyone has implemented an "Interval" class of some sort
out there, i.e. something that would give you the years, months, days, hours,
minutes and seconds between two fixed DateTime objects, rather than an absolute
date.

I see that Gavin Sinclair brought this issue up a couple times last year
(ruby-talk:95236, ruby-talk:103601), but I didn't see any sort of package on
the RAA.

I had something of my own with "fixed-time" but it doesn't handle Date, Time or
DateTime objects (i.e. it needs a major makeover). Before I do that, though, I
wanted to see if anyone out there (Gavin?) already had something out there,
sitting on their hard drive, but unreleased.

Regards,

Dan

PS - I'm very confused by the current implementation of "-" (minus) for Date
and DateTime, where the returned value is a Rational. Can someone elaborate on
how this is used?


 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      09-26-2005
I don't know of any specific DataTimeInterval, but Mega has two things
that might help:

require 'mega/interval'
require 'mega/time_in_english'

t= Time.now
i = Interval[ t, t + 5.days ]
=> #<Interval:0xb7aefde8 @last=Sat Oct 01 14:15:28 EDT 2005,
@direction=1, @first=Mon Sep 26 14:15:28 EDT 2005, @exclude_last=false,
@exclude_first=false>

i.distance / 1.days
=> 5.0

HTH.

Hmm... who's wants to put together a generic Delta delgator class?

T.

 
Reply With Quote
 
 
 
 
Graham Foster
Guest
Posts: n/a
 
      09-26-2005
On Mon, 26 Sep 2005 18:55:51 +0100, Daniel Berger
<(E-Mail Removed)> wrote:

> Hi all,
>
> I was just wondering if anyone has implemented an "Interval" class of
> some sort out there, i.e. something that would give you the years,
> months, days, hours, minutes and seconds between two fixed DateTime
> objects, rather than an absolute date.
>

Doesn't Rails Active Support porvide .ago, .since, .years_since, .from_now
etc? Any use?
Graham
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-26-2005
Daniel Berger <(E-Mail Removed)> wrote:
> Hi all,
>
> I was just wondering if anyone has implemented an "Interval" class of
> some sort out there, i.e. something that would give you the years,
> months, days, hours, minutes and seconds between two fixed DateTime
> objects, rather than an absolute date.


Note that it's easy to output difference in seconds, minutes, hours, days
and weeks but then it starts to get more difficult.

I once cooked up something albeit in Java that takes two arrays (an array
with factors and an array with unit names) and a value and constructs a
string from that. Untested:

def create_units(val, factors, units)
raise "mismatch" unless factors.size == units.size

r = []

factors.zip(units) do |f,u|
v = val % f
r << "#{val} #{v}" if v != 0
val = (val - v) / f
end

r.reverse!
r.join " "
end

Kind regards

robert

 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      09-26-2005
Robert Klemme wrote:
> Daniel Berger <(E-Mail Removed)> wrote:
>
>> Hi all,
>>
>> I was just wondering if anyone has implemented an "Interval" class of
>> some sort out there, i.e. something that would give you the years,
>> months, days, hours, minutes and seconds between two fixed DateTime
>> objects, rather than an absolute date.

>
>
> Note that it's easy to output difference in seconds, minutes, hours,
> days and weeks but then it starts to get more difficult.


You mean with the existing DateTime class? How? I thought this was possible,
but my Google skills appear to be failing me at the moment. My own experiments
weren't much better.

Regards,

Dan


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-27-2005
Daniel Berger wrote:
> Robert Klemme wrote:
>> Daniel Berger <(E-Mail Removed)> wrote:
>>
>>> Hi all,
>>>
>>> I was just wondering if anyone has implemented an "Interval" class
>>> of some sort out there, i.e. something that would give you the
>>> years, months, days, hours, minutes and seconds between two fixed
>>> DateTime objects, rather than an absolute date.

>>
>>
>> Note that it's easy to output difference in seconds, minutes, hours,
>> days and weeks but then it starts to get more difficult.

>
> You mean with the existing DateTime class? How? I thought this was
> possible, but my Google skills appear to be failing me at the moment.
> My own experiments weren't much better.


No, I mean because of the underlying model (our calendar). Months have
different lengths. Same for years and quarters.

Kind regards

robert

 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      09-27-2005
On Sep 27, 2005, at 2:51 AM, Robert Klemme wrote:
> Daniel Berger wrote:
>> Robert Klemme wrote:
>>> Note that it's easy to output difference in seconds, minutes, hours,
>>> days and weeks but then it starts to get more difficult.

>> You mean with the existing DateTime class? How? I thought this was
>> possible, but my Google skills appear to be failing me at the moment.
>> My own experiments weren't much better.

> No, I mean because of the underlying model (our calendar). Months
> have
> different lengths. Same for years and quarters.


For example:

require 'date'
d1 = DateTime.parse( "2/1/2000" )
d2 = DateTime.parse( "3/2/2000" )
puts d2-d1 #=> 30

Should the human-readable difference in that time say:
1 month, 1 day
30 days
1 month
?

How about:
d1 = DateTime.parse( "2/1/2000" )
d2 = DateTime.parse( "2/1/2001" )
d3 = DateTime.parse( "2/1/2002" )
puts d2-d1 #=> 366
puts d3-d2 #=> 365

Should those both say "1 year", or "1 year, 1 day" for the first?

If you standardize and say "All 'months' are 30 days long, all
'years' are 365 days" then you can run into situations like the above
where the obvious difference to a human is different from the
computed difference. This only falls down when you try to go beyond
weeks; if all your time intervals are short enough that formatting
them as "3 weeks, 4 days, 5 hours, 27 minutes" is acceptable enough,
then you're good to go.

Alternatively, you need to keep year/month/date information along
with the endpoints of your interval, and do piecewise differencing.


 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      09-27-2005
Gavin Kistner schrieb:
> On Sep 27, 2005, at 2:51 AM, Robert Klemme wrote:
>>
>> No, I mean because of the underlying model (our calendar). Months have
>> different lengths. Same for years and quarters.

>
> For example:
>
> require 'date'
> d1 = DateTime.parse( "2/1/2000" )
> d2 = DateTime.parse( "3/2/2000" )
> puts d2-d1 #=> 30
>
> Should the human-readable difference in that time say:
> 1 month, 1 day
> 30 days
> 1 month
> ?
>
> How about:
> d1 = DateTime.parse( "2/1/2000" )
> d2 = DateTime.parse( "2/1/2001" )
> d3 = DateTime.parse( "2/1/2002" )
> puts d2-d1 #=> 366
> puts d3-d2 #=> 365
>
> Should those both say "1 year", or "1 year, 1 day" for the first?
>
> If you standardize and say "All 'months' are 30 days long, all 'years'
> are 365 days" then you can run into situations like the above where the
> obvious difference to a human is different from the computed
> difference. This only falls down when you try to go beyond weeks; if
> all your time intervals are short enough that formatting them as "3
> weeks, 4 days, 5 hours, 27 minutes" is acceptable enough, then you're
> good to go.
>
> Alternatively, you need to keep year/month/date information along with
> the endpoints of your interval, and do piecewise differencing.


Recently I've been representing time intervals by two numbers: months
and seconds. With this representation it is possible to differentiate
between "1 year" and "365 days":

1 year = 12 months (and 0 seconds)
365 days = (0 months and) 365 * 24 * 60 * 60 seconds

Regards,
Pit


 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      09-27-2005
On Sep 27, 2005, at 7:19 AM, Pit Capitain wrote:
> Recently I've been representing time intervals by two numbers:
> months and seconds. With this representation it is possible to
> differentiate between "1 year" and "365 days":
>
> 1 year = 12 months (and 0 seconds)
> 365 days = (0 months and) 365 * 24 * 60 * 60 seconds


While precise, I claim that that's not very human-friendly.

"How long ago did I receive this email?"
"20160 seconds"
"Whaaaa? Is that long or short?"
"Er, I meant 2 weeks"
"Oh, thanks."



 
Reply With Quote
 
Randy Kramer
Guest
Posts: n/a
 
      09-27-2005
On Tuesday 27 September 2005 04:51 am, Robert Klemme wrote:
> No, I mean because of the underlying model (our calendar). Months have
> different lengths. Same for years and quarters.


Two thoughts, which I don't know whether will help or hurt (may be useful
approximations?):

* (I guess this is the furthest off point): There seems to be an at least
"semi-official" designation of the weeks in a year--you will find some
calendars that number the weeks, and when I've checked, those numbers seem to
be consistent between calendars, even made in different countries. (I think
the ones I've noticed, some years ago, were calendars from England, Austria,
and the US.)

* Depending on the level of precision I wanted, I might calculate an
interval in weeks, then consider 4 1/3 weeks ~= 1 month, 13 weeks ~= 1
quarter, and 52 weeks = 1 year. If I wanted to express some arbitrary number
of weeks in months, (e.g., 62, I'd divide by 4.34 and round off to the
nearest integer and call it that number of months.

Nope, I take that back. I'd more likely just express the difference between
say, Jan 3rd and Dec 15 as the difference in months then the difference in
days, i.e., 11 months, 12 days. (and I'd approximate that as either 11, 11
1/2, or 11 months, 2 weeks).

If I wanted to express that in weeks, I guess my two choices would be to use
the semi-official designation of weeks (first bullet above) and get the
number of weeks by the difference in week numbers, and then depending on the
days of the week, approximate it to the nearest 1/2 (or actual number of
days).

Or, calculate the actual difference in days, then divide by 7, call that
weeks, and the remainder in days (or rounded off to, say, 1/2 week.

Do others see the need for other approaches?

Randy Kramer


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
time control doesn't work with interval > 259 Dica ASP .Net 0 11-11-2005 06:01 PM
'point in time' time interval Andrew Poulos Javascript 4 06-15-2005 12:34 AM
Measuring a short interval in time Wes Harrison Java 2 09-14-2004 05:04 PM
Finding all time periods for a given interval within a date range Graeme Longman Python 1 08-04-2004 06:02 PM
Recommendations for Time Lapse Photography - Interval Shooting (correction) No Left Turn Digital Photography 5 09-30-2003 11:16 PM



Advertisments