获取北京时间

 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. Scala学习(二)

    二.Scala基础 1.变量:三种修饰符 -> ①val 常亮②var 变量③lazy val 惰性变量求值 2.类型 3.代码块 Block {exp1;exp2} 或 { exp1 exp2 ...

  2. Android 判断字符串是否为空

    TextUtils.isEmpty(str) 可以判断字符串是否为null或者"",当是的时候为true,否的时候为false

  3. C#获取并写入ORACLE数据库中中英文字符集问题

    背景: 开发语言:C# 开发工具:VS2010 A方ORACLE数据库:中文字符集 B方ORACLE数据库:英文字符集 传递方式:webservice方式(取数据,并把取出的数据放到DataTable ...

  4. C#读写config配置文件

    应用程序配置文件(App.config)是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 对于一个config ...

  5. 数位DP入门

    HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...

  6. mydumper 快速高效备份mysql,按照表生成备份文件,快速恢复

    Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具.开发人员主要来自MySQL,Facebook,SkySQL公司.目前已经在一些线上使用了Mydumper. Mydum ...

  7. .htaccess下Flags速查表

    Flags是可选参数,当有多个标志同时出现时,彼此间以逗号分隔. 速查表: RewirteRule 标记 含义 描述 R Redirect 发出一个HTTP重定向 F Forbidden 禁止对URL ...

  8. IT技术学习指导之Linux系统入门的4个阶段(纯干货带图)

    IT技术学习指导之Linux系统入门的4个阶段(纯干货带图) 全世界60%的人都在使用Linux.几乎没有人没有受到Linux系统的"恩惠",我们享受的大量服务(包括网页服务.聊天 ...

  9. STEP模块——电子钟

    原理 显示时分秒(日期也可以加上),两个按键调节时间 原理图 代码 /*---------------------------------------------------------------- ...

  10. 转: Rest简介及Spring实现

    一 Roy Fielding 2000年Rest被Roy Fielding提出来的,我对Roy Fielding的印象有以下几个. 一是RoyFielding做为Http协议的起草者,在Http协议发 ...