当前位置:网站首页>Get network time by unity
Get network time by unity
2022-07-26 22:07:00 【Unity tool man】
Network time
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkTime : MonoBehaviour
{
void Start()
{
// Automatically get network time
StartCoroutine(GetWebTime());
}
#region Get network time
/// <summary>
/// Get the current network time
/// </summary>
/// <returns></returns>
IEnumerator GetWebTime()
{
// Get time address
string url = "https://www.baidu.com"; // Baidu //http://www.beijing-time.org/"; // Beijing time.
Debug.Log(" Start getting server time ... The access address is : " + url);
DateTime _webNowTime = DateTime.Now;
// Acquisition time
UnityWebRequest WebRequest = new UnityWebRequest(url);
// Wait for the request to complete
yield return WebRequest.SendWebRequest();
// Page loading complete And there are no errors in the download process string.IsNullOrEmpty Determine whether the string is null Or is it " ", If it's a return true
//WebRequest.error If a download error occurs during the download process Will return an error message If the download is not completed, it will be blocked until the download is completed
if (WebRequest.isDone && string.IsNullOrEmpty(WebRequest.error))
{
// Save the return value as a dictionary
Dictionary<string, string> resHeaders = WebRequest.GetResponseHeaders();
string key = "DATE";
string value = null;
// obtain key by "DATE" Of Value value
if (resHeaders != null && resHeaders.ContainsKey(key))
{
resHeaders.TryGetValue(key, out value);
}
if (value == null)
{
Debug.LogError(" No access to key by DATE Corresponding Value value ...");
yield break;
}
// Here we are value, Convert to local time
_webNowTime = FormattingGMT(value);
Debug.Log(value + " , Network time after conversion :" + _webNowTime);
// Convert to timestamp
TimeSpan cha = (_webNowTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)));
long t = (long) cha.TotalSeconds;
Debug.Log(" Network time to timestamp :" + t);
}
}
/// <summary>
/// GMT( Greenwich mean time ) Convert time to local time
/// </summary>
/// <param name="gmt"> In string form GMT Time </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(" Time conversion error ...");
}
return dt;
}
#endregion
}
边栏推荐
猜你喜欢
随机推荐
When deploying Flink on a single machine and creating the connection table of oracle19c RAC, the error ora-12505 is reported. Who can help
Xshell7 personal free download, use
Keyword maintenance of dream weaving document doesn't work
js验证复杂密码
Join method in JS
It is said that Samsung obtained EUV photoresist from Belgium
matlab 基音周期估计后处理
Go -- go language naming specification
day07-
TASK04|分类分析
day07-
JS delay execution window.onload
Flink's real-time data analysis practice in iFLYTEK AI marketing business
深入源码剖析String类为什么不可变?(还不明白就来打我)
JDBC总结
What to do if the browser home page is tampered with, and how to recover if the home page is tampered with
软件测试技术之跨平台的移动端UI自动化测试(下)
我的sql没问题为什么还是这么慢|MySQL加锁规则
Go----Go语言中的变量使用方法
Flink 在讯飞 AI 营销业务的实时数据分析实践








