N
nosiren
It is well known that the implementation of time zones other than the
machine local time zone has been left as an exercise for the user in
the .NET framework. It would seem that System.TimeZone, being an
abstract class, is meant to be used as a base class for any such
implementations. Having thoroughly reviewed the MSDN documentation for
System.TimeZone it would seem that, especially considering the new
DateTime.Kind property, System.TimeZone is actually unusable as a base
class.
Previously, although the MSDN documentation suggested otherwise, I had
to believe that the spirit of ToLocalTime() was to return a time that
was local to the particular time zone instance being used. This belief
was strengthened by the shared source implementation Microsoft provided
in 2002, and has been shared by any implementation I have seen that
uses System.TimeZone as a base class (see
http://www.michaelbrumm.com/simpletimezone.html for one example).
The addition of DateTime.Kind in .NET 2.0 has proven this belief to be
incorrect. ToLocalTime() must by contract always return machine local
time.
Here's an experiment. Fully implement the System.TimeZone abstract
interface for any time zone other than your own. Now set a breakpoint
in all of your methods and call TimeZone.ToLocalTime() for DateTimes of
Local, Utc, and Unspecified DateTime.Kind. You should discover, as I
did, that your code never gets called. Now try
TimeZone.ToUniversalTime() for the same values. Interestingly,
ToUniversalTime() WILL call your code for both Local and Unspecified
DateTime.Kind values. Make sense of that.
I just want to understand what the purpose of System.TimeZone really
is. If it were sealed, and all of its methods made static, it seems it
would lose no utility whatsoever. Am I missing something?
Any discussion would be appreciated.
machine local time zone has been left as an exercise for the user in
the .NET framework. It would seem that System.TimeZone, being an
abstract class, is meant to be used as a base class for any such
implementations. Having thoroughly reviewed the MSDN documentation for
System.TimeZone it would seem that, especially considering the new
DateTime.Kind property, System.TimeZone is actually unusable as a base
class.
Previously, although the MSDN documentation suggested otherwise, I had
to believe that the spirit of ToLocalTime() was to return a time that
was local to the particular time zone instance being used. This belief
was strengthened by the shared source implementation Microsoft provided
in 2002, and has been shared by any implementation I have seen that
uses System.TimeZone as a base class (see
http://www.michaelbrumm.com/simpletimezone.html for one example).
The addition of DateTime.Kind in .NET 2.0 has proven this belief to be
incorrect. ToLocalTime() must by contract always return machine local
time.
Here's an experiment. Fully implement the System.TimeZone abstract
interface for any time zone other than your own. Now set a breakpoint
in all of your methods and call TimeZone.ToLocalTime() for DateTimes of
Local, Utc, and Unspecified DateTime.Kind. You should discover, as I
did, that your code never gets called. Now try
TimeZone.ToUniversalTime() for the same values. Interestingly,
ToUniversalTime() WILL call your code for both Local and Unspecified
DateTime.Kind values. Make sense of that.
I just want to understand what the purpose of System.TimeZone really
is. If it were sealed, and all of its methods made static, it seems it
would lose no utility whatsoever. Am I missing something?
Any discussion would be appreciated.