欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity3D如何獲取時(shí)間戳或北京時(shí)間

 更新時(shí)間:2020年10月28日 09:30:08   作者:代碼妖  
這篇文章主要為大家詳細(xì)介紹了Unity3D獲取時(shí)間戳或北京時(shí)間的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Unity3D獲取時(shí)間戳或北京時(shí)間的具體代碼,供大家參考,具體內(nèi)容如下

單機(jī)游戲因?yàn)闆](méi)有服務(wù)器下發(fā)時(shí)間戳所以要自己獲取,當(dāng)然也可以用現(xiàn)成的時(shí)間API來(lái)獲取。

如果獲取本地時(shí)間,會(huì)導(dǎo)致玩家隨意修改日期來(lái)達(dá)到數(shù)據(jù)更改,如每日獎(jiǎng)品、每日獎(jiǎng)勵(lì)等等。

單機(jī)游戲本來(lái)就不要網(wǎng)絡(luò)的,可是獲取時(shí)間需要網(wǎng)絡(luò),這有點(diǎn)矛盾,有沒(méi)有誰(shuí)有更好的解決方案呢?

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
 
namespace ConsoleApplication1
{
 
 class Program
 {
  static void Main(string[] args)
  {
   Console.WriteLine( GetBeiJingTime());
   Console.ReadKey();
  }
 
  public static string GetBeiJingTime()
  {
   bool isget = false;
   string result = string.Empty;
   try
   {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://open.baidu.com/special/time/");//百度北京時(shí)間地址
    req.Headers.Add("content", "text/html; charset=gbk");
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    Stream stream = res.GetResponseStream();
    StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("gbk"));
    string html = sr.ReadToEnd();
    Func<string,string> f1 = (p) =>{
     Regex reg = new Regex("(?<=baidu_time\\().*?(?=\\))");
     return reg.Matches(p)[0].Value;};
    string time = f1(html).Substring(0, 10);//這里是時(shí)間戳
    stream.Dispose();
    sr.Dispose();
    DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
    long lTime = long.Parse(time + "0000000");
    TimeSpan toNow = new TimeSpan(lTime);
    result = dtStart.Add(toNow).ToString("yyyyMMdd");
    isget = true;
   }
   catch (Exception)
   {
   }
   finally
   {
    if (!isget)result = "19700101";//如果沒(méi)有網(wǎng)絡(luò)就返回默認(rèn)
   }
   return result;
  }
 }
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論