⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ntptime.cs

📁 pureIRCd是一个用c语音写的irc(RFC459)服务,可以给您带来很多的方便
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

namespace SDP
{
    /// <summary>
    /// A helper class for converting DateTimes and TimeSpans to and from NTP timestamps.
    /// </summary>
    public static class NtpTime
    {
        private static DateTime epoch = new DateTime(1900, 1, 1, 0, 0, 0, 0);

        /// <summary>
        /// The seconds difference between NTP Time and Unix Time.
        /// </summary>
        public const long NTPCONST = 2208988800;

        /// <summary>
        /// Converts a DateTime to an NTP timestamp.
        /// </summary>
        /// <param name="date">A DateTime.</param>
        /// <returns>An NTP timestamp.</returns>
        public static long GetNtpTime(DateTime date)
        {
            return (date.Ticks / 1000) + NTPCONST;
        }

        /// <summary>
        /// Converts a TimeSpan to an NTP timestamp.
        /// </summary>
        /// <param name="time">A TimeSpan.</param>
        /// <returns>An NTP timestamp.</returns>
        public static long GetNtpTime(TimeSpan time)
        {
            return (time.Ticks / 1000) + NTPCONST;
        }

        /// <summary>
        /// Converts an NTP timestamp to a DateTime.
        /// </summary>
        /// <param name="ntpTime">The NTP timestamp.</param>
        /// <returns>A DateTime.</returns>
        public static DateTime GetDateTime(long ntpTime)
        {
            return new DateTime((ntpTime - NTPCONST) * 1000);
        }

        /// <summary>
        /// Converts time in the Typed Time format to a Time Span.
        /// 
        /// RFC 2327 says:
        /// The syntax for [Typed Time] is a number immediately followed by a single 
        /// case-sensitive character. Fractional units are not allowed - a 
        /// smaller unit should be used instead.  The following unit specification 
        /// characters are allowed:
        ///     d - days (86400 seconds)
        ///     h - minutes (3600 seconds)
        ///     m - minutes (60 seconds)
        ///     s - seconds (allowed for completeness but not recommended)
        /// 
        /// For example: 0d 2h 30m
        /// </summary>
        /// <param name="typedTimeUnit">One unit of typed time. For example: "30m"</param>
        /// <returns>A TimeSpan</returns>
        public static TimeSpan GetTimeSpan(string typedTimeUnit)
        {
            Regex r = new Regex(@"(\d+)(\w)");
            if (!r.IsMatch(typedTimeUnit)) throw new ArgumentException();
            Match m = r.Match(typedTimeUnit);
            Dictionary<string, int> d = new Dictionary<string, int>();
            d.Add("d", 0);
            d.Add("h", 0);
            d.Add("m", 0);
            d.Add("s", 0);
            string type = m.Groups[2].Value;
            if (!d.ContainsKey(type)) throw new ArgumentException();
            return new TimeSpan(d["d"], d["h"], d["m"], d["s"]);
        }

        /// <summary>
        /// Converts an NTP timestamp to a TimeSpan.
        /// </summary>
        /// <param name="ntpTime">An NTP timestamp.</param>
        /// <returns>A TimeSpan.</returns>
        public static TimeSpan GetTimeSpan(long ntpTime)
        {
            return new TimeSpan((ntpTime - NTPCONST) * 1000);
        }

        /// <summary>
        /// Gets the current time in seconds since January 1, 1900.
        /// </summary>
        /// <returns>The number of seconds since January 1, 1900.</returns>
        public static long GetTime()
        {
            NTPClient nc = new NTPClient("time.nist.gov");
            nc.Connect(false);
            return GetNtpTime(nc.OriginateTimestamp);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -