Table of Contents

TimeToLive Class

Definition

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

Represents a TimeToLive (TTL) object that can be used when issuing authentication tokens or similar.

public record TimeToLive : ComparableValueObject<TimeSpan>, IEquatable<ValueObject>, IEquatable<SingleValueObject<TimeSpan>>, IComparisonOperators<ComparableValueObject<TimeSpan>, ComparableValueObject<TimeSpan>, bool>, IEqualityOperators<ComparableValueObject<TimeSpan>, ComparableValueObject<TimeSpan>, bool>, IEquatable<ComparableValueObject<TimeSpan>>, IEquatable<TimeToLive>
Inheritance
object
TimeToLive
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 issues a one-year access-key lifespan and reports the resulting duration, then compares two lifespans to decide which one grants a longer validity window. Use the FromMinutes, FromHours, FromDays, FromMonths, or FromYears factories to model lifespans in the unit that fits the product; the implicit TimeSpan conversion lets you treat the value as a normal span whenever you need to.

using System;
using Codebelt.SharedKernel;
using Codebelt.SharedKernel.Security;

namespace TokenIssuanceSample;

public static class LifespanPolicy
{
    public static TimeSpan ChooseLifespan(string tier)
    {
        TimeToLive candidate = tier == "premium"
            ? TimeToLive.FromYears(1)
            : TimeToLive.FromDays(30);

        // Implicit conversion to TimeSpan lets the duration flow into existing APIs that expect a span.
        return (TimeSpan)candidate;
    }
}

Remarks

Constructors

Name Description
TimeToLive(TimeSpan)

Initializes a new instance of the TimeToLive class.

Fields

Name Description
MaxValue

Denotes the largest possible value of TimeToLive.

MinValue

Denotes the smallest possible value of TimeToLive.

Methods

Name Description
FromDays(double)

Returns a TimeToLive that represents a specified number of days.

FromHours(double)

Returns a TimeToLive that represents a specified number of hours.

FromMinutes(double)

Returns a TimeToLive that represents a specified number of minutes.

FromMonths(int)

Returns a TimeToLive that represents a specified number of months.

FromYears(int)

Returns a TimeToLive that represents a specified number of years.

ToString()

Returns a string> that represents this instance.

Operators

Name Description
implicit operator TimeSpan(TimeToLive)

Performs an implicit conversion from TimeToLive to TimeSpan.

implicit operator TimeToLive(TimeSpan)

Performs an implicit conversion from TimeSpan to TimeToLive.

See Also