using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /* 工具-时间处理 */ public class TimeUtil { //格式化,最终得出分和秒 public static string GetTimeStr(float time, bool ceil = true) { int seconds = ceil ? Mathf.CeilToInt(time) : Mathf.FloorToInt(time); string str = ""; int m = seconds / 60; if (m < 10) { str += 0; } str += m; str += " : "; int s = seconds % 60; if (s < 10) { str += 0; } str += s; return str; } public static string GetOfflineTimeStr(long offlineTime, bool online) { if (online) return TextAutoLanguage2.GetTextByKey("friend_offline-time_0"); long now = JCUnityLib.TimeUtils.GetTimestamp(); long dt = now - offlineTime; if (dt < 0) dt = 0; if (offlineTime == 0) return TextAutoLanguage2.GetTextByKey("friend_offline-time_1"); long min = dt / (60 * 1000); if (min == 0) min += 1; if (min < 60) { return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_2"), min); } long hour = min / 60; if (hour < 24) { return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_3"), hour); } long day = hour / 24; if (day < 365) { return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_4"), day); } long year = day / 365; return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_5"), year); } public static void SetOfflineTimeTextKey(long offlineTime, bool online, TextAutoLanguage2 output) { if (online) { output.SetTextKey("friend_offline-time_0"); return; } long now = JCUnityLib.TimeUtils.GetTimestamp(); long dt = now - offlineTime; if (dt < 0) dt = 0; if (offlineTime == 0) { output.SetTextKey("friend_offline-time_1"); return; } long min = dt / (60 * 1000); if (min == 0) min += 1; if (min < 60) { output.textFormatArgs = new object[]{min}; output.SetTextKey("friend_offline-time_2"); return; } long hour = min / 60; if (hour < 24) { output.textFormatArgs = new object[]{hour}; output.SetTextKey("friend_offline-time_3"); return; } long day = hour / 24; if (day < 365) { output.textFormatArgs = new object[]{day}; output.SetTextKey("friend_offline-time_4"); return; } long year = day / 365; output.textFormatArgs = new object[]{year}; output.SetTextKey("friend_offline-time_5"); return; } }