1 /*
2  *  Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 2 or (at your option)
7  *  version 3 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "Clock.h"
18 
19 QSharedPointer<Clock> Clock::m_instance;
20 
currentDateTimeUtc()21 QDateTime Clock::currentDateTimeUtc()
22 {
23     return instance().currentDateTimeUtcImpl();
24 }
25 
currentDateTime()26 QDateTime Clock::currentDateTime()
27 {
28     return instance().currentDateTimeImpl();
29 }
30 
currentSecondsSinceEpoch()31 uint Clock::currentSecondsSinceEpoch()
32 {
33     // TODO: change to toSecsSinceEpoch() when min Qt >= 5.8
34     return instance().currentDateTimeImpl().toTime_t();
35 }
36 
currentMilliSecondsSinceEpoch()37 qint64 Clock::currentMilliSecondsSinceEpoch()
38 {
39     return instance().currentDateTimeImpl().toMSecsSinceEpoch();
40 }
41 
serialized(const QDateTime & dateTime)42 QDateTime Clock::serialized(const QDateTime& dateTime)
43 {
44     auto time = dateTime.time();
45     if (time.isValid() && time.msec() != 0) {
46         return dateTime.addMSecs(-time.msec());
47     }
48     return dateTime;
49 }
50 
datetimeUtc(int year,int month,int day,int hour,int min,int second)51 QDateTime Clock::datetimeUtc(int year, int month, int day, int hour, int min, int second)
52 {
53     return QDateTime(QDate(year, month, day), QTime(hour, min, second), Qt::UTC);
54 }
55 
datetime(int year,int month,int day,int hour,int min,int second)56 QDateTime Clock::datetime(int year, int month, int day, int hour, int min, int second)
57 {
58     return QDateTime(QDate(year, month, day), QTime(hour, min, second), Qt::LocalTime);
59 }
60 
datetimeUtc(qint64 msecSinceEpoch)61 QDateTime Clock::datetimeUtc(qint64 msecSinceEpoch)
62 {
63     return QDateTime::fromMSecsSinceEpoch(msecSinceEpoch, Qt::UTC);
64 }
65 
datetime(qint64 msecSinceEpoch)66 QDateTime Clock::datetime(qint64 msecSinceEpoch)
67 {
68     return QDateTime::fromMSecsSinceEpoch(msecSinceEpoch, Qt::LocalTime);
69 }
70 
parse(const QString & text,Qt::DateFormat format)71 QDateTime Clock::parse(const QString& text, Qt::DateFormat format)
72 {
73     return QDateTime::fromString(text, format);
74 }
75 
parse(const QString & text,const QString & format)76 QDateTime Clock::parse(const QString& text, const QString& format)
77 {
78     return QDateTime::fromString(text, format);
79 }
80 
~Clock()81 Clock::~Clock()
82 {
83 }
84 
Clock()85 Clock::Clock()
86 {
87 }
88 
currentDateTimeUtcImpl() const89 QDateTime Clock::currentDateTimeUtcImpl() const
90 {
91     return QDateTime::currentDateTimeUtc();
92 }
93 
currentDateTimeImpl() const94 QDateTime Clock::currentDateTimeImpl() const
95 {
96     return QDateTime::currentDateTime();
97 }
98 
resetInstance()99 void Clock::resetInstance()
100 {
101     m_instance.reset();
102 }
103 
setInstance(Clock * clock)104 void Clock::setInstance(Clock* clock)
105 {
106     m_instance = QSharedPointer<Clock>(clock);
107 }
108 
instance()109 const Clock& Clock::instance()
110 {
111     if (!m_instance) {
112         m_instance = QSharedPointer<Clock>(new Clock());
113     }
114     return *m_instance;
115 }
116