DateTimeUtil.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace Ips.Library.Basic
  6. {
  7. public static class DateTimeUtil
  8. {
  9. public const string DefaultDisplayFmt = "yyyy-MM-dd HH:mm:ss";
  10. public const string DefaultNumFmt = "yyyyMMddHHmmss";
  11. public static readonly DateTime _DT1970 = new DateTime(1970, 1, 1, 8, 0, 0);
  12. public static long To1970s(DateTime source)
  13. {
  14. return (long)(source - _DT1970).TotalSeconds;
  15. }
  16. public static long To1970ms(DateTime source)
  17. {
  18. return (long)(source - _DT1970).TotalMilliseconds;
  19. }
  20. public static DateTime From1970s(long seconds)
  21. {
  22. return _DT1970.AddSeconds(seconds);
  23. }
  24. public static DateTime From1970ms(long milliseconds)
  25. {
  26. return _DT1970.AddMilliseconds(milliseconds);
  27. }
  28. public static void WaitTime(DateTime time)
  29. {
  30. if (time <= DateTime.Now) return;
  31. while (true)
  32. {
  33. Thread.Sleep(100);
  34. if (time <= DateTime.Now) break;
  35. }
  36. }
  37. }
  38. }