-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV7 - Extract.cs
More file actions
34 lines (29 loc) · 1.29 KB
/
V7 - Extract.cs
File metadata and controls
34 lines (29 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
namespace DaanV2.UUID;
public static partial class V7 {
/// <summary>Extracts the Unix milliseconds timestamp from the UUID</summary>
/// <param name="uuid">The <see cref="UUID"/> to extract the timestamp from</param>
/// <returns>Unix timestamp in milliseconds</returns>
public static UInt64 ExtractUtc(UUID uuid) {
(UInt64 bits48, _, _) = Format.Extract(uuid);
return bits48;
}
/// <summary>Extracts the datetime from the UUID</summary>
/// <param name="uuid">The <see cref="UUID"/> to extract the timestamp from</param>
/// <returns>The <see cref="DateTime"/></returns>
public static DateTime Extract(UUID uuid) {
UInt64 unixMs = ExtractUtc(uuid);
return UnixMillisecondsToDateTime(unixMs);
}
/// <summary>Converts Unix milliseconds to DateTime</summary>
/// <param name="unixMs">Milliseconds since Unix epoch (1970-01-01 00:00:00 UTC)</param>
/// <returns>The corresponding DateTime in UTC</returns>
private static DateTime UnixMillisecondsToDateTime(UInt64 unixMs) {
try {
return UnixEpoch.AddMilliseconds(unixMs);
}
catch (ArgumentOutOfRangeException) {
// If the milliseconds value is too large, return MaxValue
return DateTime.MaxValue;
}
}
}