Programming Tips - C/C++: Set the timezone for the current process

Date: 2023mar29 UPdate: 2026oct2 OS: Windows Language: C/C++ Q. C/C++: Set the timezone for the current process A. The function TzSet() below does it:
#include <stdio.h> #include <stdlib.h> #include <time.h> // Warning: Sets the timezone for all threads! // For Windows and Linux void TzSet(const char *szTz) { #if defined(_WIN32) || defined(_WIN64) _putenv_s("TZ", szTz); _tzset(); #else setenv("TZ", szTz, true); tzset(); #endif } int main() { TzSet("PST5PDT"); system("date"); }
Output:
Fri Oct 2 02:20:46 PM PDT 20NN