-void print_time(const char *format, struct tm *current_tm) {
- static char part[512];
- /* Get date & time */
- if (current_tm == NULL) {
- return;
+static bool local_timezone_init = false;
+static const char *local_timezone = NULL;
+static const char *current_timezone = NULL;
+
+void set_timezone(const char *tz) {
+ if (!local_timezone_init) {
+ /* First call, initialize. */
+ local_timezone = getenv("TZ");
+ local_timezone_init = true;
+ }
+ if (tz == NULL || tz[0] == '\0') {
+ /* User wants localtime. */
+ tz = local_timezone;
+ }
+ if (tz != current_timezone) {
+ if (tz) {
+ setenv("TZ", tz, 1);
+ } else {
+ unsetenv("TZ");
+ }
+ tzset();
+ current_timezone = tz;