Skip to content

Commit

Permalink
[PosixTimezone] Refactor timezone reading
Browse files Browse the repository at this point in the history
  • Loading branch information
sundermann committed Sep 30, 2023
1 parent c94910c commit a0a8b02
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 46 deletions.
90 changes: 50 additions & 40 deletions xbmc/platform/posix/PosixTimezone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
* See LICENSES/README.md for more information.
*/

#include <time.h>
#include "PlatformDefs.h"
#include "PosixTimezone.h"
#include "utils/SystemInfo.h"

#include "ServiceBroker.h"
#include "utils/StringUtils.h"
#include "XBDateTime.h"
#include "settings/lib/Setting.h"
#include "settings/lib/SettingDefinitions.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include <stdlib.h>
#include "settings/lib/Setting.h"
#include "settings/lib/SettingDefinitions.h"
#include "utils/StringUtils.h"
#include "utils/SystemInfo.h"

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <filesystem>
#include <fstream>

#include "PlatformDefs.h"

CPosixTimezone::CPosixTimezone()
{
Expand Down Expand Up @@ -195,44 +198,51 @@ void CPosixTimezone::SetTimezone(const std::string& timezoneName)

std::string CPosixTimezone::GetOSConfiguredTimezone()
{
char timezoneName[255];
std::string timezoneName;

// try Slackware approach first
ssize_t rlrc = readlink("/etc/localtime-copied-from"
, timezoneName, sizeof(timezoneName)-1);
// try Slackware approach first
timezoneName = ReadFromLocaltime("/etc/localtime-copied-from");

// RHEL and maybe other distros make /etc/localtime a symlink
if (rlrc == -1)
rlrc = readlink("/etc/localtime", timezoneName, sizeof(timezoneName)-1);
// RHEL and maybe other distros make /etc/localtime a symlink
if (timezoneName.empty())
timezoneName = ReadFromLocaltime("/etc/localtime");

if (rlrc != -1)
{
timezoneName[rlrc] = '\0';

char* p = strrchr(timezoneName,'/');
if (p)
{ // we want the previous '/'
char* q = p;
*q = 0;
p = strrchr(timezoneName,'/');
*q = '/';
if (p)
p++;
}
return p;
}
// now try Debian approach
if (timezoneName.empty())
timezoneName = ReadFromTimezone("/etc/timezone");

// now try Debian approach
timezoneName[0] = 0;
FILE* fp = fopen("/etc/timezone", "r");
if (fp)
{
if (fgets(timezoneName, sizeof(timezoneName), fp))
timezoneName[strlen(timezoneName)-1] = '\0';
fclose(fp);
}
return timezoneName;
}

std::string CPosixTimezone::ReadFromLocaltime(const std::string_view filename)
{
std::error_code e;
std::string path = std::filesystem::read_symlink(filename, e);

// Read the timezone starting from the second last occurrence of /
size_t pos = path.rfind('/');
if (pos == std::string::npos)
return "";

pos = path.rfind('/', pos - 1);
if (pos == std::string::npos)
return "";

return path.substr(pos + 1);
}

std::string CPosixTimezone::ReadFromTimezone(const std::string_view filename)
{
std::string timezoneName;
std::ifstream file(filename.data());

if (file.is_open())
{
std::getline(file, timezoneName);
file.close();
}

return timezoneName;
return timezoneName;
}

void CPosixTimezone::SettingOptionsTimezoneCountriesFiller(
Expand Down
14 changes: 8 additions & 6 deletions xbmc/platform/posix/PosixTimezone.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ class CPosixTimezone : public ISettingCallback, public ISettingsHandler
void* data);

private:
std::vector<std::string> m_counties;
std::map<std::string, std::string> m_countryByCode;
std::map<std::string, std::string> m_countryByName;

std::map<std::string, std::vector<std::string> > m_timezonesByCountryCode;
std::map<std::string, std::string> m_countriesByTimezoneName;
std::string ReadFromLocaltime(std::string_view filename);
std::string ReadFromTimezone(std::string_view filename);
std::vector<std::string> m_counties;
std::map<std::string, std::string> m_countryByCode;
std::map<std::string, std::string> m_countryByName;

std::map<std::string, std::vector<std::string>> m_timezonesByCountryCode;
std::map<std::string, std::string> m_countriesByTimezoneName;
};

extern CPosixTimezone g_timezone;
Expand Down

0 comments on commit a0a8b02

Please sign in to comment.