Closed fowl2 closed 1 year ago
@fowl2 : we do have conversion built in (see https://github.com/pnp/pnpcore/blob/dev/src/sdk/PnP.Core/Model/SharePoint/Core/Internal/TimeZone.cs#L43-L227). Would that help you?
When a PnPContext
is created we automatically load the web's regional settings (see https://pnp.github.io/pnpcore/using-the-sdk/basics-context.html#loading-additional-iweb-and-isite-properties-when-creating-a-pnpcontext), so the web's timezone information is present. This enables you to convert to and from UTC date times as shown in these test cases https://github.com/pnp/pnpcore/blob/dev/src/sdk/PnP.Core.Test/SharePoint/WebTests.cs#L756-L942.
Today the internal static TimeZoneInfo GetTimeZoneInfoFromSharePoint(string timeZoneDescription)
method is internal, but this one possibly can be made public.
Hi @fowl2 , did my comments help solve your timezone questions?
@fowl2 : I'm closing this issue as there's no recent feedback. Always happy to re-open if still relevant, just let me know. Thanks for using PnP Core SDK and providing the feedback.
@jansenbe sorry I've had this browser tab open for a few days, let me press send!
Ahh interesting! Without looking at the implementation I would have assumed LocalTimeToUtc
and UtcToLocalTime
were simply calling the server methods with that same name, and suffer from the associated round-trip penalty.
Making GetTimeZoneInfoFromSharePoint
or similar public would be useful!
On the implementation:
@fowl2 : using Id versus "Description" indeed is a better approach, I've changed our internal implementation. Also provided a GetTimeZoneInfo
method in ITimeZone
to get the equivalent .NET timezone object for a SharePoint timezone. Would this be sufficient for your use cases?
@fowl2: also adding here that indeed the user should specify the correct input.
// Convert to UTC time
var utcDate = localDate.ToUniversalTime();
// Convert to Web's timezone
var localSiteTime = context.Web.RegionalSettings.TimeZone.UtcToLocalTime(utcDate);
I think this can be closed now, happy to hear if you feel different.
Category
Describe the feature
It'd be really great if this library provided a helper method to convert SharePoint timezones into .Net timezones.
This is really useful:
Additional context
The
TimeZone.Information
property doesn't include the 'when' of any daylight savings transition(s), so isn't enough to synthesize a useful timezone.Example of with implementation (uses Nodatime, but a pure
TimeZoneInfo
version would be almost identical minus the fallback):Not all the SharePoint timezone descriptions match the Windows timezone descriptions, so I had to manually fix up some of them, but this script provided the starting point:
Alternatively, you could use
UTCToLocalTime
andLocalTimeToUTC
but that's a lot of server round trips (performance) and precludes using a useful library to reduce errors (reliability).