C# 跳过值班时间代码逻辑
c#,值班,时间,代码,逻辑
·
浏览次数 : 5
小编点评
```csharp
///
/// 当前时间是否是值班时间
///
private bool IsDutyTime
{
get
{
// 定义值班时间格式
var dutyTime = "23:00-07:00";
// 检查值班时间格式是否正确
if (!string.IsNullOrEmpty(dutyTime))
{
// 解析值班时间格式
var sp = dutyTime.Split('-');
// 检查时间段是否符合要求
if (sp.Length == 0)
{
var msg = $"值班时间格式有误:{dutyTime},正确格式为(17:30-09:00)";
logger.Error(msg);
return false;
}
// 设置开始和结束时间
var hStart = 0;
var mStart = 0;
var hEnd = 0;
var mEnd = 0;
// 解析开始和结束时间
if (spStart.Length == 1)
{
hStart = Convert.ToInt32(spStart[0]);
mStart = Convert.ToInt32(spStart[1]);
}
else if (spEnd.Length == 1)
{
hEnd = Convert.ToInt32(spEnd[0]);
mEnd = Convert.ToInt32(spEnd[1]);
}
// 检查开始时间比结束时间早则属于值班时间
if (hStart > hEnd)
{
return true;
}
else if (hStart == hEnd && mStart > mEnd)
{
return true;
}
else if (hStart <= hEnd && mStart <= mEnd)
{
return true;
}
}
return false;
}
}
```
正文
/// <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;
}
}
与C# 跳过值班时间代码逻辑相似的内容: