using System; using UnityEngine; /// /// 间隔类型 /// public enum E_MoreThanType { /// /// 分秒 /// MS, /// /// 时分秒 /// HMS, /// /// 天时分秒 /// DHMS, } /// /// 间隔时间 /// public enum E_IntervalType { /// /// 天 /// D, /// /// 时 /// H, /// /// 分 /// M, /// /// 秒 /// S, } /// /// 时间管理器 /// public class TimeManager : SingletonManager { /// /// 根据long得到显示时间 /// /// /// public DateTime GetDateTimeSeconds(long timestamp) { long begtime = timestamp * 10000000; DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0); long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度 long time_tricks = tricks_1970 + begtime;//日志日期刻度 DateTime dt = new DateTime(time_tricks);//转化为DateTime return dt; } /// /// 得到 long /// /// public long GetTimeSpan() { TimeSpan timeStamp = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(timeStamp.TotalSeconds); } /// /// 获取间隔秒数 /// /// 开始时间 /// 结束时间 /// public T GetSubSeconds(DateTime startTimer, DateTime endTimer, E_IntervalType e_IntervalType) where T : struct { TimeSpan startSpan = new TimeSpan(startTimer.Ticks); TimeSpan nowSpan = new TimeSpan(endTimer.Ticks); TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration(); T time = default(T); switch (e_IntervalType) { case E_IntervalType.D: time = (T)Convert.ChangeType(subTimer.Days, typeof(T)); break; case E_IntervalType.H: time = (T)Convert.ChangeType(subTimer.Hours, typeof(T)); break; case E_IntervalType.M: time = (T)Convert.ChangeType(subTimer.TotalMinutes, typeof(T)); break; case E_IntervalType.S: time = (T)Convert.ChangeType(subTimer.TotalSeconds, typeof(T)); break; } //返回相差时长(算上分、时的差值,返回相差的总秒数) return time; } /// /// 将秒换算成钟表格式 /// public string ConversionTable(float second, E_MoreThanType e_MoreThan)//倒计时 { string time = ""; int d = Mathf.FloorToInt(second / 86400); // 1天 = 86400秒 int h = Mathf.FloorToInt(second / 3600); //秒数取整 一小时为3600秒 秒数对3600取整即为小时 int m = Mathf.FloorToInt(second % 3600 / 60); //一分钟为60秒 秒数对3600取余再对60取整即为分钟 float s = Mathf.FloorToInt(second % 3600 % 60);//对3600取余再对60取余即为秒数 switch (e_MoreThan) { case E_MoreThanType.MS: time = m + ":" + s.ToString("00");//返回00:00时间格式 break; case E_MoreThanType.HMS: time = h + ":" + m + ":" + s.ToString("00");//返回00:00:00时间格式 break; case E_MoreThanType.DHMS: time = d + ":" + h + ":" + m + ":" + s.ToString("00");//返回00:00:00时间格式 break; } return time; } }