简体   繁体   English

确定落在时间范围之间的时间戳

[英]Determine time stamp falling between a time range

I need to run a job/scheduler, which should only update records in database it is falling under the specified time range. 我需要运行一个作业/调度程序,它应该只更新它在指定时间范围内的数据库中的记录。 In my case, the time range is from 3.30 AM to next day 1.30AM. 就我而言,时间范围是从凌晨3点30分到次日凌晨1点30分。 So, with in this time frame the job needs to update the records. 因此,在此时间范围内,作业需要更新记录。 In order to get this time interval, I using TimeOfDay() function, but my logic is getting failed, bcoz if the currenttime is say 6.00 Am, then "currentTime <= todaysJob.ENDTIME.Value.TimeOfDay" is returning false. 为了获得这个时间间隔,我使用了TimeOfDay()函数,但是我的逻辑失败了,如果当前时间是6.00 Am,则bcoz然后“currentTime <= todaysJob.ENDTIME.Value.TimeOfDay”返回false。 I am using the below code to check 我使用以下代码进行检查

     var currentTime = DateTime.Now.TimeOfDay;
     if (currentTime > todaysJob.STARTTIME.Value.TimeOfDay && 
          currentTime <= todaysJob.ENDTIME.Value.TimeOfDay)
            {
                // Do logic
            }
        bool endTomorrow = true;

        DateTime taskDate = new DateTime(2012, 08, 31);

        TimeSpan Start = new TimeSpan(03, 30, 00);
        TimeSpan End = new TimeSpan(01, 30, 00);

        DateTime currentTime = DateTime.Now;

        bool flag = false;

        if (currentTime.TimeOfDay >= Start)
        {
            if (endTomorrow)
            {
                flag = currentTime.Date <= taskDate || (currentTime.Date == taskDate.AddDays(1) && currentTime.TimeOfDay < End);
            }
            else
            {
                flag = currentTime.TimeOfDay < End;
            }
        }

        if (flag)
        {
            //do the task
        }

EDIT 编辑

So I added: 所以我补充说:

  1. a boolean flag, determining whether the task should end the next day 布尔标志,确定任务是否应该在第二天结束
  2. a datetime variable (taskDate) saying the date of the task 一个日期时间变量(taskDate),表示任务的日期

Start and End are equal to todaysJob.STARTTIME and todaysJob.ENDTIME, so you take them from DB as they are. Start和End等于todaysJob.STARTTIME和todaysJob.ENDTIME,所以你可以按原样从DB中取出它们。

EDIT 编辑

If you could have your job like this: 如果你能得到这样的工作:

public class Job
{
    public TimeSpan STARTTIME;
    public TimeSpan ENDTIME;
    public DayOfWeek taskDayOfWeek;
    public bool IsEndingTommorow;

    public bool IsTomorrow(DayOfWeek d)
    {
        if (d == DayOfWeek.Sunday)
            return taskDayOfWeek == DayOfWeek.Saturday;
        else
            return d <= taskDayOfWeek;
    }
}

then you could 那么你可以

        DateTime currentTime = DateTime.Now;

        bool flag = false;

        if (currentTime.TimeOfDay >= todaysJob.STARTTIME)
        {
            if (todaysJob.IsEndingTommorow)
            {
                flag = currentTime.DayOfWeek == todaysJob.taskDayOfWeek || (todaysJob.IsTomorrow(currentTime.DayOfWeek) && currentTime.TimeOfDay < todaysJob.ENDTIME);
            }
            else
            {
                flag = currentTime.TimeOfDay < todaysJob.ENDTIME;
            }
        }

        if (flag)
        {
            //do the task
        }

EDIT 编辑

I've edited my code another time: added a method to avoid problems with the DayOfWeek enum 我已经编辑了我的代码了一次:添加了一种避免DayOfWeek枚举问题的方法

You can use the Time Period Library for .NET , to determine, if a moment falls into multiple time periods: 如果片刻属于多个时间段,您可以使用.NET时间段库来确定:

// ----------------------------------------------------------------------
public bool CheckDateBetweenDatesSample()
{
  DateTime now = DateTime.Now;
  TimePeriodCollection periods = new TimePeriodCollection();
  // read periods (Start/end) from database
  // ...
  periods.Add( new TimeRange( start, end ) );
  return periods.HasIntersectionPeriods( now );
} // CheckDateBetweenDatesSample

Try this, its working for me. 试试这个,它为我工作。 Correct me if i'm wrong. 如果我错了纠正我。

TimeSpan timeOfDay = DateTime.Now.TimeOfDay;
TimeSpan startTimeToD = startTime.TimeOfDay;
TimeSpan endTimeToD = endTime.TimeOfDay;

if (timeOfDay > startTimeToD || timeOfDay < endTimeToD )
{
      Console.WriteLine("Hello World");
}

timeOfDay = new TimeSpan(2, 30, 00); //testcase
if (timeOfDay > startTimeToD || timeOfDay < endTimeToD )
{
     //will never execute.
}

You cannot use TimeOfDay property as it is the absolute distance(elapsed time) from a midpoint(Midnight). 您不能使用TimeOfDay属性,因为它是从中点(午夜)的绝对距离(经过时间)。 What is wrong with using just DateTime for comparison? 使用DateTime进行比较有什么问题? Comparison operators( < > <= >=) work perfectly fine with DateTime datatype. 比较运算符(<> <=> =)与DateTime数据类型完美匹配。

For Eg: 对于Eg:

DateTime currentTime = DateTime.Now;
DateTime startTime = DateTime.AddMinutes(-50D); //in your case this would be a DT todaysJob.STARTTIME.Value
DateTime endTime = DateTime.AddMinutes(50);// in your case this would be a DT todaysJob.ENDTIME.Value

if(currentTime > startTime && currentTime <= endTime)
{
  Console.Write("Works Fine"); //your logic
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM