MythTV  master
mythtimezone.cpp
Go to the documentation of this file.
1 #include "mythtimezone.h"
2 
3 #include <cstdlib> // for malloc, getenv
4 #include <ctime>
5 
6 #include <QDataStream>
7 #include <QTextStream>
8 #include <QDateTime>
9 #include <QTimeZone>
10 #include <QFileInfo>
11 #include <QDir>
12 
13 #include "mythcorecontext.h"
14 #include "mythlogging.h"
15 #include "mythdate.h"
16 
17 #if QT_VERSION < QT_VERSION_CHECK(5,10,0)
18 #define qEnvironmentVariable getenv
19 #endif
20 
21 namespace MythTZ
22 {
23 
24 int calc_utc_offset(void)
25 {
26  QDateTime loc = QDateTime::currentDateTime();
27  QDateTime utc = loc.toUTC();
28  loc = QDateTime(loc.date(), loc.time(), Qt::UTC);
29  return utc.secsTo(loc);
30 }
31 
32 /* Helper function for getTimeZoneID() that provides an unprocessed time zone
33  id obtained using system-dependent means of identifying the system's time
34  zone. */
35 static QString getSystemTimeZoneID(void)
36 {
37  QString zone_id("UNDEF");
38 #ifdef _WIN32
39  // struct _TIME_ZONE_INFORMATION { ...
40  // GetTimeZoneInformation();
41  // ...
42  // Sadly, Windows zone names are different to the (probably Unix)
43  // backend's names - "AUS Eastern Standard Time" vs "Australia/Sydney".
44  // Translation is not worthwhile. Leave it as UNDEF to check the offset.
45 #else
46  QDateTime dt = QDateTime::currentDateTime();
47  zone_id = dt.timeZone().id();
48 #endif
49  return zone_id;
50 }
51 
56 QString getTimeZoneID(void)
57 {
58  QString zone_id("UNDEF");
59 #ifndef _WIN32
60  // First, try the TZ environment variable to check for environment-specific
61  // overrides
62  QString tz = qEnvironmentVariable("TZ");
63  if (tz.isEmpty())
64  {
65  // No TZ, so attempt to determine the system-configured time zone ID
66  tz = getSystemTimeZoneID();
67  }
68 
69  if (!tz.isEmpty())
70  {
71  zone_id = tz;
72  if (zone_id.startsWith("\"") || zone_id.startsWith("'"))
73  zone_id.remove(0, 1);
74  if (zone_id.endsWith("\"") || zone_id.endsWith("'"))
75  zone_id.chop(1);
76  if (zone_id.startsWith(":"))
77  zone_id.remove(0, 1);
78  // the "posix/" subdirectory typically contains the same files as the
79  // "zoneinfo/" parent directory, but are not typically what are in use
80  if (zone_id.startsWith("posix/"))
81  zone_id.remove(0, 6);
82  }
83 
84 #endif
85  return zone_id;
86 }
87 
92 bool checkTimeZone(void)
93 {
94  return true;
95 }
96 
99 bool checkTimeZone(const QStringList &/*master_settings*/)
100 {
101  return true;
102 }
103 
104 }; // namespace MythTZ
MythTZ::getTimeZoneID
QString getTimeZoneID(void)
Returns the zoneinfo time zone ID or as much time zone information as possible.
Definition: mythtimezone.cpp:56
MythTZ::getSystemTimeZoneID
static QString getSystemTimeZoneID(void)
Definition: mythtimezone.cpp:35
mythdate.h
mythlogging.h
MythTZ::checkTimeZone
bool checkTimeZone(void)
Verifies the time zone settings on this system agree with those on the master backend.
Definition: mythtimezone.cpp:92
MythTZ
Definition: mythtimezone.cpp:21
MythTZ::calc_utc_offset
int calc_utc_offset(void)
Definition: mythtimezone.cpp:24
mythcorecontext.h
mythtimezone.h