TimeUtil.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class TimeUtil
  6. {
  7. //格式化,最终得出分和秒
  8. public static string GetTimeStr(float time, bool ceil = true)
  9. {
  10. int seconds = ceil ? Mathf.CeilToInt(time) : Mathf.FloorToInt(time);
  11. string str = "";
  12. int m = seconds / 60;
  13. if (m < 10) {
  14. str += 0;
  15. }
  16. str += m;
  17. str += " : ";
  18. int s = seconds % 60;
  19. if (s < 10)
  20. {
  21. str += 0;
  22. }
  23. str += s;
  24. return str;
  25. }
  26. public static string GetOfflineTimeStr(long offlineTime, bool online) {
  27. if (online) return TextAutoLanguage2.GetTextByKey("friend_offline-time_0");
  28. long now = JC.CS.Utility.GetTimestamp();
  29. long dt = now - offlineTime;
  30. if (dt < 0) dt = 0;
  31. if (offlineTime == 0) return TextAutoLanguage2.GetTextByKey("friend_offline-time_1");
  32. long min = dt / (60 * 1000);
  33. if (min == 0) min += 1;
  34. if (min < 60) {
  35. return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_2"), min);
  36. }
  37. long hour = min / 60;
  38. if (hour < 24) {
  39. return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_3"), hour);
  40. }
  41. long day = hour / 24;
  42. if (day < 365) {
  43. return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_4"), day);
  44. }
  45. long year = day / 365;
  46. return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_5"), year);
  47. }
  48. }