Xamarin/XAML/C# - 构建一个计算 DateTime 数组中所有周末天数的方法

Posted

技术标签:

【中文标题】Xamarin/XAML/C# - 构建一个计算 DateTime 数组中所有周末天数的方法【英文标题】:Xamarin/XAML/C# - Build a method that counts all the weekend days in an array of DateTime's 【发布时间】:2021-11-25 15:18:53 【问题描述】:

问题:我正在构建一个应用程序来记录假期天数。我目前正在处理的页面从用户那里获取 2 个日期(开始日期和结束日期),然后输出选择的天数,以便在应用程序的其他地方使用。

解决方案:最初,我想禁用周六和周日的选择,但 HERE 周围浮动的一个解决方案不适用于我的解决方案。我的第二个想法是取而代之的是用户选择的日期范围并构建一个数组,我可以在其中计算周六和周日的数量并将它们从全天总数中删除。

这是我创建“removeWeekends”方法的计算器页面的代码。我只需要一些帮助来弄清楚如何在周六和周日扫描这个数组,然后用页面实现它。任何帮助或建议表示赞赏。如果我可以提供更多代码或详细说明我的问题,请告诉我。

    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();
        

    

结果:已经实现了 Jason 的答案,所以现在的代码是这样的:

 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();
        

    

当我进入此页面的选项卡时,现在遇到 LOS 溢出问题。有人对此有经验吗?调试日志如下。

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

【问题讨论】:

【参考方案1】:

在您的循环中,只需检查DayOfWeek,然后再将其添加到您的列表中

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

【讨论】:

已将其添加到我的解决方案中,但遇到了 LOS 溢出问题。我知道这与垃圾收集有关,但不知道如何应对。 在为dateFromdateTo 设置任何值之前,您在构造函数中调用OnDateSelected 已将其注释掉,页面现在可以运行而不会崩溃。但是,当我尝试更改日期时,遇到相同的 LOS 溢出问题。有什么建议吗? 首先,您正在调用removeWeekends 您检查输入。除此之外,只需调试它。添加一些基本的控制台日志记录以检查 for 循环的输入以及执行次数等 看起来循环卡住了,没有增加日期。真的不明白为什么会发生这种情况,但会单独发布一篇文章来尝试找出答案。感谢您的帮助,将以上标记为答案。

以上是关于Xamarin/XAML/C# - 构建一个计算 DateTime 数组中所有周末天数的方法的主要内容,如果未能解决你的问题,请参考以下文章

VSTS 构建代理 - 一台计算机可以运行多个构建代理吗?

试图构建一个函数来计算图像中的唯一像素

构建一个 SQL 来计算与父级相关的行数

用 PHP 构建 OOP 计算器

构建时的链接器符号算术计算错误的结果

Vue3-前端构建工具(Vue-Cli和Vite) & 计算属性 & 侦听器