I have a table with dates start => 2013-02-04 end => 2013-02-13 I can count all days: def self.business_days(start, end) ((start)..(end)).select {|d| (1..5).include?(d.wday) }.size end => 8..o.k. but we have two cweek(s) in the above ex. => first cweek 5 business days (Mo - Fr), second 3 (Mo - Wed) I want to store the cweek + business days per week like this week_id, days => 60 , 5 => 61, 3 Tried (start_week_id..end_week_id).each { |week| Model.create(:week_id => week, :days => ???? end Need some inspiration how to find out the days per given cweek Thanks for support Werner -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/qzMu2MfCLWUJ. For more options, visit https://groups.google.com/groups/opt_out.
On Fri, Feb 8, 2013 at 11:35 AM, Werner <webagentur.laude-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> I have a table with dates > > start => 2013-02-04 > end => 2013-02-13 > > I can count all days: > def self.business_days(start, end) > ((start)..(end)).select {|d| (1..5).include?(d.wday) }.size > end > > => 8..o.k. > > but we have two cweek(s) > > in the above ex. => first cweek 5 business days (Mo - Fr), > second 3 (Mo - Wed) > > I want to store the cweek + business days per week like this > week_id, days > => 60 , 5 > => 61, 3 > > Tried > (start_week_id..end_week_id).each { |week| > Model.create(:week_id => week, :days => ???? > end > > > Need some inspiration how to find out the days per given cweek > > Thanks for support > WernerDo you have the starting date of each week? or are these weeks in the standard year, i.e., the week_num will be correct? If so, calculating the number of days per week in the span might go something like this pseudo-code: for each date from start_date to end_date if date is a weekday, then increment days_worked_this_week[date.week_num] end if end for days_worked_this_week will be an array with the accumulated days worked in that week. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Thanks for your input... I will check that.. Werner Am Samstag, 9. Februar 2013 23:46:20 UTC+1 schrieb tamouse:> > On Fri, Feb 8, 2013 at 11:35 AM, Werner <webagent...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org<javascript:>> > wrote: > > I have a table with dates > > > > start => 2013-02-04 > > end => 2013-02-13 > > > > I can count all days: > > def self.business_days(start, end) > > ((start)..(end)).select {|d| (1..5).include?(d.wday) }.size > > end > > > > => 8..o.k. > > > > but we have two cweek(s) > > > > in the above ex. => first cweek 5 business days (Mo - Fr), > > second 3 (Mo - Wed) > > > > I want to store the cweek + business days per week like this > > week_id, days > > => 60 , 5 > > => 61, 3 > > > > Tried > > (start_week_id..end_week_id).each { |week| > > Model.create(:week_id => week, :days => ???? > > end > > > > > > Need some inspiration how to find out the days per given cweek > > > > Thanks for support > > Werner > > Do you have the starting date of each week? or are these weeks in the > standard year, i.e., the week_num will be correct? > > If so, calculating the number of days per week in the span might go > something like this pseudo-code: > > for each date from start_date to end_date > > if date is a weekday, then > increment days_worked_this_week[date.week_num] > end if > > end for > > days_worked_this_week will be an array with the accumulated days > worked in that week. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/JfTaS45ZPlAJ. For more options, visit https://groups.google.com/groups/opt_out.
You might find the business_time and holidays gems useful. -- Benjamin Curtis http://honeybadger.io - Modern error tracking for Rails apps On Sunday, February 10, 2013 11:52:30 PM UTC-8, Werner wrote:> > Thanks for your input... > I will check that.. > Werner > > Am Samstag, 9. Februar 2013 23:46:20 UTC+1 schrieb tamouse: >> >> On Fri, Feb 8, 2013 at 11:35 AM, Werner <webagent...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> >> wrote: >> > I have a table with dates >> > >> > start => 2013-02-04 >> > end => 2013-02-13 >> > >> > I can count all days: >> > def self.business_days(start, end) >> > ((start)..(end)).select {|d| (1..5).include?(d.wday) }.size >> > end >> > >> > => 8..o.k. >> > >> > but we have two cweek(s) >> > >> > in the above ex. => first cweek 5 business days (Mo - Fr), >> > second 3 (Mo - Wed) >> > >> > I want to store the cweek + business days per week like this >> > week_id, days >> > => 60 , 5 >> > => 61, 3 >> > >> > Tried >> > (start_week_id..end_week_id).each { |week| >> > Model.create(:week_id => week, :days => ???? >> > end >> > >> > >> > Need some inspiration how to find out the days per given cweek >> > >> > Thanks for support >> > Werner >> >> Do you have the starting date of each week? or are these weeks in the >> standard year, i.e., the week_num will be correct? >> >> If so, calculating the number of days per week in the span might go >> something like this pseudo-code: >> >> for each date from start_date to end_date >> >> if date is a weekday, then >> increment days_worked_this_week[date.week_num] >> end if >> >> end for >> >> days_worked_this_week will be an array with the accumulated days >> worked in that week. >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/QlcE4bMmTuQJ. For more options, visit https://groups.google.com/groups/opt_out.