TimeUtil.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /* 工具-时间处理 */
  6. public class TimeUtil
  7. {
  8. //格式化,最终得出分和秒
  9. public static string GetTimeStr(float time, bool ceil = true)
  10. {
  11. int seconds = ceil ? Mathf.CeilToInt(time) : Mathf.FloorToInt(time);
  12. string str = "";
  13. int m = seconds / 60;
  14. if (m < 10) {
  15. str += 0;
  16. }
  17. str += m;
  18. str += " : ";
  19. int s = seconds % 60;
  20. if (s < 10)
  21. {
  22. str += 0;
  23. }
  24. str += s;
  25. return str;
  26. }
  27. public static string GetOfflineTimeStr(long offlineTime, bool online) {
  28. if (online) return TextAutoLanguage2.GetTextByKey("friend_offline-time_0");
  29. long now = JCUnityLib.TimeUtils.GetTimestamp();
  30. long dt = now - offlineTime;
  31. if (dt < 0) dt = 0;
  32. if (offlineTime == 0) return TextAutoLanguage2.GetTextByKey("friend_offline-time_1");
  33. long min = dt / (60 * 1000);
  34. if (min == 0) min += 1;
  35. if (min < 60) {
  36. return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_2"), min);
  37. }
  38. long hour = min / 60;
  39. if (hour < 24) {
  40. return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_3"), hour);
  41. }
  42. long day = hour / 24;
  43. if (day < 365) {
  44. return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_4"), day);
  45. }
  46. long year = day / 365;
  47. return String.Format(TextAutoLanguage2.GetTextByKey("friend_offline-time_5"), year);
  48. }
  49. public static void SetOfflineTimeTextKey(long offlineTime, bool online, TextAutoLanguage2 output) {
  50. if (online) {
  51. output.SetTextKey("friend_offline-time_0");
  52. return;
  53. }
  54. long now = JCUnityLib.TimeUtils.GetTimestamp();
  55. long dt = now - offlineTime;
  56. if (dt < 0) dt = 0;
  57. if (offlineTime == 0) {
  58. output.SetTextKey("friend_offline-time_1");
  59. return;
  60. }
  61. long min = dt / (60 * 1000);
  62. if (min == 0) min += 1;
  63. if (min < 60) {
  64. output.textFormatArgs = new object[]{min};
  65. output.SetTextKey("friend_offline-time_2");
  66. return;
  67. }
  68. long hour = min / 60;
  69. if (hour < 24) {
  70. output.textFormatArgs = new object[]{hour};
  71. output.SetTextKey("friend_offline-time_3");
  72. return;
  73. }
  74. long day = hour / 24;
  75. if (day < 365) {
  76. output.textFormatArgs = new object[]{day};
  77. output.SetTextKey("friend_offline-time_4");
  78. return;
  79. }
  80. long year = day / 365;
  81. output.textFormatArgs = new object[]{year};
  82. output.SetTextKey("friend_offline-time_5");
  83. return;
  84. }
  85. }