2

The documentation says that...

  • You can use the ToLocalTime method to restore a local date and time value that was converted to UTC by the ToUniversalTime or FromFileTimeUtc method.

and goes on to say (immediately)

  • However, if the original time represents an invalid time in the local time zone, it will not match the restored value.

Does the latter imply that it will only not work if the time is 'invalid' (whatever that means?)?

1
  • My inclination is that the whole point of a method called ToLocalTime is that it converts time across time zones, but you're right that the documentation is ambiguous. Have you tried testing it? Tough I'm not really sure what you could use for edge cases... Commented Sep 17, 2010 at 0:04

1 Answer 1

1

I think this demonstrates what it means for times to be invalid:

DateTime now = DateTime.Now;
for (DateTime dt = now; dt < now.AddYears(1); dt += TimeSpan.FromMinutes(30))
{
    DateTime dt2 = dt.ToUniversalTime().ToLocalTime(); // dt2 == dt ?
    if (dt2 != dt)
    {
        Console.WriteLine("Not equal: {0}, {1}", dt, dt2);
    }
}

Result on my computer (you might get different results):

Not equal: 27-03-2011 02:26:28, 27-03-2011 03:26:28
Not equal: 27-03-2011 02:56:28, 27-03-2011 03:56:28

The time "27-03-2011 02:26:28" is invalid because they do not exist due to the clock moving forward one hour, causing that time to be skipped.

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.