[英]Determine if current time falls within multiple time ranges
我編寫了一個簡單的應用程序來顯示當前時間(HH:MM)。 顯示的時間每秒鍾刷新一次DispatcherTimer。
考慮到我有一個XML格式的“配置”文件,其中包含一個小時列表,該小時列表必須以不同的顏色突出顯示5分鍾:
例:
但是,如果存在“重疊”的可能性,例如:
然后應突出顯示時間,從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>
提前謝謝了! ;-)
看來您正在編寫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.