Show / Hide Table of Contents

    Class DateTimeProvider

    Class for "freezing" date and time to constant value. Usable for example in tests.

    Inheritance
    System.Object
    DateTimeProvider
    Namespace: Kros.Utils
    Assembly: Kros.Utils.dll
    Syntax
    public class DateTimeProvider : IDisposable
    Remarks

    Current time is accessible in Now property. Own time can be injected using InjectActualDateTime(DateTime).

        private void BasicExample()
        {
            using (DateTimeProvider.InjectActualDateTime(new DateTime(1978, 12, 10)))
            {
                Console.WriteLine(DateTimeProvider.Now.ToString("d.M.yyyy")); // Writes 10.12.1978
            }
        }

    Set value is valid for current thread only, so it is possible to have different values in different threads.

    Properties

    Now

    Returns own date and time, if it was set by InjectActualDateTime(DateTime). If it was not set, System.DateTime.Now is returned.

    Declaration
    public static DateTime Now { get; }
    Property Value
    Type Description
    System.DateTime

    Methods

    Dispose()

    Declaration
    public void Dispose()

    InjectActualDateTime(DateTime)

    Sets time actualDateTime, which will be returned in Now property. Use it in using block.

    Declaration
    public static IDisposable InjectActualDateTime(DateTime actualDateTime)
    Parameters
    Type Name Description
    System.DateTime actualDateTime

    Required date and time value.

    Returns
    Type Description
    System.IDisposable

    See Also

    System.IDisposable
    Back to top KROS a.s.