I have created some DateTime extensions that I need to test. In order to test them I need to be able to simulate that the DateTime instance represents various timezones other than the one of the local server creating the instance.
I have tried the following ...
//
// Create the new date to test. It will be in the timezone of the server it was created on (EST).
DateTime localDate = new DateTime(2017, 11, 14, 12, 0, 0);
//
// Get the TimeZone for another locale
TimeZoneInfo newTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Egypt Standard Time");
//
// Convert the localDate into a DateTime instance as if created on a server located in
// "Egypt Standard Time". - NOT WORKING
DateTime test = TimeZoneInfo.ConvertTime(localDate, origTimeZone);
//
// run the test
Boolean result = test.ExtensionMethod()
But this only results in "test" being another DateTime in the local EST TZ. The time has changed to reflect time in Egypt but the instance, when evaluated ... converted "toUniversalTime" for example, still converts as if it's an EST timezone.
Given the previous example my variable "test" will contain a DateTime of '2017-11-14 19:00:00'. When I convert it to UTC I was HOPING for a result of '2017-11-14 17:00:00'. Instead I am getting '2017-11-15 00:00:00.
Is there anyway to instantiate a DateTime from a timezone other than that of the local server?
origTimeZone);