Table of Contents

ClockSkew Class

Definition

Namespace
Codebelt.SharedKernel
Assemblies
Codebelt.SharedKernel.dll
Source
src/Codebelt.SharedKernel/ClockSkew.cs

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
object
ClockSkew
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

Name Description
ClockSkew(TimeSpan)

Initializes a new instance of the ClockSkew class.

Fields

Name Description
MaxValue

Denotes the largest possible value of ClockSkew.

MinValue

Denotes the smallest possible value of ClockSkew.

Methods

Name Description
FromMinutes(byte)

Returns a ClockSkew that represents a specified number of minutes.

FromSeconds(ushort)

Returns a ClockSkew that represents a specified number of seconds.

ToString()

Returns a string> that represents this instance.

Operators

Name Description
implicit operator TimeSpan(ClockSkew)

Performs an implicit conversion from TimeSpan to ClockSkew.

implicit operator ClockSkew(TimeSpan)

Performs an implicit conversion from ClockSkew to TimeSpan.

See Also