Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically change system time zone

Tags:

timezone

c#

.net

I'm working on changing system time zone from within WPF application. To do this i'm using unmanaged code that i've found on this site:changing time zones. It does change my time zone. However when I click on windows' clock i see a message: "Your current time zone is not recognized. Please select a valid time zone using the link below." I could imagine there is a slight difference somewhere. I've analyzed one time zone - how it's represented before programmatic change and after.

BEFORE:

BaseUtcOffset: 4:00:00:00
Daylight Name: "Arabian Daylight Time"
Display Name: "(UTC +4:00) Abu Dhabi, Muscat"
Id: "Arabian Daylight Time"
Standard Name: "Arabian Daylight Time"
SupportsDaylightSavingTime: false

AFTER:

BaseUtcOffset: -4:00:00:00
Daylight Name: ""
Display Name: "Arabian Daylight Time"
Id: "Arabian Daylight Time"
Standard Name: "Arabian Daylight Time"
SupportsDaylightSavingTime: false

I found no way to set "Display Name" using TIME_ZONE_INFORMATION. So something that it's called "(UTC +4:00) Abu Dhabi, Muscat" on GUI when changed by user manually using Windows setting, now it's called "Arabian Daylight Time". Perhaps those differences are one of the reasons why i see "Your current time zone..." message. I've read somewhere else that restart helps and this message disappears - unfortunately after restart nothing changes.

like image 754
user3046525 Avatar asked Feb 07 '26 12:02

user3046525


1 Answers

Well. I've found my mistake. The problem was that on Windows 7 i was trying to change time zone with:

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]   
private static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);

while i should have used:

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetDynamicTimeZoneInformation([In] ref DynamicTimeZoneInformation lpTimeZoneInformation);

Using DYNAMIC_TIME_ZONE_INFORMATION solved most of the problems regarding my current task.

like image 97
user3046525 Avatar answered Feb 09 '26 01:02

user3046525



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!