From: Eric Peterson Date: 2009-07-11T05:30:14+09:00 Subject: 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 -- Posted via http://www.ruby-forum.com/.