using ryCommon.Pram;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ryCommon
{
    /// <summary>
    /// 日期时间的操作类
    /// </summary>
   public class RyDate
    {
        /// <summary>
        /// 获取当前是周几,周一到周日，分别是1-7.
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int GetWeek_index(DateTime dt)
        {
            int week_index = dt.DayOfWeek.ToInt(); if (week_index == 0) { week_index = 7; }//获取当前是周几
            return week_index;
        }
        /// <summary>
        /// 获取当前是周几,1-7，分别是返回一到日.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetWeekName(int index)
        {
            string[] week = "一,二,三,四,五,六,日".Split(',');
            if (index.IsInRange(1, 7))
            {
                return week[index - 1];
            }
            else
                return "" ;
        }
        /// <summary>
        /// 获取当前是周几,输入日期，根据周几，分别是返回一到日.
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetWeekName(DateTime dt)
        {
            return GetWeekName(GetWeek_index(dt));
        }
        /// <summary>
        /// 获取一周的开始和结束,开始时间为第一天的0点，结束时间以最后一天的0点为结束时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static WeekInfo GetWeekInfo(DateTime dt)
        {
            int week_index = GetWeek_index(dt);//获取当前是周几
            WeekInfo info = new WeekInfo() { startDate = dt.Date.AddDays(-week_index + 1), endDate = dt.Date.AddDays(7 - week_index) };
            return info;
        }
        /// <summary>
        /// 获取2个日期相差几周
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public static int GetWeekCount(DateTime dt1, DateTime dt2)
        {
            return (GetWeekInfo(dt2).startDate - GetWeekInfo(dt1).startDate).TotalDays.ToInt() / 7;
        }
        /// <summary>
        /// 获取2个日期相差几个月
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public static int GetMonthCount(DateTime dt1, DateTime dt2)
        {
            return dt2.Year * 12 + dt2.Month - dt1.Year * 12 - dt1.Month;
        }
        /// <summary>
        /// 获取一个月的开始
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GetMonthStart(DateTime dt)
        {
            return dt.AddDays(-dt.Day+1).Date;
        }
        /// <summary>
        /// 判断是否在同一个星期。
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public static bool InSameWeek(DateTime dt1, DateTime dt2)
        {
            return GetWeekInfo(dt2).startDate==GetWeekInfo(dt1).startDate;
        }
        /// <summary>
        /// 判断是否在同一个月。
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public static bool InSameMonth(DateTime dt1, DateTime dt2)
        {
            return dt2.Year==dt1.Year && dt2.Month==dt1.Month;
        }
        /// <summary>
        /// 判断是否在同一天
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public static bool InSameDay(DateTime dt1, DateTime dt2)
        {
            return dt2.Date == dt1.Date;
        }
        
        /// <summary>  
        /// 计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.  
        /// </summary>  
        /// <param name="DateTime1">第一个日期和时间</param>  
        /// <param name="DateTime2">第二个日期和时间</param>  
        /// <returns></returns>  
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            return DateDiff(DateTime1, DateTime2, true);
        }
        /// <summary>  
        /// 计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.  
        /// </summary>  
        /// <param name="DateTime1">第一个日期和时间</param>  
        /// <param name="DateTime2">第二个日期和时间</param>  
        /// <param name="seconds">是否显示秒</param>  
        /// <returns></returns>  
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2,bool seconds)
        {
            string dateDiff = "";
            try
            {
                TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                if (ts.Days != 0)
                {
                    dateDiff = ts.Days.ToString() + "天";
                }
                if (ts.Hours != 0)
                {
                    dateDiff += ts.Hours.ToString() + "小时";
                }
                if (ts.Minutes != 0)
                {
                    dateDiff += ts.Minutes.ToString() + "分钟";
                }
                if (ts.Seconds != 0 && seconds)
                {
                    dateDiff += ts.Seconds.ToString() + "秒";
                }
            }
            catch
            {

            }
            if (dateDiff == "") { dateDiff = "0"+ (seconds?"秒": "分钟"); }
            return dateDiff;
        }
        /// <summary>  
        /// 计算一个时间与当前本地日期和时间的时间间隔,返回的是时间间隔的日期差的绝对值.  
        /// </summary>  
        /// <param name="DateTime1">一个日期和时间</param>  
        /// <returns></returns>  
        public static string DateDiff(DateTime DateTime1)
        {
            return DateDiff(DateTime1, DateTime.Now);
        }
        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long DateTimeToUnixTime(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            long timeStamp = (long)(time - startTime).TotalSeconds; // 相差秒数    
            return timeStamp;
        }
        /// <summary>        
        /// 时间戳转为C#格式时间        
        /// </summary>        
        /// <param name="timeStamp"></param>        
        /// <returns></returns>        
        public static DateTime UnixTimeToDateTime(string timeStamp)
        {
            try
            {
                System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                DateTime dt = startTime.AddSeconds(timeStamp.ToDouble(0));
                return dt;
            }
            catch { return new System.DateTime(1970, 1, 1); }
        }
        /// <summary>        
        /// 时间戳转为C#格式时间        
        /// </summary>        
        /// <param name="timeStamp"></param>        
        /// <returns></returns>        
        public static DateTime UnixTimeToDateTime(long timeStamp)
        {
            try
            {
                System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                DateTime dt = startTime.AddSeconds(timeStamp);
                return dt;
            }
            catch { return new System.DateTime(1970, 1, 1); }
        }
        /// <summary>  
        /// 将c# DateTime时间格式转换为js时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long DateTimeToJSTime(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            long timeStamp = (long)(time - startTime).TotalMilliseconds; // 相差毫秒数
            return timeStamp;
        }
        /// <summary>        
        /// JS时间戳转为C#格式时间        
        /// </summary>        
        /// <param name="timeStamp"></param>        
        /// <returns></returns>        
        public static DateTime JSTimeToDateTime(string timeStamp)
        {
            try
            {
                System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                DateTime dt = startTime.AddMilliseconds(timeStamp.ToInt64(0));
                return dt;
            }
            catch { return new System.DateTime(1970, 1, 1); }
        }
        /// <summary>        
        /// JS时间戳转为C#格式时间        
        /// </summary>        
        /// <param name="timeStamp"></param>        
        /// <returns></returns>        
        public static DateTime JSTimeToDateTime(long timeStamp)
        {
            try
            {
                System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                DateTime dt = startTime.AddMilliseconds(timeStamp);
                return dt;
            }
            catch { return new System.DateTime(1970, 1, 1); }
        }
        /// <summary>
        /// 根据年月日转换成日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        public static DateTime ToDate(int year, int month, int day)
        {
            try
            {
                return new DateTime(year, month, day);
            }
            catch { return new DateTime(2000, 1, 1); }
        }
        /// <summary>
        /// 根据日期和时间转换成日期时间
        /// </summary>
        /// <param name="date"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(DateTime date,DateTime time)
        {
            return new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second, time.Millisecond);
        }
        /// <summary>
        /// 将时间转换成当前分钟开始的时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime StartMinute(DateTime dt)
        {
            return dt.AddMilliseconds(-dt.Millisecond).AddSeconds(-dt.Second);
        }
        /// <summary>
        /// 将时间转换成当前秒钟开始的时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime StartSecond(DateTime dt)
        {
            return dt.AddMilliseconds(-dt.Millisecond);
        }
        /// <summary>
        /// 当前时间在这一天里的秒数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static Int64 DaySecond(DateTime dt)
        {
            return dt.Hour*3600+dt.Minute*60+dt.Second;
        }
    }

}
