您当前的位置: 首页 >  网络

unity工具人

暂无认证

  • 10浏览

    0关注

    205博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

unity 获取网络时间

unity工具人 发布时间:2022-07-18 23:58:21 ,浏览量:10

网络时间

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class NetworkTime : MonoBehaviour
{    
    void Start()
    {
    	// 自动获取网络时间
        StartCoroutine(GetWebTime());
    }

    #region 获取网络时间

    /// 
    /// 获取当前网络时间
    /// 
    /// 
    IEnumerator GetWebTime()
    {
        // 获取时间地址
        string url = "https://www.baidu.com"; // 百度 //http://www.beijing-time.org/"; // 北京时间
        Debug.Log("开始获取服务器时间... 获取地址是: " + url);

        DateTime _webNowTime = DateTime.Now;
        // 获取时间
        UnityWebRequest WebRequest = new UnityWebRequest(url);

        // 等待请求完成
        yield return WebRequest.SendWebRequest();
        
        //网页加载完成  并且下载过程中没有错误   string.IsNullOrEmpty 判断字符串是否是null 或者是" ",如果是返回true
        //WebRequest.error  下载过程中如果出现下载错误  会返回错误信息 如果下载没有完成那么将会阻塞到下载完成
        if (WebRequest.isDone && string.IsNullOrEmpty(WebRequest.error))
        {
        	// 将返回值存为字典
            Dictionary resHeaders = WebRequest.GetResponseHeaders();
            string key = "DATE";
            string value = null;
            // 获取key为"DATE" 的 Value值
            if (resHeaders != null && resHeaders.ContainsKey(key))
            {
                resHeaders.TryGetValue(key, out value);
            }

            if (value == null)
            {
                Debug.LogError("没有获取到key为DATE对应的Value值...");
                yield break;
            }

            // 取到了value,则进行转换为本地时间
            _webNowTime = FormattingGMT(value);
            Debug.Log(value + " ,转换后的网络时间:" + _webNowTime);

            // 转换成时间戳
            TimeSpan cha = (_webNowTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)));
            long t = (long) cha.TotalSeconds;
            Debug.Log("网络时间转时间戳:" + t);
        }
    }

    /// 
    /// GMT(格林威治时间)时间转成本地时间
    /// 
    /// 字符串形式的GMT时间
    /// 
    private DateTime FormattingGMT(string gmt)
    {
        DateTime dt = DateTime.MinValue;
        try
        {
            string pattern = "";
            if (gmt.IndexOf("+0") != -1)
            {
                gmt = gmt.Replace("GMT", "");
                pattern = "ddd, dd MMM yyyy HH':'mm':'ss zzz";
            }

            if (gmt.ToUpper().IndexOf("GMT") != -1)
            {
                pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
            }

            if (pattern != "")
            {
                dt = DateTime.ParseExact(gmt, pattern, System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.DateTimeStyles.AdjustToUniversal);
                dt = dt.ToLocalTime();
            }
            else
            {
                dt = Convert.ToDateTime(gmt);
            }
        }
        catch (Exception e)
        {
            Debug.Log(e);
            Debug.LogError("时间转换错误...");
        }
        return dt;
    }
    #endregion
}


关注
打赏
1656671177
查看更多评论
立即登录/注册

微信扫码登录

0.0392s