2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
24 #include "android/bionic_supplement/bionic_supplement.h"
26 #include "PlatformInclude.h"
27 #include "LinuxTimezone.h"
28 #include "utils/SystemInfo.h"
29 #if defined(TARGET_DARWIN)
30 #include "osx/OSXGNUReplacements.h"
33 #include "freebsd/FreeBSDGNUReplacements.h"
40 CLinuxTimezone::CLinuxTimezone() : m_IsDST(0)
44 int nameonfourthfield = 0;
46 vector<CStdString> tokens;
49 FILE* fp = fopen("/usr/share/zoneinfo/zone.tab", "r");
52 CStdString countryCode;
53 CStdString timezoneName;
55 while (getdelim(&line, &linelen, '\n', fp) > 0)
59 s.TrimLeft(" \t").TrimRight(" \n");
67 CUtil::Tokenize(s, tokens, " \t");
68 if (tokens.size() < 3)
71 countryCode = tokens[0];
72 timezoneName = tokens[2];
74 if (m_timezonesByCountryCode.count(countryCode) == 0)
76 vector<CStdString> timezones;
77 timezones.push_back(timezoneName);
78 m_timezonesByCountryCode[countryCode] = timezones;
82 vector<CStdString>& timezones = m_timezonesByCountryCode[countryCode];
83 timezones.push_back(timezoneName);
86 m_countriesByTimezoneName[timezoneName] = countryCode;
99 fp = fopen("/usr/share/zoneinfo/iso3166.tab", "r");
102 fp = fopen("/usr/share/misc/iso3166", "r");
103 nameonfourthfield = 1;
107 CStdString countryCode;
108 CStdString countryName;
110 while (getdelim(&line, &linelen, '\n', fp) > 0)
113 s.TrimLeft(" \t").TrimRight(" \n");
121 // Search for the first non space from the 2nd character and on
123 while (s[i] == ' ' || s[i] == '\t') i++;
125 if (nameonfourthfield)
128 while (s[i] != ' ' && s[i] != '\t') i++;
129 while (s[i] == ' ' || s[i] == '\t') i++;
131 while (s[i] != ' ' && s[i] != '\t') i++;
132 while (s[i] == ' ' || s[i] == '\t') i++;
135 countryCode = s.Left(2);
136 countryName = s.Mid(i);
138 m_counties.push_back(countryName);
139 m_countryByCode[countryCode] = countryName;
140 m_countryByName[countryName] = countryCode;
142 sort(m_counties.begin(), m_counties.end(), sortstringbyname());
148 vector<CStdString> CLinuxTimezone::GetCounties()
153 vector<CStdString> CLinuxTimezone::GetTimezonesByCountry(const CStdString country)
155 return m_timezonesByCountryCode[m_countryByName[country]];
158 CStdString CLinuxTimezone::GetCountryByTimezone(const CStdString timezone)
160 #if defined(TARGET_DARWIN)
161 return CStdString("?");
163 return m_countryByCode[m_countriesByTimezoneName[timezone]];
167 void CLinuxTimezone::SetTimezone(CStdString timezoneName)
169 bool use_timezone = false;
171 #if !defined(TARGET_DARWIN)
174 if (g_sysinfo.IsAppleTV2())
180 static char env_var[255];
181 sprintf(env_var, "TZ=:%s", timezoneName.c_str());
187 CStdString CLinuxTimezone::GetOSConfiguredTimezone()
189 char timezoneName[255];
191 // try Slackware approach first
192 ssize_t rlrc = readlink("/etc/localtime-copied-from"
193 , timezoneName, sizeof(timezoneName)-1);
196 timezoneName[rlrc] = '\0';
198 char* p = strrchr(timezoneName,'/');
200 { // we want the previous '/'
203 p = strrchr(timezoneName,'/');
211 // now try Debian approach
213 FILE* fp = fopen("/etc/timezone", "r");
216 if (fgets(timezoneName, sizeof(timezoneName), fp))
217 timezoneName[strlen(timezoneName)-1] = '\0';
224 CLinuxTimezone g_timezone;