TimeHelper.cs 853 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. namespace Base
  3. {
  4. public static class TimeHelper
  5. {
  6. private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  7. /// <summary>
  8. /// 客户端时间
  9. /// </summary>
  10. /// <returns></returns>
  11. public static long ClientNow()
  12. {
  13. return Convert.ToInt64((DateTime.UtcNow - epoch).TotalMilliseconds);
  14. }
  15. public static long ClientNowTicks()
  16. {
  17. return Convert.ToInt64((DateTime.UtcNow - epoch).Ticks);
  18. }
  19. /// <summary>
  20. /// 登陆前是客户端时间,登陆后是同步过的服务器时间
  21. /// </summary>
  22. /// <returns></returns>
  23. public static long Now()
  24. {
  25. TimeComponent gameTimeComponent = Game.Scene?.GetComponent<TimeComponent>();
  26. if (gameTimeComponent == null)
  27. {
  28. return TimeHelper.ClientNow();
  29. }
  30. long t = gameTimeComponent.Now();
  31. return t;
  32. }
  33. }
  34. }