【问题标题】:How to check business days in c#如何在c#中查看工作日
【发布时间】:2011-11-24 07:50:14
【问题描述】:

我的客户昨天给了我非常不同的要求。

我在服务器上有一个文件夹,每天有数千个文件传入。他要我写一个逻辑来检查文件的日期。如果文件夹中有超过 3 个工作日(周一至周五)的文件,那么他希望我删除这些文件

示例:如果星期六在文件夹中创建了任何文件,则该文件应在星期三删除,因为在这之间我们有星期六和星期日,这不应该算作工作日。

我的开发环境是 c# .NET 3.5

我认为我应该编写自定义方法。

请帮帮我。

【问题讨论】:

  • 你也介意假期吗?我的意思是,圣诞节、复活节和地区性假期?
  • 不,他们希望假期作为工作日。

标签: c# .net .net-3.5


【解决方案1】:

George Duckett 解决方案适合您。
只是为了帮助您,我发布了一个示例:

public static class DateExtensions
{
    public static bool IsBusinessDay(this DateTime date)
    {
        return
            date.DayOfWeek != DayOfWeek.Saturday &&
            date.DayOfWeek != DayOfWeek.Sunday;
    }
    public static int BusinessDaysTo(this DateTime fromDate, DateTime toDate, 
                                     int maxAllowed = 0)
    {
        int ret = 0;
        DateTime dt = fromDate;
        while (dt < toDate)
        {
            if (dt.IsBusinessDay()) ret++;
            if (maxAllowed > 0 && ret == maxAllowed) return ret;
            dt = dt.AddDays(1);
        }
        return ret;
    }
}

有了这个,你可以做这样的事情:

DateTime from = DateTime.Now.AddDays(-8);
int ret = from.BusinessDaysTo(DateTime.Now);
int ret2 = from.BusinessDaysTo(DateTime.Now.AddDays(5), 8);

【讨论】:

    【解决方案2】:
    1. 使用Directory.GetFiles枚举所有文件。

      Directory.GetFiles(directoryPath)
      
    2. 使用File.GetCreationTime获取文件的创建日期。

      File.GetCreationTime(filePath)
      
    3. 获取该日期与今天之间的工作日:

      Calculate the number of business days between two dates?this answer

    4. 编写一个 LINQ 查询以确定条件是否满足。

      var FilesToDelete =
          from filePath in Directory.GetFiles("folder")
          where File.GetCreationTime(filePath).BusinessDaysUntil(DateTime.Today) > 3
          select filePath;
      

    【讨论】:

    • 我会使用File.GetCreationTime,而不是为每个文件创建一个新的FileInfo
    【解决方案3】:
        public static bool IsBusinessDay(this DateTime value)
        {
            if (value.DayOfWeek == DayOfWeek.Sunday) return false;
            if (value.DayOfWeek == DayOfWeek.Saturday) return false;
    
            return true;
        }
    

    记得将扩展方法放在静态类中。

    【讨论】:

    • 这不能回答问题。
    【解决方案4】:

    只需调用:

    var cleaner = new FileCleaner();
    clenaer.DeleteOldFiles(@"C:\YourDiretory");
    

    代码

    public class FileCleaner
    {
        public IEnumerable<string> GetOldFiles(string directoryName)
        {
            var limit = DateTime.Now.AddBusinessDays(-3);
            return Directory.GetFiles(directoryName)
                            .Where(file => File.GetCreationTime(file) < limit);
        }
    
        public void DeleteOldFiles(string directoryName)
        {
            foreach (var filename in GetOldFiles(directoryName))
            {
                File.Delete(filename);
            }
        }
    }
    
    
    public static class DateTimeExtensions
    {
        public static bool IsBusinessDay(this DateTime instance)
        {
            return instance.DayOfWeek != DayOfWeek.Saturday 
                   && instance.DayOfWeek != DayOfWeek.Sunday;
        }
        public static DateTime AddBusinessDays(this DateTime instance, int days)
        {
            var newDate = instance;
            while (days > 0)
            {
                newDate = newDate.AddDays(-1);
                if (newDate.IsBusinessDay())
                    --days;
            }
            return newDate;
        }
    }
    

    【讨论】:

      【解决方案5】:

      如果您只需要文件创建日期,我不会使用 .NET 类,如果有数千个文件需要检查,它们会产生大量开销。

      根据我在一个目录中处理大量文件 (50k+) 并且需要性能的经验,我会使用 FindFirstFile FindNextFileFindClose 来加快处理速度并减少开销。

      WIN32_FIND_DATA findData;
      

      DateTime.FromFileTimeUtc((long)findData.ftLastWriteTime.dwLowDateTime + (long)findData.ftLastWriteTime.dwHighDateTime * 4294967296

      【讨论】:

      • 根据您的经验,上述代码比使用 .NET 类快多少?
      • @MEMark 我已经有一段时间没有确切的数字了。它的范围从 1 到 5,具体取决于文件的数量。
      猜你喜欢
      • 2017-08-19
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多