Torc  0.1
torctime.cpp
Go to the documentation of this file.
1 /* Class TorcTime
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2018
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torctime.h"
25 #include "torclogging.h"
26 #include "torclocalcontext.h"
27 #include "torcadminthread.h"
28 
38  : QObject(), TorcHTTPService(this, QStringLiteral("time"), QStringLiteral("time"), TorcTime::staticMetaObject, QStringLiteral("Tick")),
39  currentTime(),
40  m_lastTime(QDateTime::currentDateTime()),
41  m_timer(),
42  m_dateTimeFormat()
43 {
44  // NB if the language changes, the device will be restarted so no need to worry about format changes
45  m_dateTimeFormat = gLocalContext->GetLocale().dateTimeFormat(QLocale::LongFormat);
46  m_timer.setInterval(1000);
47  connect(&m_timer, &QTimer::timeout, this, &TorcTime::Tick);
48  m_timer.start();
49 }
50 
52 {
53  QReadLocker locker(&m_httpServiceLock);
54  return m_lastTime.toString(m_dateTimeFormat);
55 }
56 
58 {
59  QReadLocker locker(&m_httpServiceLock);
60  return m_lastTime.toUTC().toString(Qt::ISODate);
61 }
62 
63 void TorcTime::Tick(void)
64 {
65  QWriteLocker locker(&m_httpServiceLock);
66  QDateTime timenow = QDateTime::currentDateTime();
67  qint64 difference = timenow.secsTo(m_lastTime);
68  m_lastTime = timenow;
69  // half an hour change triggers an event
70  // anything in th order of seconds can be triggered under heavy load,
71  // so compromise between seconds and a full hour.
72  if (qAbs(difference) > (30 * 60))
73  {
74  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("Detected change in system time"));
76  }
77 
78  QString time = m_lastTime.toString(m_dateTimeFormat);
79  emit currentTimeChanged(time);
80 }
81 
82 void TorcTime::SubscriberDeleted(QObject *Subscriber)
83 {
85 }
86 
90 static class TorcTimeObject : public TorcAdminObject
91 {
92  public:
95  m_time(nullptr)
96  {
97  }
98 
99  virtual ~TorcTimeObject()
100  {
101  Destroy();
102  }
103 
104  void Create(void)
105  {
106  Destroy();
107  m_time = new TorcTime();
108  }
109 
110  void Destroy(void)
111  {
112  if (m_time)
113  delete m_time;
114  m_time = nullptr;
115  }
116 
117  private:
118  Q_DISABLE_COPY(TorcTimeObject)
119  TorcTime *m_time;
120 
void currentTimeChanged(QString &Time)
TorcLocalContext * gLocalContext
QString GetCurrentTimeUTC(void)
Definition: torctime.cpp:57
A factory class for automatically running objects outside of the main loop.
friend class TorcTimeObject
Definition: torctime.h:14
virtual ~TorcTimeObject()
Definition: torctime.cpp:99
void Create(void)
Definition: torctime.cpp:104
void Destroy(void)
Definition: torctime.cpp:110
static void NotifyEvent(int Event)
#define TORC_ADMIN_LOW_PRIORITY
void SubscriberDeleted(QObject *Subscriber)
Definition: torctime.cpp:82
void HandleSubscriberDeleted(QObject *Subscriber)
QReadWriteLock m_httpServiceLock
A static class used to create the TorcTime singleton in the admin thread.
Definition: torctime.cpp:90
QString GetCurrentTime(void)
Definition: torctime.cpp:51
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
void Tick(void)
Definition: torctime.cpp:63
TorcTime()
Definition: torctime.cpp:37
QLocale GetLocale(void)
A simple time service to confirm the time (and date) the system is currently operating with...
Definition: torctime.h:12