Пример #1
0
        public static int Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();

            ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

            log.Debug("Service starting.");

            if (args.Length < 1)
            {
                RunService();
                return(0);
            }

            var timeServer = args[0];
            var client     = new NtpClient(timeServer, 124);
            var response   = client.SendAsync().Result;

            var clockOffset      = response.GetSystemClockOffset();
            var adjustedDateTime = DateTime.Now.AddSeconds(clockOffset);
            var offsetSign       = clockOffset >= 0 ? "+" : string.Empty;

            //2011-08-04 00:40:36.642222 (+0000) +0.006611 +/- 0.041061 psp-os1 149.20.68.26 s3 no-leap
            var output = string.Format(
                "{0} {1}{2}",
                adjustedDateTime.ToString("yyyy-MM-dd hh:mm:ss.ffffff"),
                offsetSign,
                clockOffset);

            log.Debug(output);
            log.Debug(response.ToString());

            return(0);
        }
Пример #2
0
        public static int Main(string[] args)
        {
            //RunService();

            if (args.Length < 1)
            {
                Console.WriteLine("Usage: sntp.exe servername");
            }

            var timeServer = args[0];
            var client     = new NtpClient(timeServer, 124);
            var response   = client.SendAsync().Result;

            var clockOffset      = response.GetSystemClockOffset();
            var adjustedDateTime = DateTime.Now.AddSeconds(clockOffset);
            var offsetSign       = clockOffset >= 0 ? "+" : string.Empty;

            //2011-08-04 00:40:36.642222 (+0000) +0.006611 +/- 0.041061 psp-os1 149.20.68.26 s3 no-leap
            var output = string.Format(
                "{0} {1}{2}",
                adjustedDateTime.ToString("yyyy-MM-dd hh:mm:ss.ffffff"),
                offsetSign,
                clockOffset);

            Console.WriteLine(output);
            Console.WriteLine();
            Console.WriteLine(response.ToString());

            return(0);
        }
Пример #3
0
        protected async Task PollForTime()
        {
            while (!this.cancellationTokenSource.Token.IsCancellationRequested)
            {
                try
                {
                    log.DebugFormat("Requesting time data from the time source {0}.", this.server);

                    var client = new NtpClient(this.server, this.port);
                    log.Debug("before send.");
                    var response = await client.SendAsync();

                    log.Debug("before getsystemclock.");
                    var offset = response.GetSystemClockOffset();

                    log.DebugFormat("Received time data from the time source {0}. The difference from the system time is {1} seconds.", this.server, offset);

                    this.SetLastPollTime(DateTime.UtcNow);

                    // Update system time
                    if (this.ShouldSetClock(TimeSpan.FromSeconds(offset)))
                    {
                        IClock clock = new WindowsClock();
                        log.DebugFormat("Synchronizing the system time with time source {0}.", this.server);
                        var clockSet = clock.SetTimeUtc(DateTime.UtcNow.AddSeconds(offset));
                        if (clockSet)
                        {
                            this.SetLastUpdateTime(DateTime.UtcNow);
                            log.DebugFormat("The system time was updated by {0} seconds.", offset);
                        }
                        else
                        {
                            var error = clock.GetLastError();
                            log.DebugFormat("The system time could not be updated. Error code: {0}.", error);
                        }
                    }

                    log.Debug(response.ToString());
                }catch (Exception e)
                {
                    log.ErrorFormat("Exception:{0}", e.Message);
                }
                await Task.Delay(TimeSpan.FromSeconds(this.pollingInterval));
            }
        }