开发者

Change .NET CLR time zone without using registry

I'd like to change the default time zone of a .NET CLR without using the registry or changing my OS time zone. Is there a way?

EDIT: It looks like the .NET CLR just doesn't support having a default time zone different than the OS's (unlike the JVM).

In other words I'd like this statement to return something other than my OS's time zone:

TimeZoneInfo timeZoneInfo = 开发者_StackOverflowTimeZoneInfo.Local;
Console.Out.WriteLine("timeZoneInfo = {0}", timeZoneInfo);

The reason I'd like to do this is to run a .NET GUI with the time zone of my users (London) rather than the time zone of my machine (Chicago).

For example, you can change a Java runtime's time zone by adding to the commandline:

-Duser.timezone="Europe/Berlin"

So, for example, if you want DateTime.Now to return a different time zone, you can't without changing all the references to DateTime.Now to something else, which is what I was hoping to avoid in the first place.


You are asking about getting a different result for the time zone setting, but I am assuming in the end you are interested in getting the time returned in another time zone by default.

The .NET framework supports UTC, local, and also a generic time value without a sense of time zone. See the DateTimeKind enumeration.

When dealing with time values I normally use UTC for everything internally and convert to a specific zone when interacting with the user.

So, to answer your question the only way I know to get a local time returned in another time zone is to change the time zone of the machine.

That begin said, to get your desired effect you could write a utility class that gets the time in UTC then use a configuration parameter to store an offset to apply before returning it.


Instead of relying on the CLR/framework to give you time zone information, you should rely on a an external source of information for working with time zones.

I recommend using the public info time zone database (also known as the tz or Olson database). While it doesn't claim to have 100% accuracy, I've found it as accurate for anything that I need to use.

There are .NET classes that can parse this database, namely the ZoneInfo (tz database / Olson database) .NET API. Note that there isn't a binary distribution, you'll have to download the latest version and compile it yourself.

Using this library, you find the time zone that you want (they are represented in ZoneInfo objects) for the region you are looking in, then you can give it a DateTime to perform various functions on it (like get the UTC offset on that date, get the current local time, UTC time, etc).

Note, this is more accurate than the .NET API IMO, in that it requires you to provide a date to get time zone information for; various regions have changed rules regarding UTC offsets over time, and the tz database along with the ZoneInfo API do a good job of representing that history and producing results accurately.

Don't worry about there not being any changes in the API since 2009; at the time of this writing, it currently parses all of the files in the latest data distrubition (I actually ran it against the ftp://elsie.nci.nih.gov/pub/tzdata2011k.tar.gz file on September 25, 2011 — in March 2017, you'd retrieve it from ftp://ftp.iana.org/tz/releases/tzdata2017a.tar.gz).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜