
namespace Hotline.Tools;
public static class DateTimeExtensions
{
    /// <summary>
    /// 获取指定日期所在周的开始时间和结束时间
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static (DateTime, DateTime) GetWeekStartAndEnd(this DateTime date)
    {
        int diff = date.DayOfWeek - DayOfWeek.Monday;
        if (diff < 0)
        {
            diff += 7;
        }

        var weekStart = date.AddDays(-1 * diff).Date;
        var weekEnd = weekStart.AddDays(7).AddTicks(-1);

        return (weekStart, weekEnd);
    }

    /// <summary>
    /// 获取指定日期所在天的开始时间和结束时间
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static (DateTime, DateTime) GetDayStartAndEnd(this DateTime date)
    {
        var dayStart = date.Date; 
        var dayEnd = dayStart.AddDays(1).AddTicks(-1); 
        return (dayStart, dayEnd);
    }

    /// <summary>
    /// 获取指定日期所在月的开始时间和结束时间
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static (DateTime, DateTime) GetMonthStartAndEnd(this DateTime date)
    {
        var monthStart = new DateTime(date.Year, date.Month, 1);
        var monthEnd = monthStart.AddMonths(1).AddTicks(-1);
        return (monthStart, monthEnd);
    }
}
