namespace CommonLibrary
{
    using System;
    using System.Globalization;

    public static class DateUtil
    {
        private static readonly string[] Weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

        /// <summary>
        /// 得到本周第一天(以星期天为第一天)
        /// </summary>
        /// <param name="datetime">指定日期</param>
        /// <returns>返回本周以星期天为第一天的日期</returns>
        public static DateTime GetWeekFirstDaySun(DateTime datetime)
        {
            // 星期天为第一天
            int weeknow = Convert.ToInt32(datetime.DayOfWeek);
            int daydiff = (-1) * weeknow;

            // 本周第一天
            string firstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");

            return Convert.ToDateTime(firstDay);
        }

        /// <summary>
        /// 得到本周第一天(以星期一为第一天)
        /// </summary>
        /// <param name="datetime">指定日期</param>
        /// <returns>返回本周以星期一为第一天的日期</returns>
        public static DateTime GetWeekFirstDayMon(DateTime datetime)
        {
            // 星期一为第一天
            int weeknow = Convert.ToInt32(datetime.DayOfWeek);

            // 因为是以星期一为第一天，所以要判断weeknow等于0时，要向前推6天。
            weeknow = weeknow == 0 ? (7 - 1) : (weeknow - 1);
            int daydiff = (-1) * weeknow;

            // 本周第一天
            string firstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");

            return Convert.ToDateTime(firstDay);
        }

        /// <summary>
        /// 得到本周最后一天(以星期六为最后一天)
        /// </summary>
        /// <param name="datetime">指定日期</param>
        /// <returns>以星期六为最后一天的日期</returns>
        public static DateTime GetWeekLastDaySat(DateTime datetime)
        {
            // 星期六为最后一天
            int weeknow = Convert.ToInt32(datetime.DayOfWeek);
            int daydiff = (7 - weeknow) - 1;

            // 本周最后一天
            string lastDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");

            return Convert.ToDateTime(lastDay);
        }

        /// <summary>
        /// 得到本周最后一天(以星期天为最后一天)
        /// </summary>
        /// <param name="datetime">指定日期</param>
        /// <returns>以星期天为最后一天的日期</returns>
        public static DateTime GetWeekLastDaySun(DateTime datetime)
        {
            // 星期天为最后一天
            int weeknow = Convert.ToInt32(datetime.DayOfWeek);
            weeknow = weeknow == 0 ? 7 : weeknow;
            int daydiff = 7 - weeknow;

            // 本周最后一天
            string lastDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");

            return Convert.ToDateTime(lastDay);
        }

        /// <summary>
        /// 获取指定日期，在为一年中为第几周
        /// </summary>
        /// <param name="datetime">指定日期</param>
        /// <returns>返回第几周</returns>
        public static int GetWeekOfYear(DateTime datetime)
        {
            GregorianCalendar gc = new GregorianCalendar();
            int weekOfYear = gc.GetWeekOfYear(datetime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);

            return weekOfYear;
        }

        /// <summary>
        /// 获取中文星期
        /// </summary>
        /// <param name="datetime">指定日期</param>
        /// <returns>中文星期</returns>
        public static string GetWeekDayCN(DateTime datetime)
        {
            return Weekdays[Convert.ToInt32(datetime.DayOfWeek)];
        }
    }
}
