mirror of
https://github.com/morgan9e/FreeRDP
synced 2026-04-15 00:44:19 +09:00
[winpr,timezone] unify TZ override and restore
This commit is contained in:
@@ -30,7 +30,7 @@ if(WITH_TIMEZONE_FROM_FILE)
|
||||
install(FILES TimeZoneNameMap.json DESTINATION ${WINPR_RESOURCE_ROOT})
|
||||
endif()
|
||||
|
||||
set(SRCS TimeZoneNameMapUtils.c TimeZoneNameMap.h timezone.c)
|
||||
set(SRCS TimeZoneNameMapUtils.c TimeZoneNameMap.h timezone.c timezone.h)
|
||||
if(WITH_TIMEZONE_COMPILED)
|
||||
list(APPEND SRCS TimeZoneNameMap_static.h)
|
||||
endif()
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include <winpr/string.h>
|
||||
#include "timezone.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@@ -73,25 +74,14 @@ static void append_timezone(const char* dir, const char* name)
|
||||
if (!tz)
|
||||
return;
|
||||
|
||||
const char* otz = getenv("TZ");
|
||||
char* oldtz = NULL;
|
||||
if (otz)
|
||||
oldtz = _strdup(otz);
|
||||
setenv("TZ", tz, 1);
|
||||
tzset();
|
||||
char* oldtz = setNewAndSaveOldTZ(tz);
|
||||
|
||||
const time_t t = time(NULL);
|
||||
struct tm lt = { 0 };
|
||||
(void)localtime_r(&t, <);
|
||||
append(tz, lt.tm_zone);
|
||||
if (oldtz)
|
||||
{
|
||||
setenv("TZ", oldtz, 1);
|
||||
free(oldtz);
|
||||
}
|
||||
else
|
||||
unsetenv("TZ");
|
||||
restoreSavedTZ(oldtz);
|
||||
free(tz);
|
||||
tzset();
|
||||
}
|
||||
|
||||
static void handle_link(const char* base, const char* dir, const char* name);
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <winpr/assert.h>
|
||||
#include <winpr/file.h>
|
||||
#include "../log.h"
|
||||
#include "timezone.h"
|
||||
|
||||
#define TAG WINPR_TAG("timezone")
|
||||
|
||||
@@ -875,31 +876,12 @@ DWORD EnumDynamicTimeZoneInformation(const DWORD dwIndex,
|
||||
const time_t t = time(NULL);
|
||||
struct tm tres = { 0 };
|
||||
|
||||
const char* tz = getenv("TZ");
|
||||
char* tzcopy = NULL;
|
||||
if (tz)
|
||||
{
|
||||
size_t tzianalen = 0;
|
||||
winpr_asprintf(&tzcopy, &tzianalen, "TZ=%s", tz);
|
||||
}
|
||||
char* tzcopy = entry->Iana ? setNewAndSaveOldTZ(entry->Iana) : NULL;
|
||||
|
||||
char* tziana = NULL;
|
||||
{
|
||||
size_t tzianalen = 0;
|
||||
winpr_asprintf(&tziana, &tzianalen, "TZ=%s", entry->Iana);
|
||||
}
|
||||
if (tziana)
|
||||
putenv(tziana);
|
||||
|
||||
tzset();
|
||||
struct tm* local_time = localtime_r(&t, &tres);
|
||||
free(tziana);
|
||||
if (tzcopy)
|
||||
putenv(tzcopy);
|
||||
else
|
||||
unsetenv("TZ");
|
||||
free(tzcopy);
|
||||
tzset();
|
||||
|
||||
if (entry->Iana)
|
||||
restoreSavedTZ(tzcopy);
|
||||
|
||||
if (local_time)
|
||||
dynamic_time_zone_from_localtime(local_time, lpTimeZoneInformation);
|
||||
@@ -936,3 +918,32 @@ DWORD GetDynamicTimeZoneInformationEffectiveYears(
|
||||
return ERROR_FILE_NOT_FOUND;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32)
|
||||
char* setNewAndSaveOldTZ(const char* val)
|
||||
{
|
||||
// NOLINTBEGIN(concurrency-mt-unsafe)
|
||||
const char* otz = getenv("TZ");
|
||||
char* oldtz = NULL;
|
||||
if (otz)
|
||||
oldtz = _strdup(otz);
|
||||
setenv("TZ", val, 1);
|
||||
tzset();
|
||||
// NOLINTEND(concurrency-mt-unsafe)
|
||||
return oldtz;
|
||||
}
|
||||
|
||||
void restoreSavedTZ(char* saved)
|
||||
{
|
||||
// NOLINTBEGIN(concurrency-mt-unsafe)
|
||||
if (saved)
|
||||
{
|
||||
setenv("TZ", saved, 1);
|
||||
free(saved);
|
||||
}
|
||||
else
|
||||
unsetenv("TZ");
|
||||
tzset();
|
||||
// NOLINTEND(concurrency-mt-unsafe)
|
||||
}
|
||||
#endif
|
||||
|
||||
26
winpr/libwinpr/timezone/timezone.h
Normal file
26
winpr/libwinpr/timezone/timezone.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* WinPR: Windows Portable Runtime
|
||||
* Time Zone
|
||||
*
|
||||
* Copyright 2025 Armin Novak <anovak@thincast.com>
|
||||
* Copyright 2025 Thincast Technologies GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(_WIN32)
|
||||
char* setNewAndSaveOldTZ(const char* val);
|
||||
void restoreSavedTZ(char* saved);
|
||||
#endif
|
||||
Reference in New Issue
Block a user