База знанияИзпитен център
BGБългарски
Избери език13

CKTIS / C#

System.TimeProvider: практическа справка

System.TimeProvider: практическа справка: TimeProvider абстрахира текущото време и timer-и, позволявайки детерминирани тестове.

Версия: .NET API; verified against .NET 10 reference surface

Синтаксис

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

TimeProvider абстрахира текущото време и timer-и, позволявайки детерминирани тестове.

Инжектирайте го вместо пряко използване на DateTimeOffset.UtcNow и Timer в тестируем код.

Системният provider още зависи от часовника на host; монотонен timestamp се различава от UTC.

GetElapsedTime изисква timestamp-и от същия provider; timer-ите трябва да се освобождават.

Пълната програма действително използва System.TimeProvider; точният очакван stdout е True.

Примери

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

Резултат

True

Пълната програма действително използва `System.TimeProvider`; точният очакван stdout е `True`.

Източници