排行榜 统计
  • 建站日期:2021-02-22
  • 文章总数:8 篇
  • 评论总数:1 条
  • 分类总数:4 个
  • 最后更新:2月23日

关于时间的问题代码

本文阅读 1 分钟
        static void Main(string[] args)
        {
            for (var i = 1; i <= 1000; i++)
            {
                var sDate = radddddddtime();
                var eDate = sdate.AddHours(new Random().Next(0, 100));
                var lightDate = GetLightDateTime(sDate, eDate);

                if (!lightDate.HasValue)
                {
                    Console.WriteLine("不存在白天时间");
                }
                else
                {
                    Console.WriteLine("Usa Los Light Time ---" + date.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                }
            }

            Console.ReadKey();
        }

        public static DateTime? GetLightDateTime(DateTime startDate, DateTime endDate)
        {
            
            var timeSpanHour = (endDate - startDate).TotalHours;

            if (timeSpanHour>72)
            {
                timeSpanHour = 72;
                endDate = startDate.AddHours(72);
            }

            if (timeSpanHour >= 12 || IsUsaLightDateTime(startDate)
                                  || IsUsaLightDateTime(endDate))
            {
                if (timeSpanHour < 1)
                {
                    if (IsUsaLightDateTime(startDate))
                    {
                        return startDate;
                    }

                    if (IsUsaLightDateTime(endDate))
                    {
                        return endDate;
                    }
                }

                var random = new Random();
                for (int i = 0; i < 200; i++)
                {
                    var date = startDate.AddHours(random.Next(0, (int)timeSpanHour));
                    if (IsUsaLightDateTime(date))
                    {
                        return date;
                    }
                }
                Console.WriteLine("200次循环随机没有去到白天时间");
            }

            return null;
        }

        /// <summary>
        /// 自定义白天时间判断
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        private static bool IsUsaLightDateTime(DateTime dateTime)
        {
            dateTime = dateTime.AddHours(-15);
            var lightStartDate = dateTime.Date.AddHours(9);
            var lightEndDate = dateTime.Date.AddHours(21);

            return dateTime >= lightStartDate && dateTime <= lightEndDate;
        }

        public static RandomDateTime()
        {
            int year = new Random().Next(2015, 2020);
            int month = new Random().Next(1, 12);
            int day = new Random().Next(1, 28);

            int hour = new Random().Next(0, 24);
            int minute = new Random().Next(0, 60);
            int second = new Random().Next(0, 60);

            string tempStr = $"{year}-{month}-{day} {hour}:{minute}:{second}";
            return Convert.ToDateTime(tempStr);
        }
原创文章,作者:Ayooo,如若转载,请注明出处:https://comzg.com/study/timeprogram.html
Microsoft Remote Desktop Beta For Mac 地址
« 上一篇 02-23

发表评论

成为第一个评论的人

作者信息

热门文章

2

最多点赞

0赞, 阅读:2
0赞, 阅读:2

标签TAG

热评文章

最近回复