当前位置: 移动技术网 > IT编程>开发语言>c# > Unity3D如何获取时间戳或北京时间

Unity3D如何获取时间戳或北京时间

2020年06月14日  | 移动技术网IT编程  | 我要评论

dezhouxueyuan,超市陈列,清汤火锅底料

本文实例为大家分享了unity3d获取时间戳或北京时间的具体代码,供大家参考,具体内容如下

单机游戏因为没有服务器下发时间戳所以要自己获取,当然也可以用现成的时间api来获取。

如果获取本地时间,会导致玩家随意修改日期来达到数据更改,如每日奖品、每日奖励等等。

单机游戏本来就不要网络的,可是获取时间需要网络,这有点矛盾,有没有谁有更好的解决方案呢?

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/");//百度北京时间地址
        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);//这里是时间戳
        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";//如果没有网络就返回默认
      }
      return result;
    }
  }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网