2

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 = TimeZoneInfo.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.

Garrett Smith
  • 688
  • 1
  • 7
  • 24
  • what do you mean by CLR? the CLR is a *runtime*. – Daniel A. White Sep 27 '11 at 12:10
  • If you don't want to have the local timezone, don't use `TimeZoneInfo.Local`... – Daniel Hilgarth Sep 27 '11 at 12:15
  • possible duplicate of [How to change the timezone setting of Windows2k8 with .net](http://stackoverflow.com/questions/3408055/how-to-change-the-timezone-setting-of-windows2k8-with-net) – Aamir Sep 27 '11 at 12:18
  • @Garrett Smith: I think that your question isn't really what you want to do; is it possible that you want to get timezone information for a timzeone *other* than the one that is currently set on your local machine? – casperOne Sep 27 '11 at 12:32

2 Answers2

1

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.

TWA
  • 12,756
  • 13
  • 56
  • 92
  • That's not such a good idea, for two reasons: a) it could affect other processes that are running; you can't limit it to just your process (imagine `DateTime.Now` on IIS and `getdate()` in SQL Server on the same machine) and b) it only takes into account the timezone information for the current time; rules for time zones change over time, so the time zone information is really a function of the date that you want to see the time zone information for. – casperOne Sep 27 '11 at 12:34
  • @casperOne - Yeah, I know it isn't a good idea, but it is the only way I know it can be done. – TWA Sep 27 '11 at 12:39
  • See [my answer](http://stackoverflow.com/questions/7568864/change-net-clr-time-zone-without-using-registry/7569086#7569086) for alternatives; I've had great success with the [tz database](http://www.twinsun.com/tz/tz-link.htm) to process time zone information and ignored the [`TimeZone` class](http://msdn.microsoft.com/en-us/library/system.timezone.aspx) completely for years now. – casperOne Sep 27 '11 at 12:41
  • @casperOne - It boils down to what he really wants to do. Your solution is definitely more comprehensive, but if he just wants to get the current time in US-Eastern instead of US-Central then it might be a little overkill. – TWA Sep 27 '11 at 13:11
  • Perhaps, but performing an operation that has a system-wide impact for a process-wide operation (at most) seems reckless at best. – casperOne Sep 27 '11 at 13:24
  • @casperOne - Yes, but I said "...the only way I know to..." I am not necessarily recommending that he do it, which is why I offered the utility class idea. Our answers are definitely geared toward two different scenarios. Without more specific information about his situation it is impossible to evaluate which answer is more appropriate, although as I said yours is definitely more comprehensive. – TWA Sep 27 '11 at 13:39
0

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).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
casperOne
  • 73,706
  • 19
  • 184
  • 253