Table of Contents

Class ZonedTimeProviderExtension

Namespace
Arc4u
Assembly
Arc4u.dll

Extension methods to register a ZonedTimeProvider.

public static class ZonedTimeProviderExtension
Inheritance
ZonedTimeProviderExtension
Inherited Members

Methods

AddZonedTimeProvider(IServiceCollection)

Registers a ZonedTimeProvider as the TimeProvider of the application, replacing any TimeProvider already registered. Its time zone is TimeZone of the ApplicationConfig; when the time zone is empty or cannot be found, the time zone of the machine is used and a warning is logged. The ApplicationConfig options must be registered (see AddApplicationConfig).

public static IServiceCollection AddZonedTimeProvider(this IServiceCollection services)

Parameters

services IServiceCollection

The service collection.

Returns

IServiceCollection

The service collection.

Examples

builder.Services.AddApplicationConfig(builder.Configuration);
builder.Services.AddZonedTimeProvider();

public sealed class ShippingService(TimeProvider timeProvider)
{
    public bool ShipsToday() => timeProvider.GetLocalNow().TimeOfDay < new TimeSpan(17, 0, 0);
}

AddZonedTimeProvider(IServiceCollection, string, string)

Registers a ZonedTimeProvider for a specific time zone as a keyed TimeProvider.

public static IServiceCollection AddZonedTimeProvider(this IServiceCollection services, string serviceKey, string timeZoneId)

Parameters

services IServiceCollection

The service collection.

serviceKey string

The key used to inject the provider, for example with [FromKeyedServices("NewYork")].

timeZoneId string

The identifier of the time zone, as understood by FindSystemTimeZoneById(string).

Returns

IServiceCollection

The service collection.

Examples

builder.Services.AddZonedTimeProvider("NewYork", "America/New_York");

public sealed class ReportService([FromKeyedServices("NewYork")] TimeProvider newYork)
{
    public string Format(DateTime createdUtc) => createdUtc.AsLocalTime(newYork).ToString("g");
}

Exceptions

ArgumentNullException

services or serviceKey is null.

ArgumentException

timeZoneId is null or empty.

TimeZoneNotFoundException

The time zone timeZoneId is not found.