Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.TimeProvider: practical reference

System.TimeProvider: practical reference: TimeProvider abstracts current time and timers, enabling deterministic tests.

Version: .NET API; verified against .NET 10 reference surface

Syntax

System.TimeProvider /* select a documented member or constructor */

TimeProvider abstracts current time and timers, enabling deterministic tests.

Inject it instead of directly using DateTimeOffset.UtcNow and Timer in testable code.

The system provider still depends on the host clock; a monotonic timestamp differs from UTC.

GetElapsedTime requires timestamps from the same provider; timers must be disposed.

The complete program actually uses System.TimeProvider; its exact expected stdout is True.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

TimeProvider provider = TimeProvider.System;
Console.WriteLine(provider.GetUtcNow().Offset == TimeSpan.Zero);

Result

True

The complete program actually uses `System.TimeProvider`; its exact expected stdout is `True`.

Sources