| 1234567891011121314151617181920212223242526272829 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class PlcDataConvert
- {
- public static string FormatDWordSpanToDHMS(uint microseconds, bool includeMilliseconds = false)
- {
- // 1 微秒 = 10 ticks(因为 .NET 中 1 tick = 100 纳秒)
- //long ticks = microseconds * 10L;
- //TimeSpan duration = TimeSpan.FromTicks(ticks);
- // 或者用毫秒方式(可能有精度损失,但通常可接受):
- TimeSpan ts = TimeSpan.FromMilliseconds(microseconds);
- long totalDays = (long)ts.TotalDays;
- int hours = ts.Hours;
- int minutes = ts.Minutes;
- int seconds = ts.Seconds;
- if (includeMilliseconds)
- return $"{totalDays}.{hours:D2}:{minutes:D2}:{seconds:D2}.{ts.Milliseconds:D3}";
- else
- return $"{totalDays}.{hours:D2}:{minutes:D2}:{seconds:D2}";
- }
- }
|