Files
irc-netcore-api/IRaCIS.Core.Application/Helper/OtherTool/HolidayHelper.cs
T
2024-10-22 15:30:38 +08:00

106 lines
3.4 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using RestSharp;
namespace IRaCIS.Core.Application.Helper;
#region 对接模型
public class HolidayCalendar
{
public string Name { get; set; }
public string Version { get; set; }
public string Generated { get; set; }
public string Timezone { get; set; }
public string Author { get; set; }
public string URL { get; set; }
public Dictionary<string, List<HolidayYear>> Years { get; set; }
}
public class HolidayYear
{
public string Name { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int Duration { get; set; }
public List<DateTime> CompDays { get; set; }
public string URL { get; set; }
public string Memo { get; set; }
}
#endregion
public class HolidayHelper
{
/// <summary>
/// github 链接:https://github.com/lanceliao/china-holiday-calender?tab=readme-ov-file 
/// 接口请求参考文档:https://www.koudingke.cn/docs/zh-Hans/net-lib-docs/latest/RestSharp/Usage/Usage
/// </summary>
private static RestClient _client => new RestClient("https://www.shuyz.com/githubfiles/china-holiday-calender/master/holidayAPI.json");
private static HolidayCalendar _holidayCalendar { get; set; }
private static List<DateTime> _holidays = new List<DateTime>();
private static List<DateTime> _compensatedWorkdays = new List<DateTime>();
static HolidayHelper()
{
_holidayCalendar = _client.Get<HolidayCalendar>(new RestRequest()) ?? new HolidayCalendar();
foreach (var year in _holidayCalendar.Years.Values.SelectMany(x => x))
{
// 添加节假日到假日集合
for (DateTime date = year.StartDate; date <= year.EndDate; date = date.AddDays(1))
{
_holidays.Add(date);
}
// 添加补班日到补班日集合
foreach (var compDay in year.CompDays)
{
_compensatedWorkdays.Add(compDay);
}
}
}
public static TimeSpan GetChinaWorkTimeSpan(DateTime startDate, DateTime endDate)
{
if (startDate > endDate)
{
throw new ArgumentException("结束日期必须大于或等于开始日期");
}
var diffTimeSpan = endDate - startDate;
// 初始化工作日数为0
int workdays = 0;
// 找出在给定日期范围内的假期和补班日
var filteredHolidays = _holidays.Where(d => d >= startDate && d <= endDate).ToList();
var filteredCompensatedWorkdays = _compensatedWorkdays.Where(d => d >= startDate && d <= endDate).ToList();
// 遍历日期范围,计算工作日 注意 这里是小于
for (DateTime date = startDate.Date; date < endDate.Date; date = date.AddDays(1))
{
// 判断是否为工作日
bool isWeekend = date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
bool isHoliday = filteredHolidays.Contains(date);
bool isCompensatedWorkday = filteredCompensatedWorkdays.Contains(date);
// 如果是补班日,或是平日(非假期,非周末),则算作工作日
if (isCompensatedWorkday || (!isWeekend && !isHoliday))
{
workdays++;
}
}
return new TimeSpan(workdays, diffTimeSpan.Hours, diffTimeSpan.Minutes, diffTimeSpan.Seconds);
}
}