资源简介
自己写的一个简单获取北京时间的程序,分享一下,若有不足,请多多指正。
代码片段和文件信息
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个文件信息
相关资源
- C#设计模式第二版.pdf
- C#聊天程序
-
c#xm
l读写demo - C#与CoDeSys共享内存例程
- C#编写的图像均衡化、去噪、高斯模糊
- 图斑净面积计算工具源代码基于ARCG
- c# splitter 隐藏左边窗口
- 北大青鸟全年题库(c#编写)
-
C#通过Socket发送ob
ject对象,序列化反 - 如鹏网2019 .NET高级开发就业班.rar
- C#简易画板
- C#设计模式手册(中文CHM)
- C# 引用不同文件夹下的Dll文件
- C#调用Delphi的dll以实现标准的AES加密解
- c#实现实时显示cmd操作
- NModbu官方VB C# 全
- C#串口通信DLLVisual Studio 2005
- C#实现一维离散小波变换
- c#高效导出Excel
- c#串口通信+CRC校验
- 人脸识别源码C#
- C#任务管理器实现cpu使用率以及动态网
- C# List用法详解
- C#线程间操作无效Study
- c#IPCO多线程并发业务处理
- C# DBHelper类
- c# 画流程图
- C# 读写Word
- C#权限管理系统--动态加载菜单栏
- C#在线考试系统软件版
评论
共有 条评论