C#获取北京时间与设置系统时间

时间:2022-09-07 09:54:38

获取北京时间

 public static DateTime GetBeijingTime()
{
DateTime dt; // 返回国际标准时间
// 只使用 timeServers 的 IP 地址,未使用域名
try
{
string[,] timeServers = new string[, ];
int[] searchOrder = { , , , , , , , , , , , , };
timeServers[, ] = "time-a.nist.gov";
timeServers[, ] = "129.6.15.28";
timeServers[, ] = "time-b.nist.gov";
timeServers[, ] = "129.6.15.29";
timeServers[, ] = "time-a.timefreq.bldrdoc.gov";
timeServers[, ] = "132.163.4.101";
timeServers[, ] = "time-b.timefreq.bldrdoc.gov";
timeServers[, ] = "132.163.4.102";
timeServers[, ] = "time-c.timefreq.bldrdoc.gov";
timeServers[, ] = "132.163.4.103";
timeServers[, ] = "utcnist.colorado.edu";
timeServers[, ] = "128.138.140.44";
timeServers[, ] = "time.nist.gov";
timeServers[, ] = "192.43.244.18";
timeServers[, ] = "time-nw.nist.gov";
timeServers[, ] = "131.107.1.10";
timeServers[, ] = "nist1.symmetricom.com";
timeServers[, ] = "69.25.96.13";
timeServers[, ] = "nist1-dc.glassey.com";
timeServers[, ] = "216.200.93.8";
timeServers[, ] = "nist1-ny.glassey.com";
timeServers[, ] = "208.184.49.9";
timeServers[, ] = "nist1-sj.glassey.com";
timeServers[, ] = "207.126.98.204";
timeServers[, ] = "nist1.aol-ca.truetime.com";
timeServers[, ] = "207.200.81.113";
timeServers[, ] = "nist1.aol-va.truetime.com";
timeServers[, ] = "64.236.96.53";
int portNum = ;
byte[] bytes = new byte[];
int bytesRead = ;
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
for (int i = ; i < ; i++)
{
string hostName = timeServers[searchOrder[i], ];
try
{
client.Connect(hostName, portNum);
System.Net.Sockets.NetworkStream ns = client.GetStream();
bytesRead = ns.Read(bytes, , bytes.Length);
client.Close();
break;
}
catch (Exception)
{
// ignored
}
}
char[] sp = new char[];
sp[] = ' ';
dt = new DateTime();
string str1 = System.Text.Encoding.ASCII.GetString(bytes, , bytesRead); string[] s = str1.Split(sp);
if (s.Length >= )
{
dt = DateTime.Parse(s[] + " " + s[]); // 得到标准时间
dt = dt.AddHours(); // 得到北京时间
}
else
{
dt = DateTime.Parse("2016-1-1");
}
}
catch (Exception)
{
dt = DateTime.Parse("2016-1-1");
}
return dt;
}

设置本地系统时间

 [DllImport("kernel32.dll")]
private static extern bool SetLocalTime(ref Systemtime time); [StructLayout(LayoutKind.Sequential)]
private struct Systemtime
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
} public static bool SetDate(DateTime dt)
{
Systemtime st; st.year = (short)dt.Year;
st.month = (short)dt.Month;
st.dayOfWeek = (short)dt.DayOfWeek;
st.day = (short)dt.Day;
st.hour = (short)dt.Hour;
st.minute = (short)dt.Minute;
st.second = (short)dt.Second;
st.milliseconds = (short)dt.Millisecond; bool rt = SetLocalTime(ref st);
return rt;
}

C#获取北京时间与设置系统时间的更多相关文章

  1. Qt设置系统时间(使用SetSystemTime API函数)

    大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime();  ...

  2. ubuntu设置系统时间与网络时间同步和时区

    Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...

  3. centos7设置系统时间与网络时间同步

    Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...

  4. 使用AIDL调用远程服务设置系统时间

    在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的.于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间 ...

  5. date 显示或设置系统时间和日期

    显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...

  6. Linux 设置系统时间和日期 API

    嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码. Linux设置时间和日期的步骤: 1. 设置系统时间和日期: 2. 该系统的时间和日期,同步到硬件. #include ...

  7. ubuntu设置系统时间与网络时间同步

    ubuntu设置系统时间与网络时间同步   Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC).   系统时间:指当前Linux Ker ...

  8. delphi中设置系统时间方法

    procedure TMainFrm.Timer1Timer(Sender: TObject); var   systemtime:Tsystemtime;   dt:TDateTime; begin ...

  9. CentOS设置系统时间、硬件时间、以及定时校对时间

    CentOS设置系统时间和时区 一.设置时区 方法一:使用setup工具 setup 选择Timezone configuration 选择Asia/Shanghai 空格键勾选上System clo ...

随机推荐

  1. PHP进程通信基础——信号量&plus;共享内存通信

    PHP进程通信基础--信号量+共享内存通信 由于进程之间谁先执行并不确定,这取决于内核的进程调度算法,其中比较复杂.由此有可能多进程在相同的时间内同时访问共享内存,从而造成不可预料的错误.信号量这个名 ...

  2. how-to-install-siege-on-centos-7

    https://www.joedog.org/siege-home/ https://roastahost.com/how-to-install-siege-on-centos-7/ (Works!) ...

  3. 服务器后台TCP连接存活问题

    0. 背景 公司的服务器后台部署在某一个地方,接入的是用户的APP,而该地方的网络信号较差,导致了服务器后台在运行一段时间后用户无法接入,那边的同事反馈使用netstat查看系统,存在较多的TCP连接 ...

  4. rsync使用

    1)拷贝本地文件.当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式.     如:rsync -a  ./test.c  /backup 2)使用一个远程 ...

  5. Exception-异常

    异常(Exception)是程序执行过程中所产生的问题 产生原因:用户输入无效数字.找不到需要打开的文件.在通讯中网络连接中断.JVM发生了内存溢出 异常的三个种类:检查异常.运行时异常.错误(类似异 ...

  6. 01-事件处理简介&sol;UIView拖拽

    1.ios当中常见的事件?         触摸事件        加速计事件         远程控制事件2.什么是响应者对象?     继承了UIResponds的对象我们称它为响应者对象 UIA ...

  7. Hdu 3887 Counting Offspring &bsol; Poj 3321 Apple Tree &bsol;BZOJ 1103 &lbrack;POI2007&rsqb;大都市meg

    这几个题练习DFS序的一些应用. 问题引入: 给定一颗n(n <= 10^5)个节点的有根树,每个节点标有权值,现有如下两种操作: 1.C x y     以节点x的权值修改为y. 2.Q x ...

  8. HDU-1869六度分离

    Problem Description 1967 年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不 ...

  9. Python &colon; Module

    在Python中,一个.py文件代表一个Module.在Module中可以是任何的符合Python文件格式的Python脚本.了解Module导入机制大有用处. 1 Module 组成 1.1 Mod ...

  10. 冲刺NO&period;11

    Alpha冲刺第十一天 站立式会议 项目进展 项目进入尾声,主要测设工作完成过半,项目总结也开始进行. 问题困难 项目的困难现阶段主要是测试过程中存在一些"盲点"很难发现或者发现后 ...