What is the most convenient way to create a specific instance of Microsoft.SharePoint.SPTimeZone
as the following one:
SPTimeZone utc = SPRegionalSettings.GlobalTimeZones .OfType<SPTimeZone>() .FirstOrDefault(tz => tz.Information.Bias == 0 && tz.Information.DaylightBias == 0);
Is this hack the best one I can get...
This is in particular a problem for me, since I would like to mock this part of code for unit testing and to force it to always return UTC. The property GlobalTimeZones
seems to depend on HttpContext.Current
or an actual request - a prerequisite I don't have in my Unit Tests...
N.B: I know that there is System.TimeZoneInfo
but a third party assembly forces me into using SPTimeZone ...
To stop the discussion I decided to finally answer this question on my own.
There are several really helpful solutions in the comments which I want to gather in this short answer:
I for my part decided to go with MSFakes to shim the whole 3rd party call (since I didn't wan't to test its behavior anyways) and to shim all properties of SPTimeZone that I use in my method. Introducing an adapter unfortunately wasn't an option for me, since I had to preserve the (internal) API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With