-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathTimeZoneInfoMocker.cs
More file actions
31 lines (26 loc) · 1.02 KB
/
TimeZoneInfoMocker.cs
File metadata and controls
31 lines (26 loc) · 1.02 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
using System;
using System.Reflection;
namespace NetCoreForce.Client.Tests
{
public class LocalTimeZoneInfoMocker : IDisposable
{
private readonly TimeZoneInfo _actualLocalTimeZoneInfo;
public LocalTimeZoneInfoMocker(TimeZoneInfo mockTimeZoneInfo)
{
_actualLocalTimeZoneInfo = TimeZoneInfo.Local;
SetLocalTimeZone(mockTimeZoneInfo);
}
private static void SetLocalTimeZone(TimeZoneInfo timeZoneInfo)
{
var info = typeof(TimeZoneInfo).GetField("s_cachedData", BindingFlags.NonPublic | BindingFlags.Static);
object cachedData = info.GetValue(null);
var field = cachedData.GetType().GetField("_localTimeZone", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Instance);
field.SetValue(cachedData, timeZoneInfo);
}
public void Dispose()
{
TimeZoneInfo.ClearCachedData();
SetLocalTimeZone(_actualLocalTimeZoneInfo);
}
}
}