Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > calculating an array of dates

Reply
Thread Tools

calculating an array of dates

 
 
Eric Peterson
Guest
Posts: n/a
 
      07-10-2009

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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Siep Korteling
Guest
Posts: n/a
 
      07-10-2009
Eric Peterson 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Eric Peterson
Guest
Posts: n/a
 
      07-13-2009
Siep Korteling wrote:
<snip>
>
> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      07-13-2009
On Mon, Jul 13, 2009 at 10:42 AM, Eric
Peterson<(E-Mail Removed)> wrote:
> Siep Korteling wrote:
> <snip>
>>
>> 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

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      07-13-2009
> 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

 
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
Calculating number of days between two dates. clintonb C++ 9 05-23-2007 09:32 AM
Calculating future dates Toine Python 4 02-02-2007 02:21 AM
Dates dates dates dates... SQL and ASP.NET David Lozzi ASP .Net 1 09-30-2005 02:18 PM
formula used in calculating diffrence between dates celsius C Programming 2 01-05-2005 08:02 AM
Dates! Dates! Dates! PW ASP General 4 08-09-2004 04:42 PM



Advertisments