C# 时间更新器

C# 时间更新器

       private void button1_Click(object sender, EventArgs e)
        {
            //DateTime dt1 = new DateTime(1594363972686);
            DateTime dt = this.GetTaoBaoTimeAPI();
            bool falg = UpdateTime.SetDate(dt);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DateTime dt = this.GetbeijingAPI();
            bool falg = UpdateTime.SetDate(dt);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            DateTime dt = this.GetSuningTimeAPI();
            bool falg = UpdateTime.SetDate(dt);
        }
        /// <summary>
        /// 淘宝
        /// </summary>
        /// <returns></returns>
        private DateTime GetTaoBaoTimeAPI()
        {
            string url = "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp";
            HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
            hwr.Method = "get";
            hwr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            hwr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.3 Safari/537.36";
            DateTime dt;
            try
            {
                HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
                string webtime = new StreamReader(hws.GetResponseStream()).ReadToEnd();
                long times = long.Parse(Regex.Match(webtime, "\\d{13}").Value);
                //TimeSpan timeSpan = new TimeSpan(times);
                DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                dt = startTime.AddMilliseconds(times);
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// beijing-time
        /// </summary>
        /// <returns></returns>
        private DateTime GetbeijingAPI()
        {
            string url = "http://time1909.beijing-time.org/time.asp";
            HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
            hwr.Method = "get";
            hwr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            hwr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.3 Safari/537.36";
            DateTime dt;
            try
            {
                HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
                string webtime = new StreamReader(hws.GetResponseStream()).ReadToEnd();
                string[] dtstrs = webtime.Split(';');
                string nyear = string.Empty, nmonth = string.Empty, nday = string.Empty, nhrs = string.Empty, nmin = string.Empty, nsec = string.Empty;
                foreach (string item in dtstrs)
                {
                    if (item.Contains("nyear"))
                    {
                        nyear = Regex.Match(item.Replace("\r\n", ""), "[\\d]+", RegexOptions.Multiline).Value;
                    }
                    else if (item.Contains("nmonth"))
                    {
                        nmonth = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
                    }
                    else if (item.Contains("nday"))
                    {
                        nday = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
                    }
                    else if (item.Contains("nhrs"))
                    {
                        nhrs = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
                    }
                    else if (item.Contains("nmin"))
                    {
                        nmin = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
                    }
                    else if (item.Contains("nsec"))
                    {
                        nsec = Regex.Match(item, "[\\d]+", RegexOptions.Multiline).Value;
                    }
                }
                dt = new DateTime(int.Parse(nyear), int.Parse(nmonth), int.Parse(nday), int.Parse(nhrs), int.Parse(nmin), int.Parse(nsec));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 苏宁
        /// </summary>
        /// <returns></returns>
        private DateTime GetSuningTimeAPI()
        {
            string url = "http://quan.suning.com/getSysTime.do";
            HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
            hwr.Method = "get";
            hwr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            hwr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.3 Safari/537.36";
            DateTime dt;
            try
            {
                HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
                string webtime = new StreamReader(hws.GetResponseStream()).ReadToEnd();
                long times = long.Parse(Regex.Match(webtime, "\\d{14}").Value);
                int y = int.Parse(times.ToString().Substring(0, 4));
                int mm = int.Parse(times.ToString().Substring(4, 2));
                int d = int.Parse(times.ToString().Substring(6, 2));
                int h = int.Parse(times.ToString().Substring(8, 2));
                int m = int.Parse(times.ToString().Substring(10, 2));
                int s = int.Parse(times.ToString().Substring(12, 2));
                dt = new DateTime(y,mm,d,h,m,s);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

https://time.is/t/?zh.0.347.1989.0p.480.43d.1594451166998.1594451162678

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值