Example #1
0
        /// <summary>Returns the current date and time, just like DateTime.UtcNow.</summary>
        public DateTime GetUtcNow()
        {
            var stopWatchTicks = _stopwatch.ElapsedTicks;
            var immutable      = _immutable;

            if (stopWatchTicks < immutable.ObservedTicks + _synchronizePeriodStopwatchTicks)
            {
                return(immutable.BaseTime.AddTicks(((
                                                        stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency) / (immutable.StopWatchFrequency)));
            }
            else
            {
                var utc = DateTime.UtcNow;

                DateTime tBaseNew = immutable.BaseTime.AddTicks(((stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency) / (immutable.StopWatchFrequency));

                _immutable = new DateTimePreciseSafeImmutable(
                    utc,
                    tBaseNew,
                    stopWatchTicks,
                    ((stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency * 2) / (3 * utc.Ticks - 2 * immutable.ObservedTime.Ticks - tBaseNew.Ticks));

                return(tBaseNew);
            }
        }
        /// <summary>Creates a new instance of DateTimePrecise.
        /// A large value of synchronizePeriodSeconds may cause arithmetic overthrow
        /// exceptions to be thrown. A small value may cause the time to be unstable.
        /// A good value is 10.
        /// synchronizePeriodSeconds = The number of seconds after which the
        /// DateTimePrecise will synchronize itself with the system clock.</summary>
        public DateTimePrecise(long synchronizePeriodSeconds)
        {
            _stopwatch = Stopwatch.StartNew();
            _stopwatch.Start();

            var utc = DateTime.UtcNow;
            _immutable = new DateTimePreciseSafeImmutable(utc, utc, _stopwatch.ElapsedTicks, Stopwatch.Frequency);

            _synchronizePeriodStopwatchTicks = synchronizePeriodSeconds * Stopwatch.Frequency;
        }
Example #3
0
        /// <summary>Creates a new instance of DateTimePrecise.
        /// A large value of synchronizePeriodSeconds may cause arithmetic overthrow
        /// exceptions to be thrown. A small value may cause the time to be unstable.
        /// A good value is 10.
        /// synchronizePeriodSeconds = The number of seconds after which the
        /// DateTimePrecise will synchronize itself with the system clock.</summary>
        public DateTimePrecise(long synchronizePeriodSeconds)
        {
            _stopwatch = Stopwatch.StartNew();
            _stopwatch.Start();

            var utc = DateTime.UtcNow;

            _immutable = new DateTimePreciseSafeImmutable(utc, utc, _stopwatch.ElapsedTicks, Stopwatch.Frequency);

            _synchronizePeriodStopwatchTicks = synchronizePeriodSeconds * Stopwatch.Frequency;
        }
Example #4
0
    /// Creates a new instance of DateTimePrecise.
    /// A large value of synchronizePeriodSeconds may cause arithmetic overthrow
    /// exceptions to be thrown. A small value may cause the time to be unstable.
    /// A good value is 10.
    /// synchronizePeriodSeconds = The number of seconds after which the
    /// DateTimePrecise will synchronize itself with the system clock.
    public DateTimePrecise(long synchronizePeriodSeconds)
    {
        Stopwatch = Stopwatch.StartNew();
        this.Stopwatch.Start();
        DateTime t = DateTime.UtcNow;

        _immutable = new DateTimePreciseSafeImmutable(t, t, Stopwatch.ElapsedTicks,
                                                      Stopwatch.Frequency);
        _synchronizePeriodSeconds        = synchronizePeriodSeconds;
        _synchronizePeriodStopwatchTicks = synchronizePeriodSeconds *
                                           Stopwatch.Frequency;
        _synchronizePeriodClockTicks = synchronizePeriodSeconds *
                                       _clockTickFrequency;
    }
        /// <summary>
        /// Creates a new instance of <see cref="DateTimePrecise"/>.
        /// </summary>
        /// <param name="synchronizePeriodSeconds">
        /// The number of seconds after which the <see cref="DateTimePrecise"/>
        /// will synchronize itself with the system clock. A large value may
        /// cause arithmetic overflow exceptions to be thrown. A small value may
        /// cause the time to be unstable. A good default value is 10.
        /// </param>
        private DateTimePrecise(long synchronizePeriodSeconds)
        {
            _stopwatch = Stopwatch.StartNew();
            _stopwatch.Start();

            var t = DateTime.UtcNow;

            _immutable = new DateTimePreciseSafeImmutable(t, t, _stopwatch.ElapsedTicks,
                                                          Stopwatch.Frequency);

            //_synchronizePeriodSeconds = synchronizePeriodSeconds;
            _synchronizePeriodStopwatchTicks = synchronizePeriodSeconds *
                                               Stopwatch.Frequency;
            //_synchronizePeriodClockTicks = synchronizePeriodSeconds *
            //	ClockTickFrequency;
        }
        /// <summary>Returns the current date and time, just like DateTime.UtcNow.</summary>
        public DateTime GetUtcNow()
        {
            var stopWatchTicks = _stopwatch.ElapsedTicks;
            var immutable = _immutable;

            if (stopWatchTicks < immutable.ObservedTicks + _synchronizePeriodStopwatchTicks)
            {
                return immutable.BaseTime.AddTicks(((
                    stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency) / (immutable.StopWatchFrequency));
            }
            else
            {
                var utc = DateTime.UtcNow;

                DateTime tBaseNew = immutable.BaseTime.AddTicks(((stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency) / (immutable.StopWatchFrequency));

                _immutable = new DateTimePreciseSafeImmutable(
                    utc,
                    tBaseNew,
                    stopWatchTicks,
                    ((stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency * 2) / (3 * utc.Ticks - 2 * immutable.ObservedTime.Ticks - tBaseNew.Ticks));

                return tBaseNew;
            }
        }