Home > OS >  Xamarin/XAML/C# - Build a method that counts all the weekend days in an array of DateTime's
Xamarin/XAML/C# - Build a method that counts all the weekend days in an array of DateTime's

Time:10-06

Problem: I am building an app to catalogue holiday leave days. The page I'm currently working on takes 2 dates from the user (start and end date) and then outputs the number of days selected, to be used elsewhere in the app.

Solution: Initially, I wanted to disable the selection of Saturdays and Sundays but the one solution floating around HERE did not work with my solution. My second thought was to instead take the date range the user selected and build an array of those days where I can count the number of Saturdays and Sundays and remove them from the full day total.

Here is the code behind class for the calculator page where I have created the 'removeWeekends' method. I just need some help figuring out how to scan this array for Saturdays and Sundays and then implement that with the page. Any help or advice is appreciated. Please let me know if I can provide more code or elaborate more on my issue.

    public Calculator()
    {
        InitializeComponent();
        
        OnDateSelected(null, null);
        dateFrom.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
        dateTo.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
    }

    public DateTime[] removeWeekends(DateTime a, DateTime b)
    {
        List<DateTime> allDates = new List<DateTime>();

        for (DateTime date = a; date <= b; date.AddDays(1))
            allDates.Add(date);

        return allDates.ToArray();
    }
   
    void OnDateSelected(object sender, DateChangedEventArgs e)
    {

        int days = (dateTo.Date - dateFrom.Date).Days;
        
        if(dateTo.Date < dateFrom.Date)
        {
            leaveDaysLabel.Text = "Days: Invalid Selection";
        }
        else if (dateTo.Date == dateFrom.Date)
        {
            leaveDaysLabel.Text = "Days: 1";
        }
        else
        {
            leaveDaysLabel.Text = String.Format("Days: {0}", days);

            leaveHoursLabel.Text = "Hours: "   (days * 7.4).ToString();
        }

    }

Result: Have implemented Jason's answer so code now likes like this:

 public partial class Calculator : ContentPage
{
    public Calculator()
    {
        InitializeComponent();
        
        OnDateSelected(null, null);
        dateFrom.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
        dateTo.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
    }

    public int removeWeekends(DateTime a, DateTime b)
    {
        List<DateTime> allDates = new List<DateTime>();

        for (DateTime date = a; date <= b; date.AddDays(1))
        {
            if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
            {
                allDates.Add(date);
            }
        }

        int weekendCount = allDates.Count();

        return weekendCount;
    }
   
    void OnDateSelected(object sender, DateChangedEventArgs e)
    {

        int count = removeWeekends(dateFrom.Date, dateTo.Date);

        int days = (dateTo.Date - dateFrom.Date).Days - count;
        
        if(dateTo.Date < dateFrom.Date)
        {
            leaveDaysLabel.Text = "Days: Invalid Selection";
        }
        else if (dateTo.Date == dateFrom.Date)
        {
            leaveDaysLabel.Text = "Days: 1";
        }
        else
        {
            leaveDaysLabel.Text = String.Format("Days: {0}", days);

            leaveHoursLabel.Text = "Hours: "   (days * 7.4).ToString();
        }

    }

Now run into LOS overflow issue when I enter the tab for this page. Anyone have any experience with this? Debug log below.

10-05 15:14:52.125 I/ViewRootImpl(21404): ViewRoot's Touch Event : ACTION_DOWN
10-05 15:14:52.218 I/ViewRootImpl(21404): ViewRoot's Touch Event : ACTION_UP
10-05 15:14:52.237 I/AudioManagerEx(21404): AudioManagerEx created
10-05 15:14:52.366 I/zygote64(21404): Do full code cache collection, code=107KB, data=89KB
10-05 15:14:52.366 I/zygote64(21404): After code cache collection, code=105KB, data=70KB
10-05 15:14:52.616 I/zygote64(21404): Explicit concurrent copying GC freed 21890(9MB) AllocSpace objects, 1(20KB) LOS objects, 90% free, 1235KB/13MB, paused 274us total 8.763ms
10-05 15:14:52.617 D/Mono    (21404): GC_TAR_BRIDGE bridges 139 objects 140 opaque 1 colors 139 colors-bridged 139 colors-visible 139 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.02ms tarjan 0.06ms scc-setup 0.02ms gather-xref 0.00ms xref-setup 0.01ms cleanup 0.04ms
10-05 15:14:52.617 D/Mono    (21404): GC_BRIDGE: Complete, was running for 10.06ms
10-05 15:14:52.617 D/Mono    (21404): GC_MINOR: (Concurrent start) time 6.23ms, stw 10.52ms promoted 940K major size: 1984K in use: 1240K los size: 17424K in use: 17168K
10-05 15:14:52.617 D/Mono    (21404): GC_MAJOR_CONCURRENT_START: (LOS overflow)
10-05 15:14:52.846 I/zygote64(21404): Explicit concurrent copying GC freed 649(38KB) AllocSpace objects, 0(0B) LOS objects, 91% free, 1213KB/13MB, paused 132us total 6.842ms
10-05 15:14:52.847 D/Mono    (21404): GC_TAR_BRIDGE bridges 62 objects 63 opaque 1 colors 62 colors-bridged 62 colors-visible 62 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.09ms tarjan 0.03ms scc-setup 0.02ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.05ms
10-05 15:14:52.847 D/Mono    (21404): GC_BRIDGE: Complete, was running for 8.09ms
10-05 15:14:52.847 D/Mono    (21404): GC_MAJOR_CONCURRENT_FINISH: (finishing) time 233.73ms, stw 14.81ms los size: 25608K in use: 24683K
10-05 15:14:52.847 D/Mono    (21404): GC_MAJOR_SWEEP: major size: 1984K in use: 948K
10-05 15:14:52.850 D/Mono    (21404): Requesting loading reference 1 (of 2) of Java.Interop.dll
10-05 15:14:52.851 D/Mono    (21404): Loading reference 1 of Java.Interop.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
10-05 15:14:52.851 D/Mono    (21404): Assembly Ref addref Java.Interop[0x79fce51500] -> System.Core[0x7a0cbee180]: 5
10-05 15:14:53.267 D/Mono    (21404): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 62 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.09ms tarjan 0.03ms scc-setup 0.02ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.00ms
10-05 15:14:53.267 D/Mono    (21404): GC_BRIDGE: Complete, was running for 0.13ms
10-05 15:14:53.267 D/Mono    (21404): GC_MINOR: (Concurrent start) time 0.69ms, stw 5.17ms promoted 0K major size: 1984K in use: 949K los size: 58380K in use: 57451K
10-05 15:14:53.267 D/Mono    (21404): GC_MAJOR_CONCURRENT_START: (LOS overflow)
10-05 15:14:54.108 I/zygote64(21404): Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 91% free, 1213KB/13MB, paused 129us total 5.950ms
10-05 15:14:54.108 D/Mono    (21404): GC_TAR_BRIDGE bridges 58 objects 59 opaque 1 colors 58 colors-bridged 58 colors-visible 58 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.02ms scc-setup 0.03ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.02ms
10-05 15:14:54.108 D/Mono    (21404): GC_BRIDGE: Complete, was running for 6.91ms
10-05 15:14:54.108 D/Mono    (21404): GC_MAJOR_CONCURRENT_FINISH: (finishing) time 836.63ms, stw 6.62ms los size: 99336K in use: 98411K
10-05 15:14:54.108 D/Mono    (21404): GC_MAJOR_SWEEP: major size: 1984K in use: 939K
10-05 15:14:55.768 D/Mono    (21404): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 58 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.02ms scc-setup 0.03ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.00ms
10-05 15:14:55.768 D/Mono    (21404): GC_BRIDGE: Complete, was running for 0.13ms
10-05 15:14:55.768 D/Mono    (21404): GC_MINOR: (Concurrent start) time 0.65ms, stw 7.14ms promoted 0K major size: 1984K in use: 939K los size: 230412K in use: 229483K
10-05 15:14:55.768 D/Mono    (21404): GC_MAJOR_CONCURRENT_START: (LOS overflow)
10-05 15:14:59.116 I/zygote64(21404): Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 91% free, 1213KB/13MB, paused 115us total 6.849ms
10-05 15:14:59.117 D/Mono    (21404): GC_TAR_BRIDGE bridges 58 objects 59 opaque 1 colors 58 colors-bridged 58 colors-visible 58 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.02ms tarjan 0.02ms scc-setup 0.07ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.02ms
10-05 15:14:59.117 D/Mono    (21404): GC_BRIDGE: Complete, was running for 7.92ms
10-05 15:14:59.117 D/Mono    (21404): GC_MAJOR_CONCURRENT_FINISH: (finishing) time 3341.47ms, stw 15.83ms los size: 394248K in use: 393323K
10-05 15:14:59.117 D/Mono    (21404): GC_MAJOR_SWEEP: major size: 1984K in use: 937K

CodePudding user response:

in your loop, just check the DayOfWeek before adding it to your list

for (DateTime date = a; date <= b; date.AddDays(1))
{ 
    if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)          
    { 
        allDates.Add(date);
    }
}
  • Related