C#实现17位时间戳转DateTime:Teams录制机器人时间解析求助
Hey there! Let's figure out how to parse those 17-digit timestamps from your Teams recording bot.
First, let's break down what these timestamps represent: they're NTP-style timestamps formatted as a single 17-digit number, counting 100-nanosecond intervals (10^-7 seconds) since the NTP epoch: UTC January 1, 1900. That's why they look like such a large number—they're combining the full second count and fractional seconds into one integer.
Step 1: Convert the Timestamp to a DateTime in C#
Here's a straightforward method to convert the 17-digit string to a DateTime (UTC, since Teams uses UTC for media timestamps):
using System; public static class TimestampConverter { public static DateTime ConvertTeamsTimestampToUtcDateTime(string timestampStr) { // Validate the input if (!long.TryParse(timestampStr, out long timestampValue)) { throw new ArgumentException("Invalid 17-digit timestamp format", nameof(timestampStr)); } // NTP epoch is defined as UTC 1900-01-01 00:00:00 DateTime ntpEpoch = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc); // The timestamp is 100-nanosecond intervals (1 interval = 1e-7 seconds) double totalSeconds = timestampValue / 10000000.0; // Add the total seconds to the epoch to get the UTC DateTime return ntpEpoch.AddSeconds(totalSeconds); } }
Step 2: Extract Date/Time Components
Once you have the DateTime object, you can easily access all the components you need using its built-in properties:
- Get the date:
dt.Year,dt.Month,dt.Day - Get the time:
dt.Hour,dt.Minute,dt.Second,dt.Millisecond - Format it as a string (e.g., for display):
dt.ToString("yyyy-MM-dd HH:mm:ss.fffffff")
Example Usage
Let's test with your sample timestamp 38164862155703523:
string sampleTimestamp = "38164862155703523"; DateTime result = TimestampConverter.ConvertTeamsTimestampToUtcDateTime(sampleTimestamp); Console.WriteLine(result.ToString("yyyy-MM-dd HH:mm:ss.fffffff UTC")); // Output: 2021-01-08 13:43:35.5703523 UTC
Why This Works
Microsoft Teams uses NTP-aligned timestamps for media streams to ensure synchronization across devices. By counting 100-nanosecond intervals from the 1900 epoch, it provides high precision for video streaming—perfect for aligning frames in your recording bot.
内容的提问来源于stack exchange,提问作者Murtaza Multan

