PlcDataConvert.cs 941 B

1234567891011121314151617181920212223242526272829
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. public class PlcDataConvert
  7. {
  8. public static string FormatDWordSpanToDHMS(uint microseconds, bool includeMilliseconds = false)
  9. {
  10. // 1 微秒 = 10 ticks(因为 .NET 中 1 tick = 100 纳秒)
  11. //long ticks = microseconds * 10L;
  12. //TimeSpan duration = TimeSpan.FromTicks(ticks);
  13. // 或者用毫秒方式(可能有精度损失,但通常可接受):
  14. TimeSpan ts = TimeSpan.FromMilliseconds(microseconds);
  15. long totalDays = (long)ts.TotalDays;
  16. int hours = ts.Hours;
  17. int minutes = ts.Minutes;
  18. int seconds = ts.Seconds;
  19. if (includeMilliseconds)
  20. return $"{totalDays}.{hours:D2}:{minutes:D2}:{seconds:D2}.{ts.Milliseconds:D3}";
  21. else
  22. return $"{totalDays}.{hours:D2}:{minutes:D2}:{seconds:D2}";
  23. }
  24. }