Exemplo n.º 1
0
        private void ConfigureTimeProvider()
        {
            timeProvider.TimeAvailable += (DateTime dateTime) => {
                int minimumUpdateIntervalSeconds = 0;

                Invoke(new Action(() => {
                    minimumUpdateIntervalSeconds = (int)numericUpDownMimimumUpdateInterval.Value;
                }));

                if (Environment.TickCount - lastSystemTimeUpdate < minimumUpdateIntervalSeconds * 1000)
                {
                    return;
                }

                // Positive error means system clock is ahead, negative error means system clock is behind
                TimeSpan error = SystemTimeUtils.GetSystemTime().Subtract(dateTime).Duration();

                if (checkBoxMaximumCorrectionEnabled.Checked)
                {
                    TimeSpan maximumCorrection      = new TimeSpan();
                    int      maximumCorrectionValue = (int)numericUpDownMaximumCorrection.Value;
                    string   maximumCorrectionUnit  = "";

                    Invoke(new Action(() => {
                        maximumCorrectionUnit = (string)comboBoxMaximumCorrectionUnit.SelectedItem;
                    }));

                    if (maximumCorrectionUnit == "hour(s)")
                    {
                        maximumCorrection = TimeSpan.FromHours(maximumCorrectionValue);
                    }
                    else if (maximumCorrectionUnit == "minute(s)")
                    {
                        maximumCorrection = TimeSpan.FromMinutes(maximumCorrectionValue);
                    }
                    else if (maximumCorrectionUnit == "second(s)")
                    {
                        maximumCorrection = TimeSpan.FromSeconds(maximumCorrectionValue);
                    }

                    if (error >= maximumCorrection)
                    {
                        Invoke(new Action(() => {
                            AddMessageToLog("System time error exceeded maximum correction: time not set.", LogLevel.Info);
                        }));

                        return;
                    }
                }

                SystemTimeUtils.SetSystemTime(dateTime);

                Invoke(new Action(() => {
                    AddMessageToLog(string.Format(
                                        "System time set to {0} ({1}).",
                                        dateTime.ToString("HH:mm:ss dd\\/MM\\/yyyy"), error.ToString()
                                        ), LogLevel.Info);
                }));

                lastSystemTimeUpdate = Environment.TickCount;
            };

            timeProvider.Log += (string message, LogLevel logLevel) => {
                Invoke(new Action(() => {
                    AddMessageToLog(message, logLevel);
                }));
            };
        }
Exemplo n.º 2
0
        private void timerClockDisplayUpdate_Tick(object sender, EventArgs e)
        {
            DateTime systemTime = SystemTimeUtils.GetSystemTime();

            labelCurrentDateTime.Text = systemTime.ToString("HH:mm:ss\ndd\\/MM\\/yyyy\nUTC");
        }