[英]Determine if current time falls within multiple time ranges
I wrote a simple application that displays the current time (HH:MM). 我编写了一个简单的应用程序来显示当前时间(HH:MM)。 The displayed time is refreshed every second a DispatcherTimer.
显示的时间每秒钟刷新一次DispatcherTimer。
Considering I have a "configuration" file in XML format that contains a list of hours that must be displayed highlighted in a different color for 5 minutes: 考虑到我有一个XML格式的“配置”文件,其中包含一个小时列表,该小时列表必须以不同的颜色突出显示5分钟:
Example: 例:
But if there are "overlapping" possibilities, like: 但是,如果存在“重叠”的可能性,例如:
Then the time should be highlighted from 8:15 until 08:17 然后应突出显示时间,从8:15到08:17
How would you implement your code which will check wheather the current/actual time is within these ranges? 您将如何实现将检查当前/实际时间是否在这些范围内的代码?
Will you generate first a list with all start+end times for each entry and check the actual time against this list each second? 您会首先为每个条目生成一个包含所有开始和结束时间的列表,然后每秒对照此列表检查实际时间吗? (looping through all the list values each second)?
(每秒遍历所有列表值)? :-/
: - /
Or will you achieve this in another way and how? 还是您将以另一种方式实现这一目标?
EDIT: Here is what has been done so far: 编辑:这是到目前为止已完成的工作:
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;
}
}
}
The XML file looks like: XML文件如下所示:
<DigiClock>
<Config PlaySound="true" />
<Breaks>
<Time>10:00</Time>
<Time>12:00</Time>
<Time>14:30</Time>
<Time>16:30</Time>
</Breaks>
</DigiClock>
Many thanks in advance! 提前谢谢了! ;-)
;-)
As it seems you are coding a GUI application, I would go with a timer with its interval set to 1 second (or maybe 5 seconds is enough? or something in between?) and with an event handler similar to this (untested) one: 看来您正在编写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;
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.