Пример #1
0
        private async Task <Instant> GetNetworkTimeAsync()
        {
            // http://stackoverflow.com/a/12150289

            // NTP message size - 16 bytes of the digest (RFC 2030)
            var ntpData = new byte[48];

            //Setting the Leap Indicator, Version Number and Mode values
            ntpData[0] = 0x23; // 0010 0011 : LI = 0 (no warning), VN = 4 (IPv4 or IPv6), Mode = 3 (Client Mode)

            var ipEndPoint = await ResolveIPEndPointAsync().ConfigureAwait(false);

            using (var socket = new Socket(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp))
            {
                await socket.ConnectAsync(ipEndPoint).ConfigureAwait(false);

                //Stops code hang if NTP is blocked
                socket.ReceiveTimeout = 3000;

                var buffer = new ArraySegment <byte>(ntpData);
                await socket.SendAsync(buffer, SocketFlags.None).ConfigureAwait(false);

                await socket.ReceiveAsync(buffer, SocketFlags.None).ConfigureAwait(false);
            }

            //Offset to get to the "Transmit Timestamp" field (time at which the reply
            //departed the server for the client, in 64-bit timestamp format."
            const byte serverReplyTime = 40;

            //Get the seconds part
            ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);

            //Get the seconds fraction
            ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);

            //Convert From big-endian to little-endian
            intPart   = SwapEndianness(intPart);
            fractPart = SwapEndianness(fractPart);

            var milliseconds = (long)(intPart * 1000 + fractPart * 1000 / 0x100000000L);
            var instant      = Instant.FromUtc(1900, 1, 1, 0, 0) + Duration.FromMilliseconds(milliseconds);

            return(instant);
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Instant" /> struct based
 /// on a number of milliseconds since the Unix epoch of (ISO) January 1st 1970, midnight, UTC.
 /// </summary>
 /// <param name="milliseconds">Number of milliseconds since the Unix epoch. May be negative (for instants before the epoch).</param>
 /// <returns>An <see cref="Instant"/> at exactly the given number of milliseconds since the Unix epoch.</returns>
 /// <exception cref="ArgumentOutOfRangeException">The constructed instant would be out of the range representable in Noda Time.</exception>
 public static Instant FromUnixTimeMilliseconds(long milliseconds)
 {
     Preconditions.CheckArgumentRange(nameof(milliseconds), milliseconds, MinMilliseconds, MaxMilliseconds);
     return(Instant.FromTrustedDuration(Duration.FromMilliseconds(milliseconds)));
 }
Пример #3
0
 public ZonedDateTime PlusMilliseconds(long milliseconds) => this + Duration.FromMilliseconds(milliseconds);
Пример #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Instant" /> struct based
 /// on a number of milliseconds since the Unix epoch of (ISO) January 1st 1970, midnight, UTC.
 /// </summary>
 /// <param name="milliseconds">Number of milliseconds since the Unix epoch. May be negative (for instants before the epoch).</param>
 /// <returns>An <see cref="Instant"/> at exactly the given number of milliseconds since the Unix epoch.</returns>
 /// <exception cref="ArgumentOutOfRangeException">The constructed instant would be out of the range representable in Noda Time.</exception>
 public static Instant FromMillisecondsSinceUnixEpoch(long milliseconds)
 {
     Preconditions.CheckArgumentRange(nameof(milliseconds), milliseconds, MinMilliseconds, MaxMilliseconds);
     return(new Instant(Duration.FromMilliseconds(milliseconds)));
 }
Пример #5
0
 /// <summary>
 /// From milliseconds
 /// </summary>
 /// <param name="milliseconds"></param>
 /// <returns></returns>
 public static Duration AsDurationOfMilliseconds(this double milliseconds) => Duration.FromMilliseconds(milliseconds);