当前位置: 首页 > 编程笔记 >

C#实现从网络同步标准北京时间的方法

顾兴昌
2023-03-14
本文向大家介绍C#实现从网络同步标准北京时间的方法,包括了C#实现从网络同步标准北京时间的方法的使用技巧和注意事项,需要的朋友参考一下

本文实例讲述了C#实现从网络同步标准北京时间的方法。分享给大家供大家参考。具体分析如下:

这段C#代码可以从http://www.time.ac.cn网站上获取标准的北京时间,只需简单的组合即可让本地服务器实时同步正确的北京时间

#region
/// <summary>
/// 获取标准北京时间
/// /// </summary>
/// /// <returns></returns>
///
public static DateTime GetStandardTime()   
{
/// //<?xml version="1.0" encoding="GB2312" ?>
//- <ntsc>
//- <time>
// <year>2013</year>
// <month>8</month>
// <day>29</day> 
// <Weekday /> 
// <hour>16</hour>
// <minite>29</minite>
// <second>12</second> 
// <Millisecond />
// </time> 
// </ntsc> 
DateTime dt;
WebRequest wrt = null;
WebResponse wrp = null;
try
{
wrt = WebRequest.Create("http://www.time.ac.cn/timeflash.asp?user=flash"); 
wrt.Credentials = CredentialCache.DefaultCredentials;
  wrp = wrt.GetResponse();
  StreamReader sr = new StreamReader(wrp.GetResponseStream(),Encoding.UTF8);
  string html = sr.ReadToEnd();
  sr.Close();
  wrp.Close();
  int yearIndex = html.IndexOf("<year>") + 6;
  int monthIndex = html.IndexOf("<month>") + 7;
  int dayIndex = html.IndexOf("<day>") + 5;
  int hourIndex = html.IndexOf("<hour>") + 6;
  int miniteIndex = html.IndexOf("<minite>") + 8;
  int secondIndex = html.IndexOf("<second>") + 8;
  string year = html.Substring(yearIndex, html.IndexOf("</year>") - yearIndex);
  string month = html.Substring(monthIndex, html.IndexOf("</month>") - monthIndex);
  string day = html.Substring(dayIndex, html.IndexOf("</day>") - dayIndex);
  string hour = html.Substring(hourIndex, html.IndexOf("</hour>") - hourIndex);
  string minite = html.Substring(miniteIndex, html.IndexOf("</minite>") - miniteIndex);
  string second = html.Substring(secondIndex, html.IndexOf("</second>") - secondIndex);
  dt = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second);
}
catch (WebException)
{
  return DateTime.Parse("2013-1-1");
}
catch (Exception)
{
  return DateTime.Parse("2013-1-1");
}
finally
{
  if (wrp != null)
 wrp.Close();
  if (wrt != null)
 wrt.Abort();
}
   return dt;
 }
#endregion

希望本文所述对大家的C#程序设计有所帮助。

 类似资料:
  • 本文向大家介绍python实现定时同步本机与北京时间的方法,包括了python实现定时同步本机与北京时间的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了python实现定时同步本机与北京时间的方法。分享给大家供大家参考。具体如下: 这段python代码首先从www.beijing-time.org上获取标准的北京时间,然后同步获取的北京时间到本地 希望本文所述对大家的Python程序

  • 本文向大家介绍python获取标准北京时间的方法,包括了python获取标准北京时间的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了python获取标准北京时间的方法。分享给大家供大家参考。具体分析如下: 这段python代码主要通过www.beijing-time.org的官网上获取标准的北京时间,如果你的服务器挂在网上,你可以通过这段代码定时获取北京时间,然后更新自己系统的标准时

  • 本文向大家介绍Python检查和同步本地时间(北京时间)的实现方法,包括了Python检查和同步本地时间(北京时间)的实现方法的使用技巧和注意事项,需要的朋友参考一下 背景 有时本地服务器的时间不准了,需要同步互联网上的时间。 解决方案 NTP时间同步,找到一些可用的NTP服务器进行同步即可。 通过获取一些大型网站的时间来同步为自己的时间。 * 由于NTP时间同步,如果相差比如有好几个小时,那么时

  • 本文向大家介绍javascript实时显示北京时间的方法,包括了javascript实时显示北京时间的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了javascript实时显示北京时间的方法。分享给大家供大家参考。具体如下: 该页面中实时显示北京时间,更改时区也可以作为显示世界时间,代码如下: 希望本文所述对大家的javascript程序设计有所帮助。

  • 本文向大家介绍Android中实现用命令行同步网络时间,包括了Android中实现用命令行同步网络时间的使用技巧和注意事项,需要的朋友参考一下 一、简介 Android基于Linux平台的开源手机操作系统。 二、原理 既然是Linux,那就应该支持linux的各种命令行,高度的可配置,但实验发现Android是Google的一个高度阉割版的linux,很多命令都不支持,如rdate命令(网络同步时

  • 问题内容: 我正在编写自己的ContentProvider,它将使用SyncAdapter同步到Web服务。 当同步适配器修改内容提供者的数据时,问题会发生,当内部调用getContentResolver()。notifyChange导致同步循环时,提供者触发网络同步。 客户端应用程序进行修改时,需要带有网络同步标志的notifyChange,但在修改同步适配器时应避免使用notifyChange