当前位置:网站首页>unity 获取网络时间
unity 获取网络时间
2022-07-26 21:16:00 【unity工具人】
网络时间
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkTime : MonoBehaviour
{
void Start()
{
// 自动获取网络时间
StartCoroutine(GetWebTime());
}
#region 获取网络时间
/// <summary>
/// 获取当前网络时间
/// </summary>
/// <returns></returns>
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<string, string> 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);
}
}
/// <summary>
/// GMT(格林威治时间)时间转成本地时间
/// </summary>
/// <param name="gmt">字符串形式的GMT时间</param>
/// <returns></returns>
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
}
边栏推荐
猜你喜欢

Triangular wave spectrum of MATLAB excitation model

What to do if the browser home page is tampered with, and how to recover if the home page is tampered with

imshow()函数后面如果不加waitKey()函数就不显示

flask 源码启动阶段

day07-

06 cp 命令

Can I view the history in the "stealth" mode of the secure browser?

梦里的一碗面

Xshell7 personal free download, use

Matlab draw short-term energy diagram
随机推荐
虾皮shopee根据关键词取商品列表 API
The principle of normal equation method and its difference from gradient descent method
Triangular wave spectrum of MATLAB excitation model
补充—非线性规划
Finding a new direction for the development of digital retail is the key to ensure that digital retail can enter a new stage of development
When deploying Flink on a single machine and creating the connection table of oracle19c RAC, the error ora-12505 is reported. Who can help
彻底搞通服务发现的原理和实现
逻辑漏洞----任意账号注册
matlab 画短时平均幅度谱
1 - "pytorch deep learning practice" - linear model
Understanding and practice of the trend of Bank of London foreign exchange
仅需一个依赖给Swagger换上新皮肤,既简单又炫酷
Pytorch torch.add() torch.add_() 用法
Huawei released the top ten trends in 2025: 5g, robot, AI, etc
Go----Go语言中的变量使用方法
Vb.net chart1 processing
FreeRTOS personal notes - Software Timer
Is it safe to open an account on flush? How to choose a securities firm for opening an account
Go -- go language naming specification
Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated