Class ClockSkew
- Namespace
- Codebelt.SharedKernel
- Assembly
- Codebelt.SharedKernel.dll
Represents a ClockSkew object that can be used to warrant for clock skew related scenarios such as authentication.
public record ClockSkew : ComparableValueObject<TimeSpan>, IEquatable<ValueObject>, IEquatable<SingleValueObject<TimeSpan>>, IComparisonOperators<ComparableValueObject<TimeSpan>, ComparableValueObject<TimeSpan>, bool>, IEqualityOperators<ComparableValueObject<TimeSpan>, ComparableValueObject<TimeSpan>, bool>, IEquatable<ComparableValueObject<TimeSpan>>, IEquatable<ClockSkew>
- Inheritance
-
objectClockSkew
- Implements
- Inherited Members
-
SingleValueObject<TimeSpan>.GetHashCode()SingleValueObject<TimeSpan>.Equals(object)SingleValueObject<TimeSpan>.Equals(ValueObject)SingleValueObject<TimeSpan>.Equals(SingleValueObject<TimeSpan>)SingleValueObject<TimeSpan>.<Clone>$()SingleValueObject<TimeSpan>.EqualityContract
Examples
The following example expresses a 30-second clock-skew tolerance and then measures the drift between two UTC timestamps so a verifier can decide whether a token issuance time is still within the allowed margin. Use ClockSkew.FromSeconds or ClockSkew.FromMinutes to model tolerances that match the real-world clock variance between two systems.
using System;
using Codebelt.SharedKernel;
namespace TokenIssuanceSample;
public static class ClockTolerance
{
public static bool IsWithinTolerance(DateTime issuedAt, DateTime verifiedAt)
{
ClockSkew allowed = TimeSpan.FromSeconds(30);
var issued = new CoordinatedUniversalTime(issuedAt);
var verified = new CoordinatedUniversalTime(verifiedAt);
DateTime issuedAtUtc = issued;
DateTime verifiedAtUtc = verified;
TimeSpan actualSkew = verifiedAtUtc - issuedAtUtc;
// Return whether the measured drift fits inside the configured tolerance.
return actualSkew <= allowed;
}
}
Remarks
Constructors
ClockSkew(TimeSpan)
Initializes a new instance of the ClockSkew class.
public ClockSkew(TimeSpan value)
Parameters
Exceptions
- ArgumentOutOfRangeException
valuecannot be less than MinValue -or-valuecannot exceed MaxValue.
Fields
MaxValue
Denotes the largest possible value of ClockSkew.
public static readonly TimeSpan MaxValue
Field Value
MinValue
Denotes the smallest possible value of ClockSkew.
public static readonly TimeSpan MinValue
Field Value
Methods
FromMinutes(byte)
Returns a ClockSkew that represents a specified number of minutes.
public static ClockSkew FromMinutes(byte value)
Parameters
valuebyteA number of minutes.
Returns
FromSeconds(ushort)
Returns a ClockSkew that represents a specified number of seconds.
public static ClockSkew FromSeconds(ushort value)
Parameters
valueushortA number of seconds.
Returns
ToString()
Returns a string> that represents this instance.
public override string ToString()
Returns
Operators
implicit operator TimeSpan(ClockSkew)
public static implicit operator TimeSpan(ClockSkew value)
Parameters
valueClockSkewThe value to convert.
Returns
- TimeSpan
A TimeToLive that is equivalent to
value.
implicit operator ClockSkew(TimeSpan)
public static implicit operator ClockSkew(TimeSpan value)
Parameters
valueTimeSpanThe value to convert.