Сравнение DateTime - Почему сравнение запаздывает на 1 мин - C#
Формулировка задачи:
public DateTime NextShort(DateTime cur, int ofset, int period)
{
var temp = (cur.Minute / period) * period + ofset;
return new DateTime(cur.Year, cur.Month, cur.Day, cur.Hour, temp, 0);
}
public void timer_Tick(object sender, EventArgs e) //тик = 1c
{
count_timer = DateTime.Now;
if (count_timer >= short_opros)
{
short_opros = NextShort(count_timer, 2, 3);
}
}
short_opros = 03:11:00
эта конструкция if (count_timer >= short_opros) срабатывает при count_timer 03:12:01
Решение задачи: «Сравнение DateTime - Почему сравнение запаздывает на 1 мин»
textual
Листинг программы
public partial class Form1 : Form {
DateTime count_timer, short_opros;
public Form1() {
InitializeComponent();
short_opros = DateTime.Now.AddSeconds(5);
timer.Interval = 1000;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e) {
count_timer = DateTime.Now;
if (count_timer >= short_opros) {
timer.Stop();
MessageBox.Show(string.Format("Now: {0}\r\nshort_opros: {1}", count_timer, short_opros));
}
}
}