TimeHelper.cs 783 B

123456789101112131415161718192021222324252627282930313233343536
  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 ClientNowSeconds()
  16. {
  17. return Convert.ToInt64((DateTime.UtcNow - epoch).TotalSeconds);
  18. }
  19. public static long ClientNowTicks()
  20. {
  21. return Convert.ToInt64((DateTime.UtcNow - epoch).Ticks);
  22. }
  23. /// <summary>
  24. /// 登陆前是客户端时间,登陆后是同步过的服务器时间
  25. /// </summary>
  26. /// <returns></returns>
  27. public static long Now()
  28. {
  29. return ClientNow();
  30. }
  31. }
  32. }