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 namespace MythTZ
18 {
19 
20 int calc_utc_offset(void)
21 {
22  QDateTime loc = QDateTime::currentDateTime();
23  QDateTime utc = loc.toUTC();
24  loc = QDateTime(loc.date(), loc.time(), Qt::UTC);
25  return utc.secsTo(loc);
26 }
27 
28 /* Helper function for getTimeZoneID() that provides an unprocessed time zone
29  id obtained using system-dependent means of identifying the system's time
30  zone. */
31 static QString getSystemTimeZoneID(void)
32 {
33  QString zone_id("UNDEF");
34 #ifdef _WIN32
35  // struct _TIME_ZONE_INFORMATION { ...
36  // GetTimeZoneInformation();
37  // ...
38  // Sadly, Windows zone names are different to the (probably Unix)
39  // backend's names - "AUS Eastern Standard Time" vs "Australia/Sydney".
40  // Translation is not worthwhile. Leave it as UNDEF to check the offset.
41 #else
42  QDateTime dt = QDateTime::currentDateTime();
43  zone_id = dt.timeZone().id();
44 #endif
45  return zone_id;
46 }
47 
52 QString getTimeZoneID(void)
53 {
54  QString zone_id("UNDEF");
55 #ifndef _WIN32
56  // First, try the TZ environment variable to check for environment-specific
57  // overrides
58  QString tz = getenv("TZ");
59  if (tz.isEmpty())
60  {
61  // No TZ, so attempt to determine the system-configured time zone ID
62  tz = getSystemTimeZoneID();
63  }
64 
65  if (!tz.isEmpty())
66  {
67  zone_id = tz;
68  if (zone_id.startsWith("\"") || zone_id.startsWith("'"))
69  zone_id.remove(0, 1);
70  if (zone_id.endsWith("\"") || zone_id.endsWith("'"))
71  zone_id.chop(1);
72  if (zone_id.startsWith(":"))
73  zone_id.remove(0, 1);
74  // the "posix/" subdirectory typically contains the same files as the
75  // "zoneinfo/" parent directory, but are not typically what are in use
76  if (zone_id.startsWith("posix/"))
77  zone_id.remove(0, 6);
78  }
79 
80 #endif
81  return zone_id;
82 }
83 
88 bool checkTimeZone(void)
89 {
90  return true;
91 }
92 
95 bool checkTimeZone(const QStringList &/*master_settings*/)
96 {
97  return true;
98 }
99 
100 }; // namespace MythTZ
MythTZ::getTimeZoneID
QString getTimeZoneID(void)
Returns the zoneinfo time zone ID or as much time zone information as possible.
Definition: mythtimezone.cpp:52
MythTZ::getSystemTimeZoneID
static QString getSystemTimeZoneID(void)
Definition: mythtimezone.cpp:31
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:88
MythTZ
Definition: mythtimezone.cpp:18
MythTZ::calc_utc_offset
int calc_utc_offset(void)
Definition: mythtimezone.cpp:20
mythcorecontext.h
mythtimezone.h