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
Declaration
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.
- Create a class that implements the
ITimeZoneProviderinterface:
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;
}
}
}
}
In the Program.cs file, register the
CustomTimeZoneProviderservice:using DevExpress.XtraReports.Services; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using AspNetCoreApp.Services; var builder = WebApplication.CreateBuilder(args); // ... builder.Services.AddScoped<ITimeZoneProvider, CustomTimeZoneProvider>(); builder.Services.AddHttpContextAccessor(); // ... var app = builder.Build(); // ... app.Run();In the view, set the LocalTimeZone cookie to the client’s time zone:
@model DevExpress.XtraReports.Web.WebDocumentViewer.WebDocumentViewerModel @{ var viewerRender = Html.DevExpress().WebDocumentViewer("DocumentViewer") .Height("100%") .Bind(Model); @viewerRender.RenderHtml() } @section Scripts { <link href="~/css/dx-reporting-skeleton-screen.css" rel="stylesheet" /> <link rel="stylesheet" href="~/css/viewer.part.bundle.css" /> <link rel="stylesheet" href="~/css/dx.material.blue.light.bundle.css" /> <script src="~/js/reporting.thirdparty.bundle.js"></script> <script src="~/js/viewer.part.bundle.js"></script> <script> document.addEventListener("DOMContentLoaded", () => { const tz = Intl.DateTimeFormat().resolvedOptions().timeZone; document.cookie = "LocalTimeZone=" + tz + "; path=/"; }); </script> @viewerRender.RenderScripts() }
In desktop applications, register the ITimeZoneProvider implementation at the XtraReport instance level.