获取北京时间

 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. ionic 通过PouchDB + SQLite来实现app的本地存储(Local Storage)

    首先声明,本教程参考国外网站(http://gonehybrid.com/how-to-use-pouchdb-sqlite-for-local-storage-in-your-ionic-app/) ...

  2. PSP记录个人项目耗时情况

    四则运算编程 PSP记录个人项目耗时情况 PSP Personal Software Process Stages Time(%) Planning 计划 7 Estimate 估计这个任务需要多少时 ...

  3. Java中sql语句的引号问题

    1..sql语句 在数据库中,当我们查询语句时,会使用类似的语句: Select * from userinfo where userid='1' or 1; Select * from userin ...

  4. ios基础篇(三十)—— AFNetworking的使用

    一.AFNetworking的创建 1.新建工程,命名为AFNDemo 2.导入AFNetworking.h AFNetworking文件下载:https://github.com/AFNetwork ...

  5. 转:solr6.0配置中文分词器IK Analyzer

    solr6.0中进行中文分词器IK Analyzer的配置和solr低版本中最大不同点在于IK Analyzer中jar包的引用.一般的IK分词jar包都是不能用的,因为IK分词中传统的jar不支持s ...

  6. C# 自动生成代码API文档

    暂时没学会正规的转载style临时记录一下 NET中的规范标准注释(一) -- XML注释标签讲解 http://www.cnblogs.com/mq0036/p/3544264.html NET中的 ...

  7. 单独部署activemq-web-console (转载)

    activemq-web-console的默认使用方式是通过在activemq.xml中导入jetty.xml配置一个jetty server来实现的. 其实activemq-web-console完 ...

  8. TFS Workspace 更改电脑名称

    不小心改了计算机名称 导致VS在保存项目的时候,包如下错误: 解决方法: 第一步: 第二步:输入如下片段 tf workspaces /updateComputerName:旧计算机名称  /coll ...

  9. IOS Core Animation Advanced Techniques的学习笔记(五)

    第六章:Specialized Layers   类别 用途 CAEmitterLayer 用于实现基于Core Animation粒子发射系统.发射器层对象控制粒子的生成和起源 CAGradient ...

  10. 端口限制情况下php+xdebug环境配置

    PHP程序在开发的时候调试是比较方便的,大体情况下,输出,打log是可以解决几乎所有问题. 但是还不够,有些问题,用打log的形式定位问题是相当痛苦的事情,有些时候测试环境没配好的话,你可能需要做许多 ...