calculating an array of dates

E

Eric Peterson

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


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


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
 
S

Siep Korteling

Eric said:
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


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


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
 
E

Eric Peterson

Siep Korteling 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
 
T

Todd Benson

Siep Korteling 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
=A0 def thursdays_upto_now
=A0 =A0 =A0end_date =3D Date.today
=A0 =A0 =A0res =3D []
=A0 =A0 =A0# get the last thursday:
=A0 =A0 =A0end_date -=3D 1 while (end_date.wday !=3D 4)
=A0 =A0 =A0end_date.step(self, -7) do |date|
=A0 =A0 =A0 =A0res << date
=A0 =A0 =A0end
=A0 =A0 =A0res
=A0 =A0end
=A0end

start_date =3D 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. =A0I guess my mind is still stuck in
FORTRAN method of coding. =A0I'm finding Ruby very nice and interesting.
Lots of new tricks to learn.
...

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

...no need for the while with the -=3D. In fact won't the -=3D 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
 
T

Todd Benson

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

...no need for the while with the -=3D. =A0In fact won't the -=3D always
give you the previous Thursday? =A0The #step I didn't know about. =A0Good
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
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top