This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TimeCorrection.cs
82 lines (74 loc) · 3.43 KB
/
TimeCorrection.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
namespace OtpSharp
{
/// <summary>
/// Class to apply a correction factor to the system time
/// </summary>
/// <remarks>
/// In cases where the local system time is incorrect it is preferable to simply correct the system time.
/// This class is provided to handle cases where it isn't possible for the client, the server, or both, to be on the correct time.
///
/// This library provides limited facilities to to ping NIST for a correct network time. This class can be used manually however in cases where a server's time is off
/// and the consumer of this library can't control it. In that case create an instance of this class and provide the current server time as the correct time parameter
///
/// This class is immutable and therefore threadsafe
/// </remarks>
public class TimeCorrection
{
/// <summary>
/// An instance that provides no correction factor
/// </summary>
public static readonly TimeCorrection UncorrectedInstance = new TimeCorrection();
private readonly TimeSpan timeCorrectionFactor;
/// <summary>
/// Constructor used solely for the UncorrectedInstance static field to provide an instance without a correction factor.
/// </summary>
private TimeCorrection()
{
this.timeCorrectionFactor = TimeSpan.FromSeconds(0);
}
/// <summary>
/// Creates a corrected time object by providing the known correct current UTC time. The current system UTC time will be used as the reference
/// </summary>
/// <remarks>
/// This overload assumes UTC. If a base and reference time other than UTC are required then use the other overlaod.
/// </remarks>
/// <param name="correctUtc">The current correct UTC time</param>
public TimeCorrection(DateTime correctUtc)
{
this.timeCorrectionFactor = DateTime.UtcNow - correctUtc;
}
/// <summary>
/// Creates a corrected time object by providing the known correct current time and the current reference time that needs correction
/// </summary>
/// <param name="correctTime">The current correct time</param>
/// <param name="referenceTime">The current reference time (time that will have the correction factor applied in subsequent calls)</param>
public TimeCorrection(DateTime correctTime, DateTime referenceTime)
{
this.timeCorrectionFactor = referenceTime - correctTime;
}
/// <summary>
/// Applies the correction factor to the reference time and returns a corrected time
/// </summary>
/// <param name="referenceTime">The reference time</param>
/// <returns>The reference time with the correction factor applied</returns>
public DateTime GetCorrectedTime(DateTime referenceTime)
{
return referenceTime - timeCorrectionFactor;
}
/// <summary>
/// Applies the correction factor to the current system UTC time and returns a corrected time
/// </summary>
public DateTime CorrectedUtcNow
{
get { return GetCorrectedTime(DateTime.UtcNow); }
}
/// <summary>
/// The timespan that is used to calculate a corrected time
/// </summary>
public TimeSpan CorrectionFactor
{
get { return this.timeCorrectionFactor; }
}
}
}