I want calculate the number of business day for the current month, but I don''t know how I skip the holidays and weekends. Could anyone help me with this? Thanks -- Posted via http://www.ruby-forum.com/.
Holidays you need to have defined somewhere, as they probably vary depending on the business you are dealing with at the time (Government versus private industry, etc). A brute force method would be to get the dates for the start of the month, the end of the month and step through the month from start to end by day. Check the day of the week for each date. Saturday or Sunday and it''s out. Match one of your holidays, and its out. What''s left are your business days. -- Posted via http://www.ruby-forum.com/.
Ar Chron wrote:> A brute force method would be to get the dates for the start of the > month, the end of the month and step through the month from start to end > by day. > > Check the day of the week for each date. Saturday or Sunday and it''s > out. Match one of your holidays, and its out. > > What''s left are your business days.Or create a set of all dates for the year, a set of all weekend dates for the year, and a set of all holidays for the year. Then intersect the three sets and you end up with business days. Now store that set in the database (after all there are only 365/366 days/year so it''s not going to be a load on the database). Now whenever you need to iterate though business days just grab the set from the database: Example: # Assume named_scope "days_in_month" on BusinessDays business_days = BusinessDays.days_in_month("2") business_days.each do |day| puts day.to_s end Of course don''t forget to regenerate your stored business days if holidays change. -- Posted via http://www.ruby-forum.com/.
For the holidays you are going to need to store them somewhere as was already pointed out in another post since they might even vary by company. Calculating the weekend days should be easy using the extended functionality for dates that Rails offers. You can check the ActiveSupport::CoreExtensions::DateTime::* and ActiveSupport::CoreExtensions::Date* modules for that. Good luck. On Sep 18, 2:17 pm, Penelope West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I want calculate the number of business day for the current month, but I > don''t know how I skip the holidays and weekends. > > Could anyone help me with this? > > Thanks > -- > Posted viahttp://www.ruby-forum.com/.
Sorry, I should also have added to take a look at ActiveSupport::CoreExtensions::Numeric::Time, which allows something like 1.week.from_now, 1.week.ago, etc. On Sep 19, 6:32 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:> For the holidays you are going to need to store them somewhere as was > already pointed out in another post since they might even vary by > company. Calculating the weekend days should be easy using the > extended functionality for dates that Rails offers. You can check the > ActiveSupport::CoreExtensions::DateTime::* and > ActiveSupport::CoreExtensions::Date* modules for that. > > Good luck. > > On Sep 18, 2:17 pm, Penelope West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > I want calculate the number of business day for the current month, but I > > don''t know how I skip the holidays and weekends. > > > Could anyone help me with this? > > > Thanks > > -- > > Posted viahttp://www.ruby-forum.com/.
Thanks for all help. Finally, I could take out all the holidays. It is hard for holidays that change every year. 2009/9/20 pepe <Pepe-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org>> > Sorry, I should also have added to take a look at > ActiveSupport::CoreExtensions::Numeric::Time, which allows something > like 1.week.from_now, 1.week.ago, etc. > > On Sep 19, 6:32 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote: > > For the holidays you are going to need to store them somewhere as was > > already pointed out in another post since they might even vary by > > company. Calculating the weekend days should be easy using the > > extended functionality for dates that Rails offers. You can check the > > ActiveSupport::CoreExtensions::DateTime::* and > > ActiveSupport::CoreExtensions::Date* modules for that. > > > > Good luck. > > > > On Sep 18, 2:17 pm, Penelope West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > > > > I want calculate the number of business day for the current month, but > I > > > don''t know how I skip the holidays and weekends. > > > > > Could anyone help me with this? > > > > > Thanks > > > -- > > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
We use a table of holidays here... So take your two dates.. Decide how many days are between them (You can just subtract the two date objects) Subtract how many weekend days are present between the two dates.. Subtract select count(*) from holidays where date <= begin and date => end Theres not much else out there thats pre-built.. On Mon, Sep 28, 2009 at 5:35 PM, Ludmila Moraes <ludmoraes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for all help. > > Finally, I could take out all the holidays. It is hard for holidays that > change every year. > > 2009/9/20 pepe <Pepe-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> > > >> Sorry, I should also have added to take a look at >> ActiveSupport::CoreExtensions::Numeric::Time, which allows something >> like 1.week.from_now, 1.week.ago, etc. >> >> On Sep 19, 6:32 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote: >> > For the holidays you are going to need to store them somewhere as was >> > already pointed out in another post since they might even vary by >> > company. Calculating the weekend days should be easy using the >> > extended functionality for dates that Rails offers. You can check the >> > ActiveSupport::CoreExtensions::DateTime::* and >> > ActiveSupport::CoreExtensions::Date* modules for that. >> > >> > Good luck. >> > >> > On Sep 18, 2:17 pm, Penelope West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> > wrote: >> > >> > > I want calculate the number of business day for the current month, but >> I >> > > don''t know how I skip the holidays and weekends. >> > >> > > Could anyone help me with this? >> > >> > > Thanks >> > > -- >> > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Look guys, I found a way to discover the holidays. Visit the site: code.dunae.ca/holidays have a gem holidays, this helped me a lot, and I just want share this information that I founded. 2009/9/29 Stephen <sblackstone-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> We use a table of holidays here... > > So take your two dates.. > > Decide how many days are between them > (You can just subtract the two date objects) > > Subtract how many weekend days are present between the two dates.. > > Subtract select count(*) from holidays where date <= begin and date => end > > Theres not much else out there thats pre-built.. > > > > On Mon, Sep 28, 2009 at 5:35 PM, Ludmila Moraes <ludmoraes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > >> Thanks for all help. >> >> Finally, I could take out all the holidays. It is hard for holidays that >> change every year. >> >> 2009/9/20 pepe <Pepe-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> >> >> >>> Sorry, I should also have added to take a look at >>> ActiveSupport::CoreExtensions::Numeric::Time, which allows something >>> like 1.week.from_now, 1.week.ago, etc. >>> >>> On Sep 19, 6:32 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote: >>> > For the holidays you are going to need to store them somewhere as was >>> > already pointed out in another post since they might even vary by >>> > company. Calculating the weekend days should be easy using the >>> > extended functionality for dates that Rails offers. You can check the >>> > ActiveSupport::CoreExtensions::DateTime::* and >>> > ActiveSupport::CoreExtensions::Date* modules for that. >>> > >>> > Good luck. >>> > >>> > On Sep 18, 2:17 pm, Penelope West <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >>> > wrote: >>> > >>> > > I want calculate the number of business day for the current month, >>> but I >>> > > don''t know how I skip the holidays and weekends. >>> > >>> > > Could anyone help me with this? >>> > >>> > > Thanks >>> > > -- >>> > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---