0

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?

6
  • There is an overload to convert from a DateTime in a specific zone to UTC. Using UTC as the basic "timezone" is a good idea anyhow when dealing with this: you only need to bother with timeones for input and output and the core should work. Commented Sep 14, 2017 at 15:57
  • While I appreciate the comment, acknowledge and completely agree with the practice you describe ... it is unfortunately not my situation. What I really need to know is if what I am asking can be done. Whether it SHOULD be done is circumstantial and I have meetings set up to debate that with the project team ... again ... Thanks for the feedback. Commented Sep 14, 2017 at 16:32
  • Did you possibly use the wrong variable here: TimeZoneInfo.ConvertTime(localDate, origTimeZone); Commented Sep 14, 2017 at 16:34
  • 1
    DateTime can represent time in only two timezones, the local machine's and UTC. The Kind property indicates which. So the test is not very meaningful. If you obtained the DateTime on a remote machine then it better be UTC, local time is about as useful as a boomerang that doesn't come back. Commented Sep 14, 2017 at 17:09
  • @TylerLee Yes ... My Bad. Commented Sep 14, 2017 at 17:29

1 Answer 1

2

DateTime does not include any time zone information. ToUniversalTime will always use the local system time zone to do the conversion to UTC.

Just calling ToUniversalTime on your original localDate object will give you the value you're looking for. '2017-11-14 12:00:00' on a machine running in EST will be converted to '2017-11-14 17:00:00' (+5 hours).

You may also want to look into using the DateTimeOffset class, which allows you to include an offset value along with the date/time. This allows operations like ToUniversalTime to be aware of the offset and perform the conversion accordingly, rather than assuming the local time zone offset.

Example using DateTimeOffset:

TimeZoneInfo origTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTimeOffset localDate = new DateTimeOffset(2017, 11, 14, 12, 0, 0, origTimeZone.BaseUtcOffset);

Console.WriteLine(localDate); // 2017-11-14 12:00:00 (EST)

TimeZoneInfo newTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Egypt Standard Time");
DateTimeOffset test = TimeZoneInfo.ConvertTime(localDate, newTimeZone);

Console.WriteLine(test); // 2017-11-14 19:00:00 (EGST)
Console.WriteLine(test.ToUniversalTime()); // 2017-11-14 17:00:00 (UTC)

Output:

11/14/2017 12:00:00 PM -05:00
11/14/2017 7:00:00 PM +02:00
11/14/2017 5:00:00 PM +00:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.