calculating an array of dates

Discussion in 'Ruby' started by Eric Peterson, Jul 10, 2009.

  1. 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/.
     
    Eric Peterson, Jul 10, 2009
    #1
    1. Advertising

  2. 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/.
     
    Siep Korteling, Jul 11, 2009
    #2
    1. Advertising

  3. 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/.
     
    Eric Peterson, Jul 13, 2009
    #3
  4. Eric Peterson

    Todd Benson Guest

    On Mon, Jul 13, 2009 at 10:42 AM, Eric
    Peterson<> 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
     
    Todd Benson, Jul 13, 2009
    #4
  5. Eric Peterson

    Todd Benson Guest

    > 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
     
    Todd Benson, Jul 13, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    881
    Rob Schieber
    Sep 30, 2005
  2. Toine

    Calculating future dates

    Toine, Feb 2, 2007, in forum: Python
    Replies:
    4
    Views:
    278
    Ben Finney
    Feb 2, 2007
  3. celsius
    Replies:
    2
    Views:
    497
    Stan Milam
    Jan 5, 2005
  4. clintonb
    Replies:
    9
    Views:
    901
    James Kanze
    May 23, 2007
  5. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    209
    Mark Schupp
    Aug 9, 2004
Loading...

Share This Page