16

I know we could change the current culture for the current thread. And I know we couldn't get TimeZoneInfo from the CurrentCulture because one culture may has many TimeZones like USA

But to use the same technique to deal with TimeZone for current thread.

It would be very nice if we could make something like this:

TimeZone.CurrentTimeZone = TimeZoneInfo.FindSystemTimeZoneById("timezone id");
Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
  • 1
    We're not a desired features for .NET site, we're a solid answers to solid questions site. Timezone handling is non-trivial at the best of times, adding the layer of complexity of a timezone per-thread is just asking for trouble – Anya Shenanigans Mar 28 '13 at 13:13
  • 1
    @Petesh I didn't mean to ask why actually. I was wondering if that is possible in some way. – Wahid Bitar Mar 28 '13 at 13:20
  • 1
    There is nothing stopping you creating a thread-local object for timezones, it's just that nothing except your code will make use of it. You should look at the [`ThreadLocal` Class](http://msdn.microsoft.com/en-us/library/dd642243.aspx) in .NET for a trivial way to implement it. – Anya Shenanigans Mar 28 '13 at 13:57
  • 1
    I'm writing unit tests for some code where I don't control the call to DateTime.Now and so want to do exactly this. As the questioner mentions, switching the culture on the current thread where the test is running is a very common technique, why can't you do the same for time zone information? (I was hoping you could.) – Chris Oldwood Jun 04 '18 at 20:34

1 Answers1

11

Unfortunately, any concept of the "current" time zone is tied to the operating system settings of the machine that the code is running on. There are some Win32 apis for changing the time zone, but I don't recommend using them. Not only are they not "thread-safe", but they aren't "process-safe" either. The time zone setting affects everything running on the machine.

That said, I'd be curious what your use case really is. If you are in the position to set the timezone per thread, then you are probably in a position to not rely on the local setting at all. You can probably make use of the conversion methods on TimeZoneInfo instead.

For example, say you were looking for the current time in some other time zone. You might be looking for the ability to do this:

using (TimeZone.CurrentTimeZone = ...  )
{
    var now = DateTime.Now;
}

But instead you should simply convert from UTC where appropriate:

var now = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(
                       DateTime.UtcNow, "some other timezone id");
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • The time-zone is used implicitly in a number of different functions (e.g. conversion from string to date-time using `AdjustToUniversal`). They can be surely changed manually to use an explicit time-zone, but it is not easy in a big code-base. In the same way you can explicitly specify the culture to use in functions like `string.Format()` - but there you have the alternative to set the culture for the thread and 'fix them all' in one go. – MiMo Jun 06 '13 at 16:16
  • 4
    Yes, it used internally by many functions, but no - you cannot change it. There are no hooks, and everything is sealed. One would think it would behave like cultureinfo, but it doesn't. It's completely different. There is only a single system-wide time zone setting. – Matt Johnson-Pint Jun 06 '13 at 16:28
  • Yes, I know - my previous comment was an attempted answer to your question 'what is (would be) the use case' of such a thing - if it existed. – MiMo Jun 07 '13 at 13:24
  • Use `DateTimeOffset`, not date-time, if working with data in any timezone other than the computer-wide OS timezone setting. – Ben Voigt Jun 07 '21 at 18:40
  • Don't use timezones. Seriously. This problem and more will go away. – Jay K Jul 23 '21 at 22:52
  • (Just for the record; DateTimeOffset isn't good enough either - it has all these fancy AddHours/Minutes/etc. methods but they don't account for DST changes. It's impossible since it only knows the DateTime and the offset - and the offset is not enough information to be able to do time arithmetic correctly.) – AnorZaken Feb 06 '23 at 02:17