1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#ifdef TARGET_ANDROID
#include "platform/android/bionic_supplement/bionic_supplement.h"
#endif
#include "PosixTimezone.h"
#include "ServiceBroker.h"
#include "XBDateTime.h"
#include "filesystem/File.h"
#include "platform/MessagePrinter.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "settings/lib/Setting.h"
#include "settings/lib/SettingDefinitions.h"
#include "utils/DateLib.h"
#include "utils/StringUtils.h"
#include "utils/SystemInfo.h"
#include <stdlib.h>
#include "PlatformDefs.h"
void CPosixTimezone::Init()
{
#if defined(DATE_INTERNAL_TZDATA)
XFILE::CFileStream zonetab;
if (!zonetab.Open("special://xbmc/addons/resource.timezone/resources/tzdata/zone.tab"))
{
CMessagePrinter::DisplayMessage("failed to open zone.tab");
return;
}
std::string countryCode;
std::string timezoneName;
std::vector<std::string> tokens;
for (std::string s; std::getline(zonetab, s);)
{
tokens.clear();
std::string line = s;
StringUtils::Trim(line);
if (line.length() == 0)
continue;
if (line[0] == '#')
continue;
StringUtils::Tokenize(line, tokens, " \t");
if (tokens.size() < 3)
continue;
countryCode = tokens[0];
timezoneName = tokens[2];
if (m_timezonesByCountryCode.count(countryCode) == 0)
{
std::vector<std::string> timezones;
timezones.push_back(timezoneName);
m_timezonesByCountryCode[countryCode] = timezones;
}
else
{
std::vector<std::string>& timezones = m_timezonesByCountryCode[countryCode];
timezones.push_back(timezoneName);
}
m_countriesByTimezoneName[timezoneName] = countryCode;
}
XFILE::CFileStream isotab;
if (!isotab.Open("special://xbmc/addons/resource.timezone/resources/tzdata/iso3166.tab"))
{
CMessagePrinter::DisplayMessage("failed to open iso3166.tab");
return;
}
std::string countryName;
for (std::string s; std::getline(isotab, s);)
{
tokens.clear();
std::string line = s;
StringUtils::Trim(line);
if (line.length() == 0)
continue;
if (line[0] == '#')
continue;
StringUtils::Tokenize(line, tokens, "\t");
if (tokens.size() < 2)
continue;
countryCode = tokens[0];
countryName = tokens[1];
m_countries.push_back(countryName);
m_countryByCode[countryCode] = countryName;
m_countryByName[countryName] = countryCode;
}
sort(m_countries.begin(), m_countries.end(), sortstringbyname());
#else
char* line = NULL;
size_t linelen = 0;
int nameonfourthfield = 0;
std::string s;
std::vector<std::string> tokens;
// Load timezones
FILE* fp = fopen("/usr/share/zoneinfo/zone.tab", "r");
if (fp)
{
std::string countryCode;
std::string timezoneName;
while (getdelim(&line, &linelen, '\n', fp) > 0)
{
tokens.clear();
s = line;
StringUtils::Trim(s);
if (s.length() == 0)
continue;
if (s[0] == '#')
continue;
StringUtils::Tokenize(s, tokens, " \t");
if (tokens.size() < 3)
continue;
countryCode = tokens[0];
timezoneName = tokens[2];
if (m_timezonesByCountryCode.count(countryCode) == 0)
{
std::vector<std::string> timezones;
timezones.push_back(timezoneName);
m_timezonesByCountryCode[countryCode] = timezones;
}
else
{
std::vector<std::string>& timezones = m_timezonesByCountryCode[countryCode];
timezones.push_back(timezoneName);
}
m_countriesByTimezoneName[timezoneName] = countryCode;
}
fclose(fp);
}
if (line)
{
free(line);
line = NULL;
linelen = 0;
}
// Load countries
fp = fopen("/usr/share/zoneinfo/iso3166.tab", "r");
if (!fp)
{
fp = fopen("/usr/share/misc/iso3166", "r");
nameonfourthfield = 1;
}
if (fp)
{
std::string countryCode;
std::string countryName;
while (getdelim(&line, &linelen, '\n', fp) > 0)
{
s = line;
StringUtils::Trim(s);
//! @todo STRING_CLEANUP
if (s.length() == 0)
continue;
if (s[0] == '#')
continue;
// Search for the first non space from the 2nd character and on
int i = 2;
while (s[i] == ' ' || s[i] == '\t')
i++;
if (nameonfourthfield)
{
// skip three letter
while (s[i] != ' ' && s[i] != '\t')
i++;
while (s[i] == ' ' || s[i] == '\t')
i++;
// skip number
while (s[i] != ' ' && s[i] != '\t')
i++;
while (s[i] == ' ' || s[i] == '\t')
i++;
}
countryCode = s.substr(0, 2);
countryName = s.substr(i);
m_countries.push_back(countryName);
m_countryByCode[countryCode] = countryName;
m_countryByName[countryName] = countryCode;
}
sort(m_countries.begin(), m_countries.end(), sortstringbyname());
fclose(fp);
}
free(line);
#endif
}
void CPosixTimezone::OnSettingChanged(const std::shared_ptr<const CSetting>& setting)
{
if (setting == nullptr)
return;
const std::string &settingId = setting->GetId();
if (settingId == CSettings::SETTING_LOCALE_TIMEZONE)
{
SetTimezone(std::static_pointer_cast<const CSettingString>(setting)->GetValue());
}
else if (settingId == CSettings::SETTING_LOCALE_TIMEZONECOUNTRY)
{
// nothing to do here. Changing locale.timezonecountry will trigger an
// update of locale.timezone and automatically adjust its value
// and execute OnSettingChanged() for it as well (see above)
}
}
void CPosixTimezone::OnSettingsLoaded()
{
SetTimezone(CServiceBroker::GetSettingsComponent()->GetSettings()->GetString(CSettings::SETTING_LOCALE_TIMEZONE));
}
std::vector<std::string> CPosixTimezone::GetCountries()
{
return m_countries;
}
std::vector<std::string> CPosixTimezone::GetTimezonesByCountry(const std::string& country)
{
return m_timezonesByCountryCode[m_countryByName[country]];
}
std::string CPosixTimezone::GetCountryByTimezone(const std::string& timezone)
{
#if defined(TARGET_DARWIN)
return "?";
#endif
return m_countryByCode[m_countriesByTimezoneName[timezone]];
}
void CPosixTimezone::SetTimezone(const std::string& timezoneName)
{
#if defined(TARGET_DARWIN)
return;
#endif
setenv("TZ", timezoneName.c_str(), 1);
tzset();
}
std::string CPosixTimezone::GetOSConfiguredTimezone()
{
char timezoneName[255];
// try Slackware approach first
ssize_t rlrc = readlink("/etc/localtime-copied-from", timezoneName, sizeof(timezoneName) - 1);
// RHEL and maybe other distros make /etc/localtime a symlink
if (rlrc == -1)
rlrc = readlink("/etc/localtime", timezoneName, sizeof(timezoneName) - 1);
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
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;
}
void CPosixTimezone::SettingOptionsTimezoneCountriesFiller(
const std::shared_ptr<const CSetting>& setting,
std::vector<StringSettingOption>& list,
std::string& current,
void* data)
{
std::vector<std::string> countries = g_timezone.GetCountries();
for (const auto& country : countries)
list.emplace_back(country, country);
}
void CPosixTimezone::SettingOptionsTimezonesFiller(const std::shared_ptr<const CSetting>& setting,
std::vector<StringSettingOption>& list,
std::string& current,
void* data)
{
current = std::static_pointer_cast<const CSettingString>(setting)->GetValue();
bool found = false;
std::vector<std::string> timezones = g_timezone.GetTimezonesByCountry(CServiceBroker::GetSettingsComponent()->GetSettings()->GetString(CSettings::SETTING_LOCALE_TIMEZONECOUNTRY));
for (const auto& timezone : timezones)
{
if (!found && StringUtils::EqualsNoCase(timezone, current))
found = true;
list.emplace_back(timezone, timezone);
}
if (!found && !timezones.empty())
current = timezones[0];
}
CPosixTimezone g_timezone;
|