Back to Devexpress

ITimeZoneProvider Interface

xtrareports-devexpress-dot-xtrareports-dot-services-d95d0f1f.md

latest3.1 KB
Original Source

ITimeZoneProvider Interface

Provides information about the local time zone in the UtcToLocalTime and LocalToUtcTime expression functions.

Namespace : DevExpress.XtraReports.Services

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public interface ITimeZoneProvider
vb
Public Interface ITimeZoneProvider

Remarks

You can use UtcToLocalTime and LocalToUtcTime functions to convert date-time values to local or UTC time zones. The local time zone is determined by the server’s settings. To provide a different time zone (for example, the client’s time zone), implement the ITimeZoneProvider interface. The GetCurrentTimeZone method should return a TimeZoneInfo object that is the local time zone.

The following example implements the ITimeZoneProvider interface in an ASP.NET Core application. In this example, the time zone is retrieved from a cookie named LocalTimeZone.

  1. Create a class that implements the ITimeZoneProvider interface:
csharp
using System;
using DevExpress.XtraReports.Services;
using Microsoft.AspNetCore.Http;

namespace AspNetCoreApp.Services {
    public class CustomTimeZoneProvider : ITimeZoneProvider {
        private readonly string timeZoneId;

        public CustomTimeZoneProvider(IHttpContextAccessor httpContextAccessor) {
            timeZoneId = httpContextAccessor.HttpContext?.Request.Cookies["LocalTimeZone"];
        }

        public TimeZoneInfo GetCurrentTimeZone() {
            var tzId = timeZoneId ?? "UTC";
            try {
                return TimeZoneInfo.FindSystemTimeZoneById(tzId);
            } catch {
                return TimeZoneInfo.Utc;
            }
        }
    }
}
  1. In the Program.cs file, register the CustomTimeZoneProvider service:

  2. In the view, set the LocalTimeZone cookie to the client’s time zone:

In desktop applications, register the ITimeZoneProvider implementation at the XtraReport instance level.

See Also

ITimeZoneProvider Members

Convert Time Zones (ASP.NET Core)

Convert Time Zones (ASP.NET MVC)

Convert Time Zones (ASP.NET Web Forms)

DevExpress.XtraReports.Services Namespace