c# - 查找特定时间范围内的时间发生和持续时间

标签 c# datetime time range

我用 C# 做了一个小的 parking 应用程序。根据车辆类型和时区的不同,有一些不同的定价。一天可以分为时区(例如早上、白天、傍晚和夜晚)。现在,如果客户停止 parking ,我想计算客户在哪些时区 parking 以及 parking 了多长时间。

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

第二个例子是客户 parking 24 小时,然后 parking 时间涵盖所有时区。

如何最简单地计算客户在不同时区 parking 的小时数和分钟数?

问候, 恶灵

编辑:

从 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";
        }
    }

最佳答案

遍历所有“时区”,并针对每个时区计算出该时区与客户 parking 位之间的重叠部分。例如,作为伪代码:

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 如果差异真的在于成本?

关于c# - 查找特定时间范围内的时间发生和持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6814227/

相关文章:

javascript - 如何为多种语言定义新的日期字符串并在 Moment.js 中使用它?

php - 将日期时间转换为时间戳 php

java - 将 mySQL localTime 更改为当前时间

c# - 如何使用 Skia 或 SkiaSharp 绘制富文本

c# - 如何决定api签名

c# - 捕捉到关注方向的道路

JavaScript 超时 div

c# - 静态属性作为该类实例的线程安全考虑

python - 如何在 matplotlib 中使用日期时间索引增加 xticks?

date - 使用 gnuplot 制作基于时间的图表时遇到问题