#region Environment
Windows 10 20H2
Unity 2020.3.7f1c1 LTS
VSCode 1.56.2
https://github.com/MirzkisD1Ex0/UnityTheGreat.git
#endregion
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using UnityEngine.Networking;
/// <summary> /// /// </summary> /// https://tool.lu/timestamp/ public class TimeChecker : MonoBehaviour { public string StampURL = "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp";
private void Start() { StartCoroutine(GetTimeStamp(StampURL)); }
/// <summary> /// 获取时间戳 /// </summary> /// <returns></returns> private IEnumerator GetTimeStamp(string stampURL) { UnityWebRequest webRequest = UnityWebRequest.Get(stampURL); yield return webRequest.SendWebRequest(); if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError) { Debug.Log(webRequest.error); }
JObject jb = JObject.Parse(webRequest.downloadHandler.text); Debug.Log(jb["data"]["t"]); // 时间戳
long now = long.Parse(jb["data"]["t"].ToString()); if (now >= 1621419596) { Debug.Log("时间已过期"); } Debug.Log(now); }
/// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name="timeStamp"></param> /// <returns></returns> private DateTime ConvertStringToDateTime(string timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = long.Parse(timeStamp + "0000"); TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); } } #endregion