using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
namespace 线程获取时间戳转换时间
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string b = "";//时间戳返回值
System.Timers.Timer timer = new System.Timers.Timer();//线程时间
private delegate void weituo();//委托
private void Form1_Load(object sender, EventArgs e)
{
timer.Elapsed += new ElapsedEventHandler(delegate {
time();
});
timer.Interval = 1;
timer.Start();
}
public void time()
{
WebClient a = new WebClient();
a.Encoding = Encoding.GetEncoding("GB2312");
Uri uri = new Uri("http://www.hko.gov.hk/cgi-bin/gts/time5a.pr");//获取时间戳
b = Encoding.UTF8.GetString(a.DownloadData(uri));
b = b.Replace("0=", "");
double time = Convert.ToDouble(b);
DateTime now = ConvertIntDateTime(time);
if (label1.InvokeRequired)
{
weituo label = delegate {
label1.Text = now.ToString();
};
label1.Invoke(label);
}
}
public static DateTime ConvertIntDateTime(double d)
{
System.DateTime time = System.DateTime.MinValue;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
time = startTime.AddMilliseconds(d);
return time;
}//时间戳转换
}
}