在特定时间范围内查找时间发生和持续时间

时间:2011-07-25 09:42:31

标签: c# datetime time range

我在C#中制作了一个小停车应用程序。根据车辆类型和时区,有一些不同的价格。日可以分为时区(例如早晨,白天,晚上和夜晚)。现在,如果客户停止停车,我想计算客户停放的时区和停留时间。

例如早上时区从6:00开始到12:00结束,白天时区从12:00开始到16:00结束,晚上时区从16:00开始到23:00和夜晚结束区域从23:00开始,到6:00结束。客户开始在00:30停车并在6:32停车。目前我有4个变量:停车开始时间,停车结束时间和时区开始时间和时区结束时间。

第二个例子就像客户公园24H,然后停车时间涵盖了所有时区。

如何计算客户在不同时区停放汽车的小时数和分钟数的最简单方法是什么?

此致 evilone

修改

从MSDN获得此答案并将其发布在此处,以便其他人也可以从中学习。

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DateTime start = new DateTime(2011, 7, 25, 0, 30, 0);
            DateTime end = new DateTime(2011, 7, 26, 6, 32, 0);
            List<DateTime> listTimeZones = CalculateTotalTime(start, end);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < listTimeZones.Count; i++)
            {
                sb.AppendLine(String.Format("{0}. {1}: from {2} to {3}",
                                            i + 1,
                                            GetZoneInWords(listTimeZones[i].Hour),
                                            String.Format("{0:dd.MM.yyyy hh:mm}", listTimeZones[i]),
                                            (i + 1) < listTimeZones.Count
                                                ? String.Format("{0:dd.MM.yyyy hh:mm}", listTimeZones[i + 1])
                                                : "Parking ended"));
            }
            MessageBox.Show(sb.ToString());
        }

        private List<DateTime> CalculateTotalTime(DateTime start, DateTime end)
        {
            DateTime temp = start;

            int hour = start.Hour;
            int minute = start.Minute;

            int morning = 6;
            int day = 12;
            int evening = 17;
            int night = 23;

            List<DateTime> timeZones = new List<DateTime>();

            do
            {
                temp = temp.AddHours(1);
                if (temp.Hour == morning || temp.Hour == day ||
                    temp.Hour == evening || temp.Hour == night)
                {
                    timeZones.Add(temp);
                }
            } while (temp < end);

            return timeZones;
        }

        private string GetZoneInWords(int time)
        {
            string timeOfDay = "";
            if (time.Equals(6))
                timeOfDay = "Morning";
            else if (time.Equals(12))
                timeOfDay = "Day";
            else if (time.Equals(17))
                timeOfDay = "Evening";
            else if (time.Equals(23))
                timeOfDay = "Night";

            return timeOfDay + " parking";
        }
    }

1 个答案:

答案 0 :(得分:4)

遍历所有“时区”,并为每个“时区”计算出它与客户停车之间的重叠。例如,伪代码:

private static TimeSpan FindOverlap(ParkingTime parkingTime, TimeZone timeZone)
{
    // Handle wraparound zones like 23-6. Note that this doesn't attempt
    // to handle *parking* which starts at 11.30pm etc.
    if (timeZone.Start > timeZone.End)
    {
        return FindOverlap(parkingTime,
                     new TimeZone(timeZone.Start.Date, timeZone.End)
             + FindOverlap(parkingTime,
                     new TimeZone(timeZone.End, timeZone.Start.Date.AddDays(1));
    }

    DateTime overlapStart = Max(parkingTime.Start, timeZone.Start);
    DateTime overlapEnd = Min(parkingTime.End, timeZone.End);
    TimeSpan overlap = overlapEnd - overlapStart;

    // If the customer arrived after the end or left before the start,
    // the overlap will be negative at this point.
    return overlap < TimeSpan.Zero ? TimeSpan.Zero : overlap;
}

private static DateTime Min(DateTime x, DateTime y)
{
    return x < y ? x : y;
}

private static DateTime Max(DateTime x, DateTime y)
{
    return x > y ? x : y;
}

顺便说一下,我强烈鼓励你重命名你的“时区”概念,因为它已经有了众所周知的(如果不是很清楚的话)含义。

也许你应该叫它ParkingInterval?或ParkingPriceInterval如果差异实际上与成本有关?