/// <summary> /// 当前时间是否是值班时间 /// </summary> private bool IsDutyTime { get { var dutyTime = "23:00-07:00"; if (!string.IsNullOrEmpty(dutyTime)) { var now = DateTime.Now; var sp = dutyTime.Split('-'); if (sp.Length == 0) { var msg = $"值班时间格式有误:{dutyTime},正确格式为(17:30-09:00)"; logger.Error(msg); return false; } #region 跳过设置时间段 var hStart = 0; var mStart = 0; var hEnd = 0; var mEnd = 0; //23:00-07:00 var spStart = sp[0].Split(':'); if (spStart.Length > 1) { hStart = Convert.ToInt32(spStart[0]); mStart = Convert.ToInt32(spStart[1]); } else { var msg = $"值班时间格式有误:{dutyTime},正确格式为(17:30-09:00)"; logger.Error(msg); return false; } var spEnd = sp[1].Split(':'); if (spEnd.Length > 1) { hEnd = Convert.ToInt32(spEnd[0]); mEnd = Convert.ToInt32(spEnd[1]); } else { var msg = $"值班时间格式有误:{dutyTime},正确格式为(17:30-09:00)"; logger.Error(msg); return false; } if (hStart > hEnd) { //23:00-07:00 or logger.Info($"设置时间段 dutyTime =>{dutyTime} 【now.Hour > hStart || (now.Hour == hStart && now.Minute >= mStart) || now.Hour < hEnd || (now.Hour == hEnd && now.Minute <= mEnd)】 =>" + $" 【{now.Hour} > {hStart} || ({now.Hour} == {hStart} && {now.Minute} >= {mStart}) || {now.Hour} < {hEnd} || ({now.Hour} == {hEnd} && {now.Minute} <= {mEnd})】"); if (now.Hour > hStart || (now.Hour == hStart && now.Minute >= mStart) || now.Hour < hEnd || (now.Hour == hEnd && now.Minute <= mEnd)) { logger.Info($"当前时间段,属于值班时间 =>{dutyTime}"); return true; } } else { //07:00-23:00 and logger.Info($"设置时间段 dutyTime =>{dutyTime} 【now.Hour >= hStart && now.Minute >= mStart && now.Hour <= hEnd && now.Minute <= mEnd】 => " + $"【{now.Hour} >= {hStart} && {now.Minute} >= {mStart} && {now.Hour} <= {hEnd} && {now.Minute} <= {mEnd}】"); if ((now.Hour >= hStart && now.Minute >= mStart) && (now.Hour <= hEnd && now.Minute <= mEnd)) { logger.Info($"当前时间段,属于值班时间 =>{dutyTime}"); return true; } } #endregion } return false; } }