【问题标题】:Determine if current time falls within multiple time ranges确定当前时间是否在多个时间范围内
【发布时间】:2014-05-21 17:30:15
【问题描述】:

我编写了一个显示当前时间 (HH:MM) 的简单应用程序。 显示的时间每秒刷新一次 DispatcherTimer。

考虑到我有一个 XML 格式的“配置”文件,其中包含必须以不同颜色突出显示 5 分钟的小时列表:

例子:

  • 08:15 → 突出显示从 8:15 到 8:20(含)
  • 10:20 → 突出显示从 10:20 到 10:25(含)
  • 11:55 → 突出显示从 11:55 到 12:00(含)
  • 14:15 → 突出显示从 14:15 到 14:20(含)
  • 16:05 → 突出显示从 16:05 到 16:10(含)

但如果存在“重叠”的可能性,例如:

  • 08:10
  • 08:12

然后应该突出显示从 8:15 到 08:17 的时间

您将如何实现您的代码来检查当前/实际时间是否在这些范围内?

您是否会首先生成一个包含每个条目的所有开始和结束时间的列表,然后每秒对照该列表检查实际时间? (每秒循环遍历所有列表值)? :-/

或者你会以其他方式实现这一目标吗?

编辑:这是迄今为止所做的:

private enum DisplayState
{
    /// <summary>
    /// The application has just been started or reset due to changes to the configuration file
    /// </summary>
    Initial,

    /// <summary>
    /// The current displayed time is highlighed
    /// </summary>
    Highlighted,

    /// <summary>
    /// The current displayed time is not highlighed
    /// </summary>
    Normal
};

DisplayState _lastDisplayState = DisplayState.Initial;

// The list that retrieves the hours from the XML file as string values ("HH:MM")
// Note: this can be changed into a list of TimeSpan or anything else in a near future
private List<string> _hourList;

private DispatcherTimer _dispatcherTimer;

...

/// <summary>
/// What happens each second
/// </summary>
private void dispatcherTimerTick(object sender, EventArgs e)
{
    // Store current time
    _timeToDisplay = DateTime.Now.ToString("HH:mm");

    // Refresh displayed time
    tbDigitalClock.Text = tbDigitalClockBack.Text = _timeToDisplay;

    PlaceWindow();

    // Check if our list of hours contain current time
    // Note: for the moment _hourList is a list of strings (hours from the XML file)
    if (_hourList.Contains(_timeToDisplay))
    {
        // Only change appearance once if display state changed
        if (_lastDisplayState != DisplayState.Highlighted)
        {
            _lastDisplayState = DisplayState.Highlighted;

            // Play sound alert if desired
            if (_playSound)
                _soundPlayer.Play();

            // Highlight current displayed time
            tbDigitalClock.Foreground = Brushes.Yellow;
            tbDigitalClockBack.Foreground = Brushes.Black;
            mainBorder.Background = _highlightedBackColor;
            mainBorder.BorderBrush = Brushes.Red;
        }
    }
    else
    {
        if (_lastDisplayState != DisplayState.Normal)
        {
            _lastDisplayState = DisplayState.Normal;

            // Turn displayed time appearance back to normal
            tbDigitalClock.Foreground = _defaultTextColor;
            tbDigitalClockBack.Foreground = _defaultTextBackColor;
            mainBorder.Background = Brushes.Transparent;
            mainBorder.BorderBrush = Brushes.Transparent;
        }
    }
}

XML 文件如下所示:

<DigiClock>
    <Config PlaySound="true" />
    <Breaks>
        <Time>10:00</Time>
        <Time>12:00</Time>
        <Time>14:30</Time>
        <Time>16:30</Time>
    </Breaks>
</DigiClock>

提前非常感谢! ;-)

【问题讨论】:

  • 你能告诉我们你到目前为止做了什么吗?这是一个自以为是的问题。
  • 嗨,我添加了到目前为止所做的事情。谢谢
  • 这不是关于我们将如何实现代码,而是关于您尝试过哪些不起作用
  • 好吧,我还没有实现这个,因为我仍然想知道“确定当前时间是否在这些范围内的最佳方法/替代方法是什么”。这就是我发布这个问题的原因:-)
  • 我想到了两件事:a)首先动态创建一个包含开始和结束(计算)小时的对象列表,然后每秒检查一次该列表(foreach)或 b)每秒执行一次测试从 XML(开始时间)和结束时间(开始时间 + 5 分钟)开始循环。选项 b) 似乎比 a) 更糟糕 :-/ 你认为其他更好的东西(更少消耗)吗?谢谢 :-)

标签: c# datetime time timespan range


【解决方案1】:

您似乎正在编写一个 GUI 应用程序,我会使用一个计时器,其间隔设置为 1 秒(或者可能 5 秒就足够了?或者介于两者之间?)和一个类似的事件处理程序(未经测试) 一:

TimeSpan t = new TimeSpan(0, 5, 0); // five minutes
List<DateTime> _hourList;
bool _soundPlayed = false;

private void OnTimerTick()
{
    DateTime now = DateTime.Now;
    foreach (var h in _hourList)
    {
        if ((now >= h) && (now <= (h + t)))
        {
            if (_lastDisplayState != DisplayState.Highlighted)
            {
                _lastDisplayState = DisplayState.Highlighted;
                ...
            }

            if (!_soundPlayed)
            {
                _soundPlayed = true;
                _soundPlayer.Play();
            }
            return;
        }
    }

    // if code runs until here, we are out of any highlighted moment
    _soundPlayed = false;
    if (_lastDisplayState != DisplayState.Normal)
    {
        _lastDisplayState = DisplayState.Normal;
        ...
    }
}

【讨论】:

    猜你喜欢
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多