当前位置:网站首页>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
}
边栏推荐
- Summer vacation daily question week 7:7.18 - 7.24
- Triangular wave spectrum of MATLAB excitation model
- VB.net Chart1的处理
- 七月集训(第26天) —— 并查集
- JS 延迟执行window.onload
- Flag decodes token, mounts token, decorator, and seven cattle cloud upload
- Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
- Pytoch uses RNN model to build person name classifier
- Just one dependency to give swagger a new skin, which is simple and cool~
- Pytorch squeeze() unsqueeze() 用法
猜你喜欢
随机推荐
Shrimp Shope gets the product details API according to the ID
The combobox of easyUI selects the first option by default
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
Go----Go语言中的变量使用方法
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
彻底搞通服务发现的原理和实现
JDBC operation and entry case of MySQL
45、实例分割的labelme数据集转coco数据集以及coco数据集转labelme数据集
07 df 命令
Ansible installation and use
[RequireComponent(typeof(....))]
Go ---- variable usage in go language
Go -- go language naming specification
一篇让小百彻底搞懂性能调优
06 cp 命令
Search Yiwu shopping (PAI Li Tao) API by image
Is it safe to open an account on flush? How to choose a securities firm for opening an account
JMeter自定义日志与日志分析
基于CAShapeLayer和贝塞尔曲线的圆形进度条动画
Development to testing: a six-year road to automation from scratch








