12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GetHttpTimer : MonoSingleton<GetHttpTimer>
- {
- public long timestamp = -1;
- private float nowTimes = 0;
- private void Start()
- {
- StartCoroutine(HttpTool.Instance.SendHttp(HttpEdustryAction.timestamp, "", (string msg) =>
- {
- Debug.Log(msg);
- JObject data = JObject.Parse(msg);
- if (data["code"].ToString() == "200")
- {
- timestamp = long.Parse(data["data"]["timestamp"].ToString());
- nowTimes = 0;
- Debug.Log(timestamp);
- }
- else
- {
- Debug.LogError(HttpEdustryAction.timestamp + " Error " + data["code"].ToString());
- }
- }));
- }
- void Update()
- {
- if (timestamp !=-1)
- {
- nowTimes += Time.deltaTime;
- // timestamp += Convert.ToInt64(Time.deltaTime);
- }
- }
- public DateTime GetTimer()
- {
- if (timestamp == -1)
- return System.DateTime.Now;
- return UnixTimeStampToDateTime((timestamp+(long)nowTimes));
- }
- // 将Unix时间戳转换为DateTime
- DateTime UnixTimeStampToDateTime(long unixTimeStamp)
- {
- // string times = unixTimeStamp.ToString() + "000";
- // long timestamp = long.Parse(times);
- // Unix起始时间
- // DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.);
- DateTime startTime = System.TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//获取时间戳
- DateTime dt = startTime.AddSeconds(unixTimeStamp);
- // 将时间戳转换为DateTime
- return dt;
-
-
- }
- /// <summary>
- /// 时间戳转为C#格式时间
- /// </summary>
- /// <param name=”timeStamp”></param>
- /// <returns></returns>
- public static 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);
- }
- }
|