keepassxc

Форк
0
/
Clock.cpp 
111 строк · 2.7 Кб
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

21
QDateTime Clock::currentDateTimeUtc()
22
{
23
    return instance().currentDateTimeUtcImpl();
24
}
25

26
QDateTime Clock::currentDateTime()
27
{
28
    return instance().currentDateTimeImpl();
29
}
30

31
uint Clock::currentSecondsSinceEpoch()
32
{
33
    // TODO: change to toSecsSinceEpoch() when min Qt >= 5.8
34
    return instance().currentDateTimeImpl().toTime_t();
35
}
36

37
qint64 Clock::currentMilliSecondsSinceEpoch()
38
{
39
    return instance().currentDateTimeImpl().toMSecsSinceEpoch();
40
}
41

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

51
QDateTime Clock::datetimeUtc(int year, int month, int day, int hour, int min, int second)
52
{
53
    return {QDate(year, month, day), QTime(hour, min, second), Qt::UTC};
54
}
55

56
QDateTime Clock::datetime(int year, int month, int day, int hour, int min, int second)
57
{
58
    return {QDate(year, month, day), QTime(hour, min, second), Qt::LocalTime};
59
}
60

61
QDateTime Clock::datetimeUtc(qint64 msecSinceEpoch)
62
{
63
    return QDateTime::fromMSecsSinceEpoch(msecSinceEpoch, Qt::UTC);
64
}
65

66
QDateTime Clock::datetime(qint64 msecSinceEpoch)
67
{
68
    return QDateTime::fromMSecsSinceEpoch(msecSinceEpoch, Qt::LocalTime);
69
}
70

71
QDateTime Clock::parse(const QString& text, Qt::DateFormat format)
72
{
73
    return QDateTime::fromString(text, format);
74
}
75

76
QDateTime Clock::parse(const QString& text, const QString& format)
77
{
78
    return QDateTime::fromString(text, format);
79
}
80

81
Clock::~Clock() = default;
82

83
Clock::Clock() = default;
84

85
QDateTime Clock::currentDateTimeUtcImpl() const
86
{
87
    return QDateTime::currentDateTimeUtc();
88
}
89

90
QDateTime Clock::currentDateTimeImpl() const
91
{
92
    return QDateTime::currentDateTime();
93
}
94

95
void Clock::resetInstance()
96
{
97
    m_instance.reset();
98
}
99

100
void Clock::setInstance(Clock* clock)
101
{
102
    m_instance = QSharedPointer<Clock>(clock);
103
}
104

105
const Clock& Clock::instance()
106
{
107
    if (!m_instance) {
108
        m_instance = QSharedPointer<Clock>(new Clock());
109
    }
110
    return *m_instance;
111
}
112

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.