TimeInfo.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. namespace ET
  3. {
  4. public class TimeInfo: IDisposable
  5. {
  6. public static TimeInfo Instance = new TimeInfo();
  7. private int timeZone;
  8. public int TimeZone
  9. {
  10. get
  11. {
  12. return this.timeZone;
  13. }
  14. set
  15. {
  16. this.timeZone = value;
  17. dt = dt1970.AddHours(TimeZone);
  18. }
  19. }
  20. private readonly DateTime dt1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  21. private DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  22. public long ServerMinusClientTime { private get; set; }
  23. public long FrameTime;
  24. private TimeInfo()
  25. {
  26. this.FrameTime = this.ClientNow();
  27. }
  28. public void Update()
  29. {
  30. this.FrameTime = this.ClientNow();
  31. }
  32. /// <summary>
  33. /// 根据时间戳获取时间
  34. /// </summary>
  35. public DateTime ToDateTime(long timeStamp)
  36. {
  37. return dt.AddTicks(timeStamp * 10000);
  38. }
  39. // 线程安全
  40. public long ClientNow()
  41. {
  42. return (DateTime.UtcNow.Ticks - this.dt1970.Ticks) / 10000;
  43. }
  44. public long ServerNow()
  45. {
  46. return ClientNow() + Instance.ServerMinusClientTime;
  47. }
  48. public long ClientFrameTime()
  49. {
  50. return this.FrameTime;
  51. }
  52. public long ServerFrameTime()
  53. {
  54. return this.FrameTime + Instance.ServerMinusClientTime;
  55. }
  56. public long Transition(DateTime d)
  57. {
  58. return (d.Ticks - dt.Ticks) / 10000;
  59. }
  60. public void Dispose()
  61. {
  62. Instance = null;
  63. }
  64. }
  65. }