# Time range help

**URL:** <https://www.ruby-forum.com/t/time-range-help/104157>\
**Category:** Ruby\
**Created:** [May 14, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/time-range-help/104157 "2007-05-14T21:45:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/time-range-help/104157/1 "2007-05-14T21:45:40Z")

</div>

I have 2 time objects and want to iterate over them by day. I figured  
out that I can do:  
a = Time.mktime(2007,5,1,0,0,0)  
b = Time.mktime(2007,5,7,0,0,0)  
(a…b).each { |val|  
code goes here  
}  
but when I do that it iterates over seconds. Does anyone have a  
suggestion on how to iterate over a different period when using Time  
objects?

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/time-range-help/104157/2 "2007-05-14T21:58:00Z")

</div>

Try this:

t1 = Time.mktime(2001,3,15,21,30,15)  
t2= Time.mktime(2001,3,19,21,30,15)  
(t1.day…t2.day).each\_with\_index{|x,i|  
puts “Day #{i} was … #{x}”  
}

# … and …

p t1.methods

gives … for months : t1.mon etc …

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/time-range-help/104157/3 "2007-05-14T21:59:01Z")

</div>

Does it have to be a time object? Date has some methods that will do  
what  
you want without much if any hassle.

On 5/14/07, Mike H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> objects?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
“Hey brother christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/time-range-help/104157/4 "2007-05-14T22:34:10Z")

</div>

On May 14, 2007, at 4:02 PM, Mike H. wrote:

> > > b = Time.mktime(2007,5,7,0,0,0)  
> > > so loud, I can’t hear a word you’re saying."  
> > > that I can include the appropriate gmtime adjustment in the query  
> > > values.

Why do you need to iterate over them? If these values are already in  
the database, can’t you just use:

:conditions =\> { :some\_gmt\_datetime\_column =\> a…b }

which becomes a where clause like “some\_gmt\_datetime\_column BETWEEN  
‘2007-05-01’ AND ‘2007-05-07’” (or whatever a.to\_s(:db) gives)

If this doesn’t help, perhaps you could explain your query a bit more  
and where you think the iteration is needed.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/time-range-help/104157/5 "2007-05-14T23:10:20Z")

</div>

Rob B. wrote:

> On May 14, 2007, at 4:02 PM, Mike H. wrote:
> 
> > > > b = Time.mktime(2007,5,7,0,0,0)  
> > > > so loud, I can’t hear a word you’re saying."  
> > > > that I can include the appropriate gmtime adjustment in the query  
> > > > values.
> 
> Why do you need to iterate over them? If these values are already in  
> the database, can’t you just use:
> 
> :conditions =\> { :some\_gmt\_datetime\_column =\> a…b }
> 
> which becomes a where clause like “some\_gmt\_datetime\_column BETWEEN  
> ‘2007-05-01’ AND ‘2007-05-07’” (or whatever a.to\_s(:db) gives)
> 
> If this doesn’t help, perhaps you could explain your query a bit more  
> and where you think the iteration is needed.
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

The challenge is that the database is just something I’m reporting off  
of and isn’t something that I designed or built in Rails so I’m doing a  
lot of raw SQL. I did manage to find a way around it by building a date  
range off of the time objects, iterating over that, and keeping the time  
values constant by building them off of the Time objects as well. Works  
well enough for what I’m trying to do!  
Thanks for the input!

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/time-range-help/104157/6 "2007-05-14T23:27:18Z")

</div>

On 5/14/07, Mike H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Cool. I was going to suggest using Time’s to\_datetime method although  
that  
might not help you so much if you have to put the data back once you’ve  
looked at it.

–  
“Hey brother christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/time-range-help/104157/7 "2007-05-14T22:02:28Z")

</div>

Glen H. wrote:

> Does it have to be a time object? Date has some methods that will do  
> what  
> you want without much if any hassle.
> 
> On 5/14/07, Mike H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > objects?
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> –  
> “Hey brother christian with your high and mighty errand, Your actions  
> speak  
> so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)  
> Well - I know how to do it with a date object but the objects I’m using  
> are already Time objects. The actual problem is that the dates that I’m  
> iterating over are entered in a local timezone and then converted to  
> gmtime for a SQL query. All the date values are stored in the database  
> in gmtime, so in order to run the query I have to have the time  
> included. That’s why iterating over the time object would be ideal, so  
> that I can include the appropriate gmtime adjustment in the query  
> values.
