Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / C#

System.TimeProvider: riferimento pratico

System.TimeProvider: riferimento pratico: TimeProvider astrae l’ora corrente e i timer, consentendo test deterministici.

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

Sintassi

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

TimeProvider astrae l’ora corrente e i timer, consentendo test deterministici.

Iniettalo invece di usare direttamente DateTimeOffset.UtcNow e Timer nel codice testabile.

Il provider di sistema dipende ancora dall’orologio host; un timestamp monotono è diverso da UTC.

GetElapsedTime richiede timestamp dello stesso provider; i timer devono essere rilasciati.

Il programma completo usa realmente System.TimeProvider; lo stdout esatto atteso è True.

Esempi

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);

Risultato

True

Il programma completo usa realmente `System.TimeProvider`; lo stdout esatto atteso è `True`.

Fonti