How to set Timezone per thread? [closed] How to set Timezone per thread? [closed] multithreading multithreading

How to set Timezone per thread? [closed]


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");