检查餐厅是否打开或关闭C#,多次打开/关闭时间

时间:2016-09-29 11:31:47

标签: c# datetime

我有一个场景,餐馆可以有多个开/关时间,在某些情况下,关闭时间可以在午夜过去。

这样的事情:

Day - [Monday] - Open Time - [09:00] - Close Time - [18:00]
Day - [Monday] - Open Time - [22:00] - Close Time - [02:00]

Day - [Tuesday] - Open Time - [12:00] - Close Time - [23:59]

Day - [Wednesday] - Open Time - [09:00] - Close Time - [18:00]
Day - [Wednesday] - Open Time - [22:00] - Close Time - [02:00]

Day - [Thursday] - Open Time - [14:00] - Close Time - [02:00]

Day - [Friday] - Open Time - [16:00] - Close Time - [02:00]

Day - [Saturday] - Open Time - [12:00] - Close Time - [01:00]

Day - [Sunday] - Open Time - [12:00] - Close Time - [01:59]

我需要检查餐厅是否在请求时打开或关闭,这是我到目前为止所做的(发布包含示例数据的整个代码 - 您可以粘贴到C#控制台应用程序并运行它):< / p>

internal class Program
{
    private static void Main(string[] args)
    {
        var restaurantList = new RestaurantTimingList();


        Console.WriteLine("---------------------------------RESTAURANT TIMINGS -----------------------------------------");
        Console.WriteLine("---------------------------------------------------- ----------------------------------------");

        foreach (var timing in restaurantList.Timings)
        {
            Console.WriteLine("Day - [{0}] - Open Time - [{1}] - Close Time - [{2}]", timing.Day, timing.OpenTimeString, timing.CloseTimeString);
        }
        Console.WriteLine("---------------------------------------------------- ----------------------------------------");
        Console.WriteLine("---------------------------------------------------- ----------------------------------------");

        var time = "01:01";

        var dayOfWeek = DayOfWeek.Saturday;

        Console.WriteLine("Day - [{0}] - Time - [{1}] - IsOpen - [{2}]", dayOfWeek, time, restaurantList.CheckIsOpen(dayOfWeek, time));
        Console.WriteLine("---------------------------------------------------- ----------------------------------------");

        time = "01:00";
        Console.WriteLine("Day - [{0}] - Time - [{1}] - IsOpen - [{2}]", dayOfWeek, time, restaurantList.CheckIsOpen(dayOfWeek, time));
        Console.WriteLine("---------------------------------------------------- ----------------------------------------");

        dayOfWeek = DayOfWeek.Tuesday;
        time = "00:00";
        Console.WriteLine("Day - [{0}] - Time - [{1}] - IsOpen - [{2}]", dayOfWeek, time, restaurantList.CheckIsOpen(dayOfWeek, time));
        Console.WriteLine("---------------------------------------------------- ----------------------------------------");

        time = "23:58";
        Console.WriteLine("Day - [{0}] - Time - [{1}] - IsOpen - [{2}]", dayOfWeek, time, restaurantList.CheckIsOpen(dayOfWeek, time));
        Console.WriteLine("---------------------------------------------------- ----------------------------------------");

        Console.ReadKey();
    }
}


public class RestaurantTiming
{
    public int TimingId { get; set; }
    public DayOfWeek Day { get; set; }

    public string OpenTimeString { get; set; }
    public TimeSpan OpenTime { get; set; }

    public string CloseTimeString { get; set; }
    public TimeSpan CloseTime { get; set; }

    public RestaurantTiming(int timingId, DayOfWeek day, string openTime, string closeTime)
    {
        TimingId = timingId;
        Day = day;

        OpenTimeString = openTime;
        OpenTime = TimeSpan.Parse(openTime);

        CloseTimeString = closeTime;
        CloseTime = TimeSpan.Parse(closeTime);
    }
}

public class RestaurantTimingList
{
    public List<RestaurantTiming> Timings { get; set; }

    public RestaurantTimingList()
    {
        Timings = new List<RestaurantTiming>();

        Timings.Add(new RestaurantTiming(1, DayOfWeek.Monday, "09:00", "18:00"));
        Timings.Add(new RestaurantTiming(2, DayOfWeek.Monday, "22:00", "02:00"));

        Timings.Add(new RestaurantTiming(3, DayOfWeek.Tuesday, "12:00", "23:59"));

        Timings.Add(new RestaurantTiming(4, DayOfWeek.Wednesday, "09:00", "18:00"));
        Timings.Add(new RestaurantTiming(5, DayOfWeek.Wednesday, "22:00", "02:00"));

        Timings.Add(new RestaurantTiming(6, DayOfWeek.Thursday, "14:00", "02:00"));

        Timings.Add(new RestaurantTiming(7, DayOfWeek.Friday, "16:00", "02:00"));

        Timings.Add(new RestaurantTiming(8, DayOfWeek.Saturday, "12:00", "01:00"));

        Timings.Add(new RestaurantTiming(9, DayOfWeek.Sunday, "12:00", "01:59"));
    }

    public bool CheckIsOpen(DayOfWeek weekDay, string time)
    {
        var timeSpan = TimeSpan.Parse(time);
        // Group By Day Of week - 
        var grouped = Timings.GroupBy(s => s.Day).ToList();

        // Get All timings of the day
        var weekDayTimings = grouped.FirstOrDefault(s => s.Key == weekDay);

        // No Weekday found - Restaurant closed on this return false
        if (weekDayTimings == null) return false;

        // Check passed time to see if is in Any of the timings of that Day
        foreach (var timing in weekDayTimings)
        {
            Console.WriteLine("[{0}] Open [{1}] - Close [{2}] -  ", weekDay, timing.OpenTimeString, timing.CloseTimeString);

            var start = timing.OpenTime;
            var end = timing.CloseTime;
            var now = timeSpan;

            if (start <= end)
            {
                // start and stop times are in the same day
                if (now >= start && now <= end)
                {
                    // current time is between start and stop
                    return true;
                }
            }
            else
            {
                // start and stop times are in different days
                if (now >= start || now <= end)
                {
                    // current time is between start and stop
                    return true;
                }
            }
        }
        return false;
    }

}

问题

我将尝试用一个例子解释它:

餐厅在Monday from 10:00 PM - 02:00 AM开放 - 所以时间进入第二天 - (这意味着餐厅周二从00:00 AM - 02:00 AM开放)

现在让我们假设用户在Tuesday 01:00 AM

上发出请求

像这样:

 dayOfWeek = DayOfWeek.Tuesday; // What will actually be here DateTime.Now.Day;
 time = "01:00";  // What will actually be here: DateTime.Now.TimeOfDay;
 Console.WriteLine("Day - [{0}] - Time - [{1}] - IsOpen - [{2}]", dayOfWeek, time, restaurantList.CheckIsOpen(dayOfWeek, time));

现在返回FALSE(餐厅未打开) - 但因为Monday is till 02:00 AM上的时间应该返回true。

2 个答案:

答案 0 :(得分:1)

我改变了一点你的逻辑。

让餐厅时间决定何时与时间相匹配

public class RestaurantTiming
{
    public int TimingId { get; set; }
    public Interval[] Intervals { get; set;}

    public string OpenTimeString { get; private set; }
    public string CloseTimeString { get; private set; }

    public RestaurantTiming(int timingId, DayOfWeek day, string openTime, string closeTime)
    {
        TimingId = timingId;
        OpenTimeString = openTime;
        CloseTimeString = closeTime;

        var intervals = new List<Interval>();


        var openTimeParsed = TimeSpan.Parse(openTime);      
        var closeTimeParsed = TimeSpan.Parse(closeTime);

        if (closeTimeParsed > openTimeParsed)
        {
            intervals.Add(new Interval() { Day = day, CloseTime = closeTimeParsed, OpenTime = openTimeParsed });
        }
        else
        {
            intervals.Add(new Interval() { Day = day, CloseTime = new TimeSpan(23,59,59), OpenTime = openTimeParsed });
            intervals.Add(new Interval() { Day = NextDayOfWeek(day), CloseTime = closeTimeParsed, OpenTime = new TimeSpan(0,0,0) });
        }       

        Intervals = intervals.ToArray();
    }

    public bool IsMatch(DayOfWeek day, TimeSpan span)
    {
        return Intervals.Any(x=>x.Day == day && x.OpenTime <= span && x.CloseTime >= span);
    }

    public class Interval
    {
        public DayOfWeek Day { get; set; }      
        public TimeSpan OpenTime { get; set; }      
        public TimeSpan CloseTime { get; set; }
    }

    private DayOfWeek NextDayOfWeek(DayOfWeek current)
    {
        return NextDays[current];
    }

    private static Dictionary<DayOfWeek, DayOfWeek> NextDays = new Dictionary<DayOfWeek, DayOfWeek>()
        {
            {DayOfWeek.Sunday, DayOfWeek.Monday},
            { DayOfWeek.Monday, DayOfWeek.Tuesday},
            { DayOfWeek.Tuesday, DayOfWeek.Wednesday},
            { DayOfWeek.Wednesday, DayOfWeek.Thursday},
            { DayOfWeek.Thursday, DayOfWeek.Friday},
            { DayOfWeek.Friday, DayOfWeek.Saturday},
            { DayOfWeek.Saturday, DayOfWeek.Sunday}
        };
}

在这种情况下,时间列表将更简单

public class RestaurantTimingList
{
    public List<RestaurantTiming> Timings { get; set; }

    public RestaurantTimingList()
    {
        Timings = new List<RestaurantTiming>();

        Timings.Add(new RestaurantTiming(1, DayOfWeek.Monday, "09:00", "18:00"));
        Timings.Add(new RestaurantTiming(2, DayOfWeek.Monday, "22:00", "02:00"));

        Timings.Add(new RestaurantTiming(3, DayOfWeek.Tuesday, "12:00", "23:59"));

        Timings.Add(new RestaurantTiming(4, DayOfWeek.Wednesday, "09:00", "18:00"));
        Timings.Add(new RestaurantTiming(5, DayOfWeek.Wednesday, "22:00", "02:00"));

        Timings.Add(new RestaurantTiming(6, DayOfWeek.Thursday, "14:00", "02:00"));

        Timings.Add(new RestaurantTiming(7, DayOfWeek.Friday, "16:00", "02:00"));

        Timings.Add(new RestaurantTiming(8, DayOfWeek.Saturday, "12:00", "01:00"));

        Timings.Add(new RestaurantTiming(9, DayOfWeek.Sunday, "12:00", "01:59"));
    }

    public bool CheckIsOpen(DayOfWeek weekDay, string time)
    {
        return Timings.Any(x=>x.IsMatch(weekDay, TimeSpan.Parse(time)));
    }

}

我们必须对Main

应用最小的更改
void Main()
{
    var restaurantList = new RestaurantTimingList();

    Console.WriteLine("---------------------------------RESTAURANT TIMINGS -----------------------------------------");
    Console.WriteLine("---------------------------------------------------- ----------------------------------------");

    foreach (var timing in restaurantList.Timings.SelectMany(x=>x.Intervals))
    {
        Console.WriteLine("Day - [{0}] - Open Time - [{1}] - Close Time - [{2}]", timing.Day, timing.OpenTime, timing.CloseTime);
    }
    Console.WriteLine("---------------------------------------------------- ----------------------------------------");
    Console.WriteLine("---------------------------------------------------- ----------------------------------------");

    var time = "01:01";

    var dayOfWeek = DayOfWeek.Saturday;

    Console.WriteLine("Day - [{0}] - Time - [{1}] - IsOpen - [{2}]", dayOfWeek, time, restaurantList.CheckIsOpen(dayOfWeek, time));
    Console.WriteLine("---------------------------------------------------- ----------------------------------------");

    time = "01:00";
    Console.WriteLine("Day - [{0}] - Time - [{1}] - IsOpen - [{2}]", dayOfWeek, time, restaurantList.CheckIsOpen(dayOfWeek, time));
    Console.WriteLine("---------------------------------------------------- ----------------------------------------");

    dayOfWeek = DayOfWeek.Tuesday;
    time = "00:00";
    Console.WriteLine("Day - [{0}] - Time - [{1}] - IsOpen - [{2}]", dayOfWeek, time, restaurantList.CheckIsOpen(dayOfWeek, time));
    Console.WriteLine("---------------------------------------------------- ----------------------------------------");

    time = "23:58";
    Console.WriteLine("Day - [{0}] - Time - [{1}] - IsOpen - [{2}]", dayOfWeek, time, restaurantList.CheckIsOpen(dayOfWeek, time));
    Console.WriteLine("---------------------------------------------------- ----------------------------------------");

    Console.ReadKey();  
}

输出

---------------------------------RESTAURANT TIMINGS -----------------------------------------
---------------------------------------------------- ----------------------------------------
Day - [Monday] - Open Time - [09:00:00] - Close Time - [18:00:00]
Day - [Monday] - Open Time - [22:00:00] - Close Time - [23:59:59]
Day - [Tuesday] - Open Time - [00:00:00] - Close Time - [02:00:00]
Day - [Tuesday] - Open Time - [12:00:00] - Close Time - [23:59:00]
Day - [Wednesday] - Open Time - [09:00:00] - Close Time - [18:00:00]
Day - [Wednesday] - Open Time - [22:00:00] - Close Time - [23:59:59]
Day - [Thursday] - Open Time - [00:00:00] - Close Time - [02:00:00]
Day - [Thursday] - Open Time - [14:00:00] - Close Time - [23:59:59]
Day - [Friday] - Open Time - [00:00:00] - Close Time - [02:00:00]
Day - [Friday] - Open Time - [16:00:00] - Close Time - [23:59:59]
Day - [Saturday] - Open Time - [00:00:00] - Close Time - [02:00:00]
Day - [Saturday] - Open Time - [12:00:00] - Close Time - [23:59:59]
Day - [Sunday] - Open Time - [00:00:00] - Close Time - [01:00:00]
Day - [Sunday] - Open Time - [12:00:00] - Close Time - [23:59:59]
Day - [Monday] - Open Time - [00:00:00] - Close Time - [01:59:00]
---------------------------------------------------- ----------------------------------------
---------------------------------------------------- ----------------------------------------
Day - [Saturday] - Time - [01:01] - IsOpen - [True]
---------------------------------------------------- ----------------------------------------
Day - [Saturday] - Time - [01:00] - IsOpen - [True]
---------------------------------------------------- ----------------------------------------
Day - [Tuesday] - Time - [00:00] - IsOpen - [True]
---------------------------------------------------- ----------------------------------------
Day - [Tuesday] - Time - [23:58] - IsOpen - [True]
---------------------------------------------------- ----------------------------------------

编辑: 我将结束时间指定为new TimeSpan(24,59,59),但如果您确切地检查00:00,则可以将其替换为new TimeSpan(24,0,0)

答案 1 :(得分:0)

有两种方法可以得到你想要的东西!

  1. 根据评论中的建议,您可以创建一个新表(手动,如果允许,或通过代码!),它具有每天的开始和结束时间。因此,如果星期一的时间是22:00到02:00,您的新表将在星期一22:00到23:59和星期二00:00到02:00。
  2. 这将简化您的问题并使用您的代码提供正确的输出。

    1. 如果您不想使用上述解决方案,可以执行以下操作: 检查前一天的开始和结束时间,看看他们是否在午夜后开放。
    2. 希望这有帮助! :)