How can a Windows program temporarily change its time zone?

后端 未结 8 1791
清酒与你
清酒与你 2021-02-05 20:45

I\'ve written a function to return the time_t value corresponding to midnight on a given day. When there is no midnight for a given day, it returns the earliest tim

相关标签:
8条回答
  • 2021-02-05 21:03

    I have exactly the same requirement:

    ->some processes must be stuck to UTC and others to some timezone different from the Windows system timezone

    After months of (interrupted) study I fall to the conclusion that on Windows it is only possible to set "UTC" or "current" system time zone. So only the following may be done:

         - set TZ="UTC" 
         - unset TZ
    
    0 讨论(0)
  • 2021-02-05 21:12

    I used setlocale(LC_ALL, "deu_aut") to switch the language/country settings to Austria - declared in locale.h. Sadly i haven't found a language/country string for egypt, but perhaps this gives you a hint.

    0 讨论(0)
  • 2021-02-05 21:13

    Use Isolation/Mocking framework - the only one I know of at the moment is Isolator++ which is currently in beta, I'm sure that you can get a copy by asking for one from the good people of Typemock.

    0 讨论(0)
  • 2021-02-05 21:20

    Install a hook to GetTimeZoneInformation, which overrides system data with your own preferences.

    0 讨论(0)
  • 2021-02-05 21:21

    Have you seen the SetTimeZoneInformation Win32 API function?

    0 讨论(0)
  • 2021-02-05 21:22

    Check out _putenv_s and _tzset. In theory, you should be able to set the TZ environment variable for your process with _putenv_s and then use _tzset to set the actual local time zone for your process to what the TZ environment variable is set to.

    I know that this works on Linux with putenv and tzset, and from the documentation of _putenv_s and _tzset, it appears that you should be able to do the same with them on Windows. I haven't actually tried it though.

    0 讨论(0)
提交回复
热议问题