-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathTimeExtensions.cs
More file actions
54 lines (45 loc) · 2.13 KB
/
TimeExtensions.cs
File metadata and controls
54 lines (45 loc) · 2.13 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
namespace TestBuildingBlocks;
public static class TimeExtensions
{
// The milliseconds precision in DateTime/DateTimeOffset/TimeSpan/TimeOnly values that fakers produce
// is higher than what PostgreSQL can store. This results in our resource change tracker to detect
// that the time stored in the database differs from the time in the request body. While that's
// technically correct, we don't want such side effects influencing our tests everywhere.
public static DateTimeOffset TruncateToWholeMilliseconds(this DateTimeOffset value)
{
// Because PostgreSQL does not store the UTC offset in the database, it cannot round-trip
// values with a non-zero UTC offset, and therefore always rejects such values.
DateTime dateTime = value.DateTime.TruncateToWholeMilliseconds();
return new DateTimeOffset(dateTime, TimeSpan.Zero);
}
public static DateTime TruncateToWholeMilliseconds(this DateTime value)
{
long ticksInWholeMilliseconds = TruncateTicksInWholeMilliseconds(value.Ticks);
return new DateTime(ticksInWholeMilliseconds, value.Kind);
}
private static long TruncateTicksInWholeMilliseconds(long ticks)
{
long ticksToSubtract = ticks % TimeSpan.TicksPerMillisecond;
return ticks - ticksToSubtract;
}
public static TimeSpan TruncateToWholeMilliseconds(this TimeSpan value)
{
long ticksInWholeMilliseconds = TruncateTicksInWholeMilliseconds(value.Ticks);
return new TimeSpan(ticksInWholeMilliseconds);
}
public static TimeOnly TruncateToWholeMilliseconds(this TimeOnly value)
{
long ticksInWholeMilliseconds = TruncateTicksInWholeMilliseconds(value.Ticks);
return new TimeOnly(ticksInWholeMilliseconds);
}
public static TimeOnly TruncateToWholeSeconds(this TimeOnly value)
{
long ticksInWholeSeconds = TruncateTicksInWholeSeconds(value.Ticks);
return new TimeOnly(ticksInWholeSeconds);
}
private static long TruncateTicksInWholeSeconds(long ticks)
{
long ticksToSubtract = ticks % TimeSpan.TicksPerSecond;
return ticks - ticksToSubtract;
}
}