• 大小: 57KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: C#
  • 标签: C#  

资源简介

自己写的一个简单获取北京时间的程序,分享一下,若有不足,请多多指正。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;




namespace WindowsFormsApplication28
{
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {
            
        }
        public static DateTime GetNetworkTime()
        {
            //default Windows time server
            const string ntpServer = “time.windows.com“;

            // 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] = 0x1B; //LI = 0 (no warning) VN = 3 (IPv4 only) Mode = 3 (Client Mode)

            var addresses = Dns.GetHostEntry(ntpServer).AddressList;

            //The UDP port number assigned to NTP is 123
            var ipEndPoint = new IPEndPoint(addresses[0] 123);
            //NTP uses UDP
            var socket = new Socket(AddressFamily.InterNetwork SocketType.Dgram ProtocolType.Udp);

            socket.Connect(ipEndPoint);

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

            socket.Send(ntpData);
            socket.Receive(ntpData);
            socket.Close();

            //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 = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);

            //**UTC** time
            var networkDateTime = (new DateTime(1900 1 1 0 0 0 DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
           
            return networkDateTime.ToLocalTime();
        }

        // stackoverflow.com/a/3294698/162671
        static uint SwapEndianness(ulong x)
        {
            return (uint)(((x & 0x000000ff) << 24) +
                           ((x & 0x0000ff00) << 8) +
                           ((x & 0x00ff0000) >> 8) +
                           ((x & 0xff000000) >> 24));
           
        }

        private void timer1_Tick(object sender EventArgs e)
        {
            GetNetworkTime();
            label1.Text = GetNetworkT

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-04-04 22:05  标准时间\
     目录           0  2015-04-04 22:05  标准时间\WindowsFormsApplication28\
     目录           0  2015-04-04 22:05  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\
     文件         187  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\App.config
     文件        2992  2015-04-04 22:01  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Form1.Designer.cs
     文件        3068  2015-04-04 22:01  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Form1.cs
     文件        6011  2015-04-04 22:01  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Form1.resx
     文件         537  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Program.cs
     目录           0  2015-04-04 22:05  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\
     文件        1374  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\AssemblyInfo.cs
     文件        2902  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\Resources.Designer.cs
     文件        5612  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\Resources.resx
     文件        1112  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\Settings.Designer.cs
     文件         249  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\Settings.settings
     文件        3830  2015-04-04 21:21  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\WindowsFormsApplication28.csproj
     目录           0  2015-04-04 22:05  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\
     目录           0  2015-04-04 22:05  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\
     文件        9728  2015-04-04 22:01  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.exe
     文件         187  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.exe.config
     文件       26112  2015-04-04 22:01  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.pdb
     文件       23168  2015-04-04 22:04  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.vshost.exe
     文件         187  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.vshost.exe.config
     文件         490  2013-06-18 20:28  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.vshost.exe.manifest
     目录           0  2015-04-04 22:05  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\
     目录           0  2015-04-04 22:05  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\
     文件         865  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7136  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2015-04-07 15:10  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\TempPE\
     文件           0  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2015-04-04 21:20  标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
............此处省略9个文件信息

评论

共有 条评论