确定当前时间是否在多个时间范围内

时间:2014-05-21 17:30:15

标签: c# datetime time timespan range

我写了一个显示当前时间(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(含)突出显示

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

  • 8时10
  • 8时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>

非常感谢提前! ; - )

1 个答案:

答案 0 :(得分:0)

因为看起来你正在编写一个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;
        ...
    }
}