Exemple #1
0
        private void Synchronize()
        {
            try {
                NistClock nistClock = new NistClock();
                TimeSpan  error     = nistClock.SynchronizeLocalClock();
                prevAdjustment = error.TotalSeconds;
                syncErrorText  = string.Empty;  // clear any prior error text
                prevSyncTime   = DateTime.Now;
            } catch (Exception) {
                syncErrorText = "There was an error trying to synchronize with the NIST timeserver.\nAre you connected to the Internet?";
            }

            immediateSyncRequsted = false;
            SetControlPropertyValue(SyncButton, "Enabled", true);
        }
Exemple #2
0
        /// <summary>
        /// Sync the Clock to Internet Time
        /// </summary>
        /// <returns>True if Successfull False if Failed</returns>
        static public bool SynchronizeTime()
        {
            bool synced = false;

            try
            {
                NistClock c = new NistClock();
                c.SynchronizeLocalClock();
                synced = true;
            }
            catch
            {
            }

            return(synced);
        }
        /// <summary>
        /// Sync the Clock to Internet Time
        /// </summary>
        /// <returns>True if Successfull False if Failed</returns>
        static public bool SynchronizeTime()
        {
            bool synced = false;

            try
            {
                NistClock c = new NistClock();
                c.SynchronizeLocalClock();
                synced = true;
            }
            catch
            {

            }

            return synced;
        }
Exemple #4
0
        static int Main(string[] args)
        {
            int i;

            IPAddress hostIpAddress = IPAddress.Parse("132.163.4.101");
            NistClock nistClock     = new NistClock(hostIpAddress);

            nistClock.PrintDiagnosticMessages = true;

            if (args.Length == 0)
            {
                PrintUsage();
            }
            else
            {
                if (args[0] == "msync")
                {
                    int numberOfSamples = 10;
                    if (args.Length >= 2)
                    {
                        numberOfSamples = int.Parse(args[1]);
                    }
                    if (numberOfSamples < 3)
                    {
                        Console.WriteLine("Illegal number of samples = {0}", numberOfSamples);
                        return(1);
                    }

                    double[] measurement = new double[numberOfSamples];

                    int minIndex = 0;
                    int maxIndex = 0;

                    for (i = 0; i < numberOfSamples; ++i)
                    {
                        if (i > 0)
                        {
                            System.Threading.Thread.Sleep(2000);    // avoid spamming NIST and pissing them off!
                        }
                        TimeSpan diff = nistClock.MeasureSystemClockError();
                        measurement[i] = diff.TotalSeconds;
                        if (measurement[i] < measurement[minIndex])
                        {
                            minIndex = i;
                        }
                        if (measurement[i] > measurement[maxIndex])
                        {
                            maxIndex = i;
                        }
                        Console.WriteLine("Trial #{0}:  {1,8:0.0000}", i, measurement[i]);
                    }

                    double sum          = 0.0; // sum of differences, expressed in seconds
                    double maxRemaining = -666.0e+6;
                    double minRemaining = -666.0e+6;
                    bool   firstRemainingMeasurement = true;
                    int    numSamplesKept            = 0;
                    for (i = 0; i < numberOfSamples; ++i)
                    {
                        if (i == minIndex || i == maxIndex)
                        {
                            // Ignore this data point
                            if (firstRemainingMeasurement)
                            {
                                firstRemainingMeasurement = false;
                                minRemaining = maxRemaining = measurement[i];
                            }
                            else
                            {
                                if (measurement[i] < minRemaining)
                                {
                                    minRemaining = measurement[i];
                                }
                                if (measurement[i] > maxRemaining)
                                {
                                    maxRemaining = measurement[i];
                                }
                            }
                        }
                        else
                        {
                            sum += measurement[i];
                            ++numSamplesKept;      // note that minIndex could be the same as maxIndex, so we cannot assume numSamplesKept == numberOfSamples - 2.
                        }
                    }
                    double average = sum / numSamplesKept;
                    double spread  = maxRemaining - minRemaining;

                    Console.WriteLine("Excluding minimum = {0,8:0.0000} and maximum = {1,8:0.0000}", measurement[minIndex], measurement[maxIndex]);
                    Console.WriteLine("Spread of remaining data = {0,8:0.0000} seconds.", spread);
                    DateTime adjusted = DateTime.Now.AddSeconds(average);
                    NistClock.SetTimeUtc(adjusted.ToUniversalTime());
                    Console.WriteLine("Average difference = {0,8:0.0000}", average);
                    Console.WriteLine("Set time to: {0}", adjusted);
                }
                else
                {
                    TimeSpan diff = nistClock.MeasureSystemClockError();
                    DateTime here = DateTime.Now.ToUniversalTime();
                    DateTime nist = here + diff;

                    switch (args[0])
                    {
                    case "read":
                        Console.WriteLine("NIST = {0}", MyFormat(nist));
                        Console.WriteLine("Here = {0}", MyFormat(here));
                        Console.WriteLine("diff = {0,8:0.0000} seconds", diff.TotalSeconds);
                        break;

                    case "sync":
                        NistClock.SetTimeUtc(nist);
                        Console.WriteLine("Set system clock to: {0}", MyFormat(nist));
                        Console.WriteLine("This required adding {0,8:0.0000} seconds to the system clock.", diff.TotalSeconds);
                        break;

                    default:
                        Console.WriteLine("ERROR:  Unknown command '{0}'", args[0]);
                        Console.WriteLine("Run NistClock.exe with no command line parameters for help.");
                        return(1);
                    }
                }
            }

            return(0);
        }