I''m trying to update the ''date_due'' attribute of a ''client'' object with the following function: new_date_due = @client.date_due + 1.month @ client.update_attribute(: date_due, new_date_due) Instead of simply advancing date_due a month from it''s present date (2007-11-11) it advances it 7 millennia into the future (9104-07-08). Given the rate the dollar''s falling, my clients probably can''t afford to wait that long to collect payments. I''m guessing there''s something wrong with the date formats I''m trying to add. Any idea how I could fix this? Thanks, Peter -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Try this: @client.update_attribute(: date_due, new_date_due.to_s(:db)) -Bill On Nov 11, 2007, at 5:37 PM, Peter Marks wrote:> > I''m trying to update the ''date_due'' attribute of a ''client'' object > with > the following function: > > new_date_due = @client.date_due + 1.month > @ client.update_attribute(: date_due, new_date_due) > > Instead of simply advancing date_due a month from it''s present date > (2007-11-11) it advances it 7 millennia into the future (9104-07-08). > Given the rate the dollar''s falling, my clients probably can''t > afford to > wait that long to collect payments. I''m guessing there''s something > wrong > with the date formats I''m trying to add. Any idea how I could fix > this? > > Thanks, > > Peter > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
William Pratt wrote:> Try this: > > @client.update_attribute(: date_due, new_date_due.to_s(:db)) > > -BillSame result I''m afraid. But thanks for the idea Bill. For what it''s worth, I''m using a MySQL db with this app. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Try this @ client.update_attribute(:date_due => new_date_due) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That is odd. I am using MySQL as well and I have no issues using the same code. If you send the new_date_due to the log using: logger.debug "The new date is #{new_date_due.to_s(:long)}" Do you see the proper date being logged? That will at least tell us if the problem is in your date calculation or in the update_attribute. -Bill On Nov 11, 2007, at 7:28 PM, Peter Marks wrote:> > William Pratt wrote: >> Try this: >> >> @client.update_attribute(: date_due, new_date_due.to_s(:db)) >> >> -Bill > > Same result I''m afraid. But thanks for the idea Bill. > > For what it''s worth, I''m using a MySQL db with this app. > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Abhi Manyu wrote:> Try this > > @ client.update_attribute(:date_due => new_date_due)@client.update_attribute(:date_due => new_date_due) is actually what I had (typoed on the colin space), but thanks Abhi. It looks like my problem is in the "+ 30.days" part as this code returns as 9104-07-08 in my view. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yes, I was just looking at that. The + method for dates takes an argument of days, so I bet this works:> new_date_due = @client.date_due + 30 > @client.update_attribute(: date_due, new_date_due)-Bill On Nov 11, 2007, at 7:47 PM, Peter Marks wrote:> > Abhi Manyu wrote: >> Try this >> >> @ client.update_attribute(:date_due => new_date_due) > > @client.update_attribute(:date_due => new_date_due) > > is actually what I had (typoed on the colin space), but thanks Abhi. > > It looks like my problem is in the "+ 30.days" part as this code > returns > as 9104-07-08 in my view. > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi one more thing have u used in the code like @client = "database name".find("id") (fill the database name and primary key(id)). I think then only will it be updated -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi one more thing have u used in the code like @client = "database name".find("id") (fill the database name and primary key(id)). I think then only will it be updated Any way all the best and please tell what was the prob when you find solution -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If you need it one month to the day rather than x days, this works old_due = @client.date_due @client.update_attribute(: date_due, Date.new(old_due.year, old_due.month + 1, old_due.day)) -Bill On Nov 11, 2007, at 7:47 PM, Peter Marks wrote:> > Abhi Manyu wrote: >> Try this >> >> @ client.update_attribute(:date_due => new_date_due) > > @client.update_attribute(:date_due => new_date_due) > > is actually what I had (typoed on the colin space), but thanks Abhi. > > It looks like my problem is in the "+ 30.days" part as this code > returns > as 9104-07-08 in my view. > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks guys. The "+ 30.days" was indeed the problem. "+ 30", as you suggested Bill, took care of that. Ideally I would like to advance date_due in terms of months (not sure if that''s practical) and limit the initial date_due to between the 1st and the 28th of a month. Anyone know of a clean way to do this? Thanks again for the help guys. This will work for now anyway :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
William Pratt wrote:> If you need it one month to the day rather than x days, this works > > old_due = @client.date_due > @client.update_attribute(: date_due, Date.new(old_due.year, > old_due.month + 1, old_due.day)) > > -BillHa, you read my mind! This is a great start. I''ll need to tweak this to manage a new year, but thanks! Peter -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Marks wrote:> William Pratt wrote: >> If you need it one month to the day rather than x days, this works >> >> old_due = @client.date_due >> @client.update_attribute(: date_due, Date.new(old_due.year, >> old_due.month + 1, old_due.day)) >> >> -Bill > > Ha, you read my mind! This is a great start. I''ll need to tweak this to > manage a new year, but thanks! > > PeterPerhaps not the most elegant chunk of code, but this seems to work: old_due = @ client.date_due if old_due.month == 12 @client.update_attribute(:date_due, Date.new(old_due.year + 1, 1, old_due.day)) else @client.update_attribute(:date_due, Date.new(old_due.year, old_due.month + 1, old_due.day)) end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yep, I was just writing up another email about the year bug I added. I do something similar when generating saved reports using date ranges. I use mysql''s built in date functions like this one: select DATE_ADD(''2007-01-02'', INTERVAL 1 MONTH); You can see a full list here: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_adddate I actually pass in a string something like: Client.update(@client.id, "date_due = DATE_ADD(''#{@client.date_due.to_s(:db)}'', INTERVAL 1 MONTH)") I don''t actually use it with update_attributes, but rather with a select, but this should work. Note that update skips any validation or other callbacks, but if that doesn''t matter and you don''t care that this ties you to mysql, this will do what you want. There are bound to be other ways, but this is pretty easy. -Bill On Nov 11, 2007, at 8:15 PM, Peter Marks wrote:> > William Pratt wrote: >> If you need it one month to the day rather than x days, this works >> >> old_due = @client.date_due >> @client.update_attribute(: date_due, Date.new(old_due.year, >> old_due.month + 1, old_due.day)) >> >> -Bill > > Ha, you read my mind! This is a great start. I''ll need to tweak this > to > manage a new year, but thanks! > > Peter > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
William Pratt wrote:> Yep, I was just writing up another email about the year bug I added. I > do something similar when generating saved reports using date ranges. > I use mysql''s built in date functions like this one: > > select DATE_ADD(''2007-01-02'', INTERVAL 1 MONTH); > > You can see a full list here: > http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_adddate > > I actually pass in a string something like: > > Client.update(@client.id, "date_due > DATE_ADD(''#{@client.date_due.to_s(:db)}'', INTERVAL 1 MONTH)") > > I don''t actually use it with update_attributes, but rather with a > select, but this should work. Note that update skips any validation or > other callbacks, but if that doesn''t matter and you don''t care that > this ties you to mysql, this will do what you want. There are bound to > be other ways, but this is pretty easy. > > -BillNice Bill. I''ll play around with that. Thanks for your continued support and advice. Because of people like you, my first rails app (which is far too large and ambitious) is coming along great. -Peter -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you Mr.William Pratt for sharing that great link with us. I have been reading your posts and hope you will continue this good work of replying the post in coming days -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12 Nov 2007, at 04:22, Peter Marks wrote:> > Perhaps not the most elegant chunk of code, but this seems to work: >> old_due = @ client.date_due > if old_due.month == 12 > @client.update_attribute(:date_due, Date.new(old_due.year + 1, 1, > old_due.day)) > else > @client.update_attribute(:date_due, Date.new(old_due.year, > old_due.month + 1, old_due.day)) > endI think i''d rather do new_due = old_due >> 1 If you are going to roll your own version you need to worry about things like adding a month to the 31st of august (your code will try and set the date to the 31st of september, which will fail. :-) Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, thats great. I knew there had to be a way to do it, but I have missed that operator when looking at the docs. Thanks again. -Bill Frederick Cheung wrote:> On 12 Nov 2007, at 04:22, Peter Marks wrote: > >> Perhaps not the most elegant chunk of code, but this seems to work: >> >> > > > >> old_due = @ client.date_due >> if old_due.month == 12 >> @client.update_attribute(:date_due, Date.new(old_due.year + 1, 1, >> old_due.day)) >> else >> @client.update_attribute(:date_due, Date.new(old_due.year, >> old_due.month + 1, old_due.day)) >> end >> > > I think i''d rather do > new_due = old_due >> 1 > > If you are going to roll your own version you need to worry about > things like adding a month to the 31st of august (your code will try > and set the date to the 31st of september, which will fail. > :-) > > Fred > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12 Nov 2007, at 03:47, Peter Marks wrote:> > It looks like my problem is in the "+ 30.days" part as this code > returns > as 9104-07-08 in my view.That is definitely the problem 30.days evaluates to the number of seconds in 30 days (ie 30*86400 = 2592000) This is great when you''re working with an instance of Time because + is used to mean ''add this many seconds''. However, for Date, + means ''add this many days''. So Date.new(2007,11,12) + 30.days is actually add 2592000 days (around 7100 years). Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 11, 2007 10:58 PM, Peter Marks <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Thanks guys. The "+ 30.days" was indeed the problem. "+ 30", as you > suggested Bill, took care of that. Ideally I would like to advance > date_due in terms of months (not sure if that''s practical) and limit the > initial date_due to between the 1st and the 28th of a month. Anyone know > of a clean way to do this? > > Thanks again for the help guys. This will work for now anyway :)Try new_date_due = @client.date_due >> 1 Date#+ takes a number of days as the argument. Date#>> returns a new date "right shifted" some number of months. use Date#<< to get a date some number of months ago. shadowfax:~/Documents/terralien/dltsolutions/enrollnow rick$ qri "Date#>>" ---------------------------------------------------------------- Date#>> >>(n) ------------------------------------------------------------------------ Return a new Date object that is n months later than the current one. If the day-of-the-month of the current Date is greater than the last day of the target month, the day-of-the-month of the returned Date will be the last day of the target month. shadowfax:~/Documents/terralien/dltsolutions/enrollnow rick$ qri "Date#<<" ---------------------------------------------------------------- Date#<< <<(n) ------------------------------------------------------------------------ Return a new Date object that is n months earlier than the current one. If the day-of-the-month of the current Date is greater than the last day of the target month, the day-of-the-month of the returned Date will be the last day of the target month. shadowfax:~/Documents/terralien/dltsolutions/enrollnow rick$ qri "Date#+" ----------------------------------------------------------------- Date#+ +(n) ------------------------------------------------------------------------ Return a new Date object that is n days later than the current one. n may be a negative value, in which case the new Date is earlier than the current one; however, #-() might be more intuitive. If n is not a Numeric, a TypeError will be thrown. In particular, two Dates cannot be added to each other. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> I think i''d rather do > new_due = old_due >> 1 > > If you are going to roll your own version you need to worry about > things like adding a month to the 31st of august (your code will try > and set the date to the 31st of september, which will fail. > :-) > > FredThat''s awesome! I knew there would be a clean way for this. This is much better than validating the initial due date value to be between the 1st and 28th of the month :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
robert.beene-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Nov-15 12:33 UTC
Re: Trouble advancing date attribute
Although this has been cleared up, there is the further issue related to just moving a Time object 30.days because of the differences in month length. I don''t believe anyone has mentioned the Rails magic of "next_month" and "previous_month". Here is some stuff from console that shows what I''m talking about. The first set uses Date. You''ll see that adding months doesn''t work but days does. # set time to current time>> time = Time.now.utc=> Mon Nov 12 12:32:07 UTC 2007 # make a date object with the previously set time>> x = Date.parse("#{time}")=> #<Date: 4908833/2,0,2299161>>> x.strftime("%D")=> "11/12/07" # attempt to add 1.months to x>> y = x + 1.months=> #<Date: 10092833/2,0,2299161>>> y.strftime("%D")=> "07/09/04" # attempt to add 30 to x # this works because it is expecting days. # this could work for now, but the time calculations you''ll have to # perform later make this a hassle as you might have been finding out>> z = x + 30=> #<Date: 4908893/2,0,2299161>>> z.strftime("%D")=> "12/12/07" __________________________________________________ # using time objects # set a to current time>> a = Time.parse("#{time}")=> Mon Nov 12 12:32:07 UTC 2007 # add 1.months to a, it works no problem>> b = a + 1.months=> Wed Dec 12 12:32:07 UTC 2007>> b.strftime("%D")=> "12/12/07" # add 30.days to a # this also works no problem.>> c = a + 30.days=> Wed Dec 12 12:32:07 UTC 2007>> c.strftime("%D")=> "12/12/07" Having said that, you''ll still run into issues when you are adding months or days because of the different number of days in a given month varies. Your best option is to use @object.date_due.next_month or @object.date.previous_month. This will avoid those issues. Here is an example. As you can see, January 31st + 1.months results in March. January 31st + 30.days is also in March. However, January 31st.next_month is Feb 28th.>> a = Time.parse("01/31/2007")=> Wed Jan 31 00:00:00 EST 2007>> b = a + 1.months=> Fri Mar 02 00:00:00 EST 2007>> c = a + 30.days=> Fri Mar 02 00:00:00 EST 2007>> d = a.next_month=> Wed Feb 28 00:00:00 EST 2007 You have to be very careful with time. Also, I might add to avoid any issue with Daylight Savings Time, you should be using UTC as your time based an adjust for your local time zone. On Nov 12, 10:40 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 12 Nov 2007, at 03:47, Peter Marks wrote: > > > > > It looks like my problem is in the "+ 30.days" part as this code > > returns > > as 9104-07-08 in my view. > > That is definitely the problem > > 30.days evaluates to the number of seconds in 30 days (ie 30*86400 = > 2592000) > > This is great when you''re working with an instance of Time because + > is used to mean ''add this many seconds''. However, for Date, + means > ''add this many days''. > So Date.new(2007,11,12) + 30.days is actually add 2592000 days (around > 7100 years). > > Fred--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yep, we found that around the beginning of the thread and Fredrick pointed out the << and >> methods of Date which does exactly what he wanted. I didn''t know about previous_month or next_month. They are definitely more clear and self documenting, although << and >> are part of ruby base so they will work w/ calculations outside of rails. Looks to me like they do the exact same thing. -Bill robert.beene-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Although this has been cleared up, there is the further issue related > to just moving a Time object 30.days because of the differences in > month length. I don''t believe anyone has mentioned the Rails magic of > "next_month" and "previous_month". > > Here is some stuff from console that shows what I''m talking about. The > first set uses Date. You''ll see that adding months doesn''t work but > days does. > > # set time to current time > >>> time = Time.now.utc >>> > => Mon Nov 12 12:32:07 UTC 2007 > > # make a date object with the previously set time > >>> x = Date.parse("#{time}") >>> > => #<Date: 4908833/2,0,2299161> > >>> x.strftime("%D") >>> > => "11/12/07" > > # attempt to add 1.months to x > > >>> y = x + 1.months >>> > => #<Date: 10092833/2,0,2299161> > >>> y.strftime("%D") >>> > => "07/09/04" > > # attempt to add 30 to x > # this works because it is expecting days. > # this could work for now, but the time calculations you''ll have to > # perform later make this a hassle as you might have been finding out > >>> z = x + 30 >>> > => #<Date: 4908893/2,0,2299161> > >>> z.strftime("%D") >>> > => "12/12/07" > > __________________________________________________ > # using time objects > > # set a to current time > >>> a = Time.parse("#{time}") >>> > => Mon Nov 12 12:32:07 UTC 2007 > > # add 1.months to a, it works no problem > >>> b = a + 1.months >>> > => Wed Dec 12 12:32:07 UTC 2007 > >>> b.strftime("%D") >>> > => "12/12/07" > > # add 30.days to a > # this also works no problem. > >>> c = a + 30.days >>> > => Wed Dec 12 12:32:07 UTC 2007 > >>> c.strftime("%D") >>> > => "12/12/07" > > Having said that, you''ll still run into issues when you are adding > months or days because of the different number of days in a given > month varies. Your best option is to use @object.date_due.next_month > or @object.date.previous_month. This will avoid those issues. > > Here is an example. As you can see, January 31st + 1.months results in > March. January 31st + 30.days is also in March. However, January > 31st.next_month is Feb 28th. > > >>> a = Time.parse("01/31/2007") >>> > => Wed Jan 31 00:00:00 EST 2007 > >>> b = a + 1.months >>> > => Fri Mar 02 00:00:00 EST 2007 > >>> c = a + 30.days >>> > => Fri Mar 02 00:00:00 EST 2007 > >>> d = a.next_month >>> > => Wed Feb 28 00:00:00 EST 2007 > > You have to be very careful with time. Also, I might add to avoid any > issue with Daylight Savings Time, you should be using UTC as your time > based an adjust for your local time zone. > > > On Nov 12, 10:40 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > >> On 12 Nov 2007, at 03:47, Peter Marks wrote: >> >> >> >> >>> It looks like my problem is in the "+ 30.days" part as this code >>> returns >>> as 9104-07-08 in my view. >>> >> That is definitely the problem >> >> 30.days evaluates to the number of seconds in 30 days (ie 30*86400 = >> 2592000) >> >> This is great when you''re working with an instance of Time because + >> is used to mean ''add this many seconds''. However, for Date, + means >> ''add this many days''. >> So Date.new(2007,11,12) + 30.days is actually add 2592000 days (around >> 7100 years). >> >> Fred >> > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hmm, is next_month and previous_month something you added or something added by a plugin you are using. I grepped my rails directory and I get nothing back. I tried it in console and get a no method error. I tried it in a view, same undefined method, however, << and >> work everytime. I also search the docs and the api docs, and nothing. Date.today >> 1 = 2007-12-15 -Bill William Pratt wrote:> Yep, we found that around the beginning of the thread and Fredrick > pointed out the << and >> methods of Date which does exactly what he > wanted. I didn''t know about previous_month or next_month. They are > definitely more clear and self documenting, although << and >> are > part of ruby base so they will work w/ calculations outside of rails. > Looks to me like they do the exact same thing. > > -Bill > > robert.beene-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >> Although this has been cleared up, there is the further issue related >> to just moving a Time object 30.days because of the differences in >> month length. I don''t believe anyone has mentioned the Rails magic of >> "next_month" and "previous_month". >> >> Here is some stuff from console that shows what I''m talking about. The >> first set uses Date. You''ll see that adding months doesn''t work but >> days does. >> >> # set time to current time >> >>>> time = Time.now.utc >>>> >> => Mon Nov 12 12:32:07 UTC 2007 >> >> # make a date object with the previously set time >> >>>> x = Date.parse("#{time}") >>>> >> => #<Date: 4908833/2,0,2299161> >> >>>> x.strftime("%D") >>>> >> => "11/12/07" >> >> # attempt to add 1.months to x >> >> >>>> y = x + 1.months >>>> >> => #<Date: 10092833/2,0,2299161> >> >>>> y.strftime("%D") >>>> >> => "07/09/04" >> >> # attempt to add 30 to x >> # this works because it is expecting days. >> # this could work for now, but the time calculations you''ll have to >> # perform later make this a hassle as you might have been finding out >> >>>> z = x + 30 >>>> >> => #<Date: 4908893/2,0,2299161> >> >>>> z.strftime("%D") >>>> >> => "12/12/07" >> >> __________________________________________________ >> # using time objects >> >> # set a to current time >> >>>> a = Time.parse("#{time}") >>>> >> => Mon Nov 12 12:32:07 UTC 2007 >> >> # add 1.months to a, it works no problem >> >>>> b = a + 1.months >>>> >> => Wed Dec 12 12:32:07 UTC 2007 >> >>>> b.strftime("%D") >>>> >> => "12/12/07" >> >> # add 30.days to a >> # this also works no problem. >> >>>> c = a + 30.days >>>> >> => Wed Dec 12 12:32:07 UTC 2007 >> >>>> c.strftime("%D") >>>> >> => "12/12/07" >> >> Having said that, you''ll still run into issues when you are adding >> months or days because of the different number of days in a given >> month varies. Your best option is to use @object.date_due.next_month >> or @object.date.previous_month. This will avoid those issues. >> >> Here is an example. As you can see, January 31st + 1.months results in >> March. January 31st + 30.days is also in March. However, January >> 31st.next_month is Feb 28th. >> >> >>>> a = Time.parse("01/31/2007") >>>> >> => Wed Jan 31 00:00:00 EST 2007 >> >>>> b = a + 1.months >>>> >> => Fri Mar 02 00:00:00 EST 2007 >> >>>> c = a + 30.days >>>> >> => Fri Mar 02 00:00:00 EST 2007 >> >>>> d = a.next_month >>>> >> => Wed Feb 28 00:00:00 EST 2007 >> >> You have to be very careful with time. Also, I might add to avoid any >> issue with Daylight Savings Time, you should be using UTC as your time >> based an adjust for your local time zone. >> >> >> On Nov 12, 10:40 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >> >>> On 12 Nov 2007, at 03:47, Peter Marks wrote: >>> >>> >>> >>> >>>> It looks like my problem is in the "+ 30.days" part as this code >>>> returns >>>> as 9104-07-08 in my view. >>>> >>> That is definitely the problem >>> >>> 30.days evaluates to the number of seconds in 30 days (ie 30*86400 = >>> 2592000) >>> >>> This is great when you''re working with an instance of Time because + >>> is used to mean ''add this many seconds''. However, for Date, + means >>> ''add this many days''. >>> So Date.new(2007,11,12) + 30.days is actually add 2592000 days (around >>> 7100 years). >>> >>> Fred >>> >> >> > > -- > Sincerely, > > William Pratt > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 15 Nov 2007, at 16:23, William Pratt wrote:> Hmm, is next_month and previous_month something you added or > something added by a plugin you are using. I grepped my rails > directory and I get nothing back. I tried it in console and get a no > method error. I tried it in a view, same undefined method, however, > << and >> work everytime. I also search the docs and the api docs, > and nothing. >They are part of active support, and are defined in Time only, not Date. Fred> Date.today >> 1 = 2007-12-15 > > -Bill > > William Pratt wrote: >> >> Yep, we found that around the beginning of the thread and Fredrick >> pointed out the << and >> methods of Date which does exactly what >> he wanted. I didn''t know about previous_month or next_month. They >> are definitely more clear and self documenting, although << and >> >> are part of ruby base so they will work w/ calculations outside of >> rails. Looks to me like they do the exact same thing. >> >> -Bill >> >> robert.beene-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >>> >>> Although this has been cleared up, there is the further issue >>> related >>> to just moving a Time object 30.days because of the differences in >>> month length. I don''t believe anyone has mentioned the Rails magic >>> of >>> "next_month" and "previous_month". >>> >>> Here is some stuff from console that shows what I''m talking about. >>> The >>> first set uses Date. You''ll see that adding months doesn''t work but >>> days does. >>> >>> # set time to current time >>> >>>>> time = Time.now.utc >>>>> >>> => Mon Nov 12 12:32:07 UTC 2007 >>> >>> # make a date object with the previously set time >>> >>>>> x = Date.parse("#{time}") >>>>> >>> => #<Date: 4908833/2,0,2299161> >>> >>>>> x.strftime("%D") >>>>> >>> => "11/12/07" >>> >>> # attempt to add 1.months to x >>> >>> >>>>> y = x + 1.months >>>>> >>> => #<Date: 10092833/2,0,2299161> >>> >>>>> y.strftime("%D") >>>>> >>> => "07/09/04" >>> >>> # attempt to add 30 to x >>> # this works because it is expecting days. >>> # this could work for now, but the time calculations you''ll have to >>> # perform later make this a hassle as you might have been finding >>> out >>> >>>>> z = x + 30 >>>>> >>> => #<Date: 4908893/2,0,2299161> >>> >>>>> z.strftime("%D") >>>>> >>> => "12/12/07" >>> >>> __________________________________________________ >>> # using time objects >>> >>> # set a to current time >>> >>>>> a = Time.parse("#{time}") >>>>> >>> => Mon Nov 12 12:32:07 UTC 2007 >>> >>> # add 1.months to a, it works no problem >>> >>>>> b = a + 1.months >>>>> >>> => Wed Dec 12 12:32:07 UTC 2007 >>> >>>>> b.strftime("%D") >>>>> >>> => "12/12/07" >>> >>> # add 30.days to a >>> # this also works no problem. >>> >>>>> c = a + 30.days >>>>> >>> => Wed Dec 12 12:32:07 UTC 2007 >>> >>>>> c.strftime("%D") >>>>> >>> => "12/12/07" >>> >>> Having said that, you''ll still run into issues when you are adding >>> months or days because of the different number of days in a given >>> month varies. Your best option is to use @object.date_due.next_month >>> or @object.date.previous_month. This will avoid those issues. >>> >>> Here is an example. As you can see, January 31st + 1.months >>> results in >>> March. January 31st + 30.days is also in March. However, January >>> 31st.next_month is Feb 28th. >>> >>> >>>>> a = Time.parse("01/31/2007") >>>>> >>> => Wed Jan 31 00:00:00 EST 2007 >>> >>>>> b = a + 1.months >>>>> >>> => Fri Mar 02 00:00:00 EST 2007 >>> >>>>> c = a + 30.days >>>>> >>> => Fri Mar 02 00:00:00 EST 2007 >>> >>>>> d = a.next_month >>>>> >>> => Wed Feb 28 00:00:00 EST 2007 >>> >>> You have to be very careful with time. Also, I might add to avoid >>> any >>> issue with Daylight Savings Time, you should be using UTC as your >>> time >>> based an adjust for your local time zone. >>> >>> >>> On Nov 12, 10:40 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>> wrote: >>> >>>> On 12 Nov 2007, at 03:47, Peter Marks wrote: >>>> >>>> >>>> >>>> >>>>> It looks like my problem is in the "+ 30.days" part as this code >>>>> returns >>>>> as 9104-07-08 in my view. >>>>> >>>> That is definitely the problem >>>> >>>> 30.days evaluates to the number of seconds in 30 days (ie >>>> 30*86400 >>>> 2592000) >>>> >>>> This is great when you''re working with an instance of Time >>>> because + >>>> is used to mean ''add this many seconds''. However, for Date, + means >>>> ''add this many days''. >>>> So Date.new(2007,11,12) + 30.days is actually add 2592000 days >>>> (around >>>> 7100 years). >>>> >>>> Fred >>>> >>> >> >> -- >> Sincerely, >> >> William Pratt >> >> >> > > -- > Sincerely, > > William Pratt > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Cool, thanks. I didn''t try Time since we were working with date objects. -Bill Frederick Cheung wrote:> On 15 Nov 2007, at 16:23, William Pratt wrote: > > >> Hmm, is next_month and previous_month something you added or >> something added by a plugin you are using. I grepped my rails >> directory and I get nothing back. I tried it in console and get a no >> method error. I tried it in a view, same undefined method, however, >> << and >> work everytime. I also search the docs and the api docs, >> and nothing. >> >> > They are part of active support, and are defined in Time only, not Date. > > Fred > >> Date.today >> 1 = 2007-12-15 >> >> -Bill >> >> William Pratt wrote: >> >>> Yep, we found that around the beginning of the thread and Fredrick >>> pointed out the << and >> methods of Date which does exactly what >>> he wanted. I didn''t know about previous_month or next_month. They >>> are definitely more clear and self documenting, although << and >> >>> are part of ruby base so they will work w/ calculations outside of >>> rails. Looks to me like they do the exact same thing. >>> >>> -Bill >>> >>> robert.beene-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: >>> >>>> Although this has been cleared up, there is the further issue >>>> related >>>> to just moving a Time object 30.days because of the differences in >>>> month length. I don''t believe anyone has mentioned the Rails magic >>>> of >>>> "next_month" and "previous_month". >>>> >>>> Here is some stuff from console that shows what I''m talking about. >>>> The >>>> first set uses Date. You''ll see that adding months doesn''t work but >>>> days does. >>>> >>>> # set time to current time >>>> >>>> >>>>>> time = Time.now.utc >>>>>> >>>>>> >>>> => Mon Nov 12 12:32:07 UTC 2007 >>>> >>>> # make a date object with the previously set time >>>> >>>> >>>>>> x = Date.parse("#{time}") >>>>>> >>>>>> >>>> => #<Date: 4908833/2,0,2299161> >>>> >>>> >>>>>> x.strftime("%D") >>>>>> >>>>>> >>>> => "11/12/07" >>>> >>>> # attempt to add 1.months to x >>>> >>>> >>>> >>>>>> y = x + 1.months >>>>>> >>>>>> >>>> => #<Date: 10092833/2,0,2299161> >>>> >>>> >>>>>> y.strftime("%D") >>>>>> >>>>>> >>>> => "07/09/04" >>>> >>>> # attempt to add 30 to x >>>> # this works because it is expecting days. >>>> # this could work for now, but the time calculations you''ll have to >>>> # perform later make this a hassle as you might have been finding >>>> out >>>> >>>> >>>>>> z = x + 30 >>>>>> >>>>>> >>>> => #<Date: 4908893/2,0,2299161> >>>> >>>> >>>>>> z.strftime("%D") >>>>>> >>>>>> >>>> => "12/12/07" >>>> >>>> __________________________________________________ >>>> # using time objects >>>> >>>> # set a to current time >>>> >>>> >>>>>> a = Time.parse("#{time}") >>>>>> >>>>>> >>>> => Mon Nov 12 12:32:07 UTC 2007 >>>> >>>> # add 1.months to a, it works no problem >>>> >>>> >>>>>> b = a + 1.months >>>>>> >>>>>> >>>> => Wed Dec 12 12:32:07 UTC 2007 >>>> >>>> >>>>>> b.strftime("%D") >>>>>> >>>>>> >>>> => "12/12/07" >>>> >>>> # add 30.days to a >>>> # this also works no problem. >>>> >>>> >>>>>> c = a + 30.days >>>>>> >>>>>> >>>> => Wed Dec 12 12:32:07 UTC 2007 >>>> >>>> >>>>>> c.strftime("%D") >>>>>> >>>>>> >>>> => "12/12/07" >>>> >>>> Having said that, you''ll still run into issues when you are adding >>>> months or days because of the different number of days in a given >>>> month varies. Your best option is to use @object.date_due.next_month >>>> or @object.date.previous_month. This will avoid those issues. >>>> >>>> Here is an example. As you can see, January 31st + 1.months >>>> results in >>>> March. January 31st + 30.days is also in March. However, January >>>> 31st.next_month is Feb 28th. >>>> >>>> >>>> >>>>>> a = Time.parse("01/31/2007") >>>>>> >>>>>> >>>> => Wed Jan 31 00:00:00 EST 2007 >>>> >>>> >>>>>> b = a + 1.months >>>>>> >>>>>> >>>> => Fri Mar 02 00:00:00 EST 2007 >>>> >>>> >>>>>> c = a + 30.days >>>>>> >>>>>> >>>> => Fri Mar 02 00:00:00 EST 2007 >>>> >>>> >>>>>> d = a.next_month >>>>>> >>>>>> >>>> => Wed Feb 28 00:00:00 EST 2007 >>>> >>>> You have to be very careful with time. Also, I might add to avoid >>>> any >>>> issue with Daylight Savings Time, you should be using UTC as your >>>> time >>>> based an adjust for your local time zone. >>>> >>>> >>>> On Nov 12, 10:40 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>> wrote: >>>> >>>> >>>>> On 12 Nov 2007, at 03:47, Peter Marks wrote: >>>>> >>>>> >>>>> >>>>> >>>>> >>>>>> It looks like my problem is in the "+ 30.days" part as this code >>>>>> returns >>>>>> as 9104-07-08 in my view. >>>>>> >>>>>> >>>>> That is definitely the problem >>>>> >>>>> 30.days evaluates to the number of seconds in 30 days (ie >>>>> 30*86400 >>>>> 2592000) >>>>> >>>>> This is great when you''re working with an instance of Time >>>>> because + >>>>> is used to mean ''add this many seconds''. However, for Date, + means >>>>> ''add this many days''. >>>>> So Date.new(2007,11,12) + 30.days is actually add 2592000 days >>>>> (around >>>>> 7100 years). >>>>> >>>>> Fred >>>>> >>>>> >>> -- >>> Sincerely, >>> >>> William Pratt >>> >>> >>> >>> >> -- >> Sincerely, >> >> William Pratt >> >> > > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---