Calculating an array of dates

I have the following test script. My objective is to have an array of
dates between this week’s Thursday and some date in the past that I get
out of a call to a database. You can see the output below which is not
what I wanted. I am thinking it is in the way I return data and call my
recursive routine. What am I missing?

I am hoping for data like, which later I can loop through:

[ [Thu Jul 09 12:00:00 -0700 2009],
[Thu Jul 02 12:00:00 -0700 2009],
[Thu Jun 25 12:00:00 -0700 2009],
[Thu Jun 18 12:00:00 -0700 2009],
[Thu Jun 11 13:14:44 -0700 2009] ]

Thanks for any help ya’ll can provide.
Eric

start<<<<<<<<<<<
require ‘rubygems’
require ‘Chronic’
require ‘Time’

---------- ---------- ---------- ---------- ---------- ----------

def get_weeks( this_dt, max_dt )

---------- ---------- ---------- ---------- ---------- ----------

recursively calculate thursdays

---------- ---------- ---------- ---------- ---------- ----------

return this_dt.to_a if ( this_dt.strftime( “%Y%m%b” ) ==
max_dt.strftime( “%Y%m%b” ) )
puts “get_week_list(): 4. this_dt: #{this_dt.inspect}”
puts “get_week_list(): 5. max_dt: #{max_dt.inspect}”

next_dt = Chronic.parse( ‘Last Thursday at noon’, :now => this_dt )
puts “get_week_list(): 6. next_dt: #{next_dt.inspect}\n\n”
return get_weeks( next_dt, max_dt ).to_a

end # get_weeks

---------- ---------- ---------- ---------- ---------- ----------

def get_week_list( u_id )

---------- ---------- ---------- ---------- ---------- ----------

return an array of dates between now and when this user was created

---------- ---------- ---------- ---------- ---------- ----------

---------- ---------- ----------

what is this week’s “end point”.

Either today or calculated

---------- ---------- ----------

this_week_end = Time.now
puts “get_week_list(): Today’s day: #{this_week_end.wday.to_s}”
if Time.now.wday != 4 # not thursday

this_week_end = Chronic.parse( 'Last Thursday at noon', :now =>

Time.now )

end
puts “get_week_list(): 1. this_week_end: #{this_week_end.inspect}”

---------- ---------- ----------

How far back should we get a list?

How about to when this user was created?

---------- ---------- ----------

max_date = Time.parse( ‘Thu Jun 11 13:14:44 -0700 2009’ ) # from a db
call
puts “get_week_list(): 2. max_date’s day: #{max_date.wday.to_s}”
if max_date.wday != 4

max_date = Chronic.parse( 'Last Thursday at noon', :now => max_date

)

end
puts “get_week_list(): 3. max_date: #{max_date.inspect}\n\n”

---------- ---------- ----------

recursively calc between the two

---------- ---------- ----------

dates = Array.new
dates << get_weeks( this_week_end, max_date )
return dates

end # get_week_list

--------------------------------------------------------------------

dts = Array.new
dts << get_week_list( 2 )
puts dts.inspect
puts dts.class

end<<<<<<<<<<<

returns:

C:\A\ruby>ruby w.rb
get_week_list(): Today’s day: 5
get_week_list(): 1. this_week_end: Thu Jul 09 12:00:00 -0700 2009
get_week_list(): 2. max_date’s day: 4
get_week_list(): 3. max_date: Thu Jun 11 13:14:44 -0700 2009

get_week_list(): 4. this_dt: Thu Jul 09 12:00:00 -0700 2009
get_week_list(): 5. max_dt: Thu Jun 11 13:14:44 -0700 2009
get_week_list(): 6. next_dt: Thu Jul 02 12:00:00 -0700 2009

get_week_list(): 4. this_dt: Thu Jul 02 12:00:00 -0700 2009
get_week_list(): 5. max_dt: Thu Jun 11 13:14:44 -0700 2009
get_week_list(): 6. next_dt: Thu Jun 25 12:00:00 -0700 2009

[[[0, 0, 12, 25, 6, 2009, 4, 176, true, “Pacific Daylight Time”]]]
Array

Eric P. wrote:

I have the following test script. My objective is to have an array of
dates between this week’s Thursday and some date in the past that I get
out of a call to a database. You can see the output below which is not
what I wanted. I am thinking it is in the way I return data and call my
recursive routine. What am I missing?

I am hoping for data like, which later I can loop through:

[ [Thu Jul 09 12:00:00 -0700 2009],
[Thu Jul 02 12:00:00 -0700 2009],
[Thu Jun 25 12:00:00 -0700 2009],
[Thu Jun 18 12:00:00 -0700 2009],
[Thu Jun 11 13:14:44 -0700 2009] ]

Thanks for any help ya’ll can provide.
Eric

start<<<<<<<<<<<
require ‘rubygems’
require ‘Chronic’
require ‘Time’

---------- ---------- ---------- ---------- ---------- ----------

def get_weeks( this_dt, max_dt )

---------- ---------- ---------- ---------- ---------- ----------

recursively calculate thursdays

---------- ---------- ---------- ---------- ---------- ----------

return this_dt.to_a if ( this_dt.strftime( “%Y%m%b” ) ==
max_dt.strftime( “%Y%m%b” ) )
puts “get_week_list(): 4. this_dt: #{this_dt.inspect}”
puts “get_week_list(): 5. max_dt: #{max_dt.inspect}”

next_dt = Chronic.parse( ‘Last Thursday at noon’, :now => this_dt )
puts “get_week_list(): 6. next_dt: #{next_dt.inspect}\n\n”
return get_weeks( next_dt, max_dt ).to_a

end # get_weeks

---------- ---------- ---------- ---------- ---------- ----------

def get_week_list( u_id )

---------- ---------- ---------- ---------- ---------- ----------

return an array of dates between now and when this user was created

---------- ---------- ---------- ---------- ---------- ----------

---------- ---------- ----------

what is this week’s “end point”.

Either today or calculated

---------- ---------- ----------

this_week_end = Time.now
puts “get_week_list(): Today’s day: #{this_week_end.wday.to_s}”
if Time.now.wday != 4 # not thursday

this_week_end = Chronic.parse( 'Last Thursday at noon', :now =>

Time.now )

end
puts “get_week_list(): 1. this_week_end: #{this_week_end.inspect}”

---------- ---------- ----------

How far back should we get a list?

How about to when this user was created?

---------- ---------- ----------

max_date = Time.parse( ‘Thu Jun 11 13:14:44 -0700 2009’ ) # from a db
call
puts “get_week_list(): 2. max_date’s day: #{max_date.wday.to_s}”
if max_date.wday != 4

max_date = Chronic.parse( 'Last Thursday at noon', :now => max_date

)

end
puts “get_week_list(): 3. max_date: #{max_date.inspect}\n\n”

---------- ---------- ----------

recursively calc between the two

---------- ---------- ----------

dates = Array.new
dates << get_weeks( this_week_end, max_date )
return dates

end # get_week_list

--------------------------------------------------------------------

dts = Array.new
dts << get_week_list( 2 )
puts dts.inspect
puts dts.class

end<<<<<<<<<<<

returns:

C:\A\ruby>ruby w.rb
get_week_list(): Today’s day: 5
get_week_list(): 1. this_week_end: Thu Jul 09 12:00:00 -0700 2009
get_week_list(): 2. max_date’s day: 4
get_week_list(): 3. max_date: Thu Jun 11 13:14:44 -0700 2009

get_week_list(): 4. this_dt: Thu Jul 09 12:00:00 -0700 2009
get_week_list(): 5. max_dt: Thu Jun 11 13:14:44 -0700 2009
get_week_list(): 6. next_dt: Thu Jul 02 12:00:00 -0700 2009

get_week_list(): 4. this_dt: Thu Jul 02 12:00:00 -0700 2009
get_week_list(): 5. max_dt: Thu Jun 11 13:14:44 -0700 2009
get_week_list(): 6. next_dt: Thu Jun 25 12:00:00 -0700 2009

[[[0, 0, 12, 25, 6, 2009, 4, 176, true, “Pacific Daylight Time”]]]
Array

I have not looked into the reasons why your code doesn’t work, just took
your post as a coding exercise. My try looks like this:

require ‘Date’
class Date
def thursdays_upto_now
end_date = Date.today
res = []
# get the last thursday:
end_date -= 1 while (end_date.wday != 4)
end_date.step(self, -7) do |date|
res << date
end
res
end
end

start_date = Date.parse( ‘Thu Jun 11 13:14:44 -0700 2009’ )
start_date.thursdays_upto_now.each{|thursday| puts thursday.to_s} # or
whatever

I was happy finding out Date has a step method.

hth, I had some fun,

Siep

Siep K. wrote:

I have not looked into the reasons why your code doesn’t work, just took
your post as a coding exercise. My try looks like this:

require ‘Date’
class Date
def thursdays_upto_now
end_date = Date.today
res = []
# get the last thursday:
end_date -= 1 while (end_date.wday != 4)
end_date.step(self, -7) do |date|
res << date
end
res
end
end

start_date = Date.parse( ‘Thu Jun 11 13:14:44 -0700 2009’ )
start_date.thursdays_upto_now.each{|thursday| puts thursday.to_s} # or
whatever

I was happy finding out Date has a step method.

hth, I had some fun,

Siep

much better then my final code. I guess my mind is still stuck in
FORTRAN method of coding. I’m finding Ruby very nice and interesting.
Lots of new tricks to learn.

Thanks
Eric

today = Date.today
puts( today + 4 - today.wday)

…no need for the while with the -=. In fact won’t the -= always
give you the previous Thursday? The #step I didn’t know about. Good
advice.

I didn’t test the above code, but I’m pretty sure it will work for all
edge cases.

Sorry, should have been more clear. This is simply a demonstration of
how to find the current week’s Thursday without a loop.

The rest of Siep’s code makes sense. I’d probably end up doing
something crazy. In any case, the added method is probably obscure
enough to not affect the base class Date in other applications.

Todd

On Mon, Jul 13, 2009 at 10:42 AM, Eric
Peterson[email protected] wrote:

 res = []

start_date.thursdays_upto_now.each{|thursday| puts thursday.to_s} # or
FORTRAN method of coding. I’m finding Ruby very nice and interesting.
Lots of new tricks to learn.

today = Date.today
puts( today + 4 - today.wday)

…no need for the while with the -=. In fact won’t the -= always
give you the previous Thursday? The #step I didn’t know about. Good
advice.

I didn’t test the above code, but I’m pretty sure it will work for all
edge cases.

Todd