TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttktime.cpp
Go to the documentation of this file.
1 #include "ttktime.h"
2 
3 #if TTK_QT_VERSION_CHECK(5,10,0)
4 # include <QRandomGenerator>
5 #endif
6 
8 {
9 #if !TTK_QT_VERSION_CHECK(5,10,0)
10  qsrand(QDateTime::currentMSecsSinceEpoch());
11 #endif
12 }
13 
14 int TTK::random(int value)
15 {
16 #if TTK_QT_VERSION_CHECK(5,10,0)
17  return QRandomGenerator::global()->bounded(value);
18 #else
19  return qrand() % value;
20 #endif
21 }
22 
23 
24 
25 TTKTime::TTKTime() noexcept
26 {
27  initialize();
28 }
29 
30 TTKTime::TTKTime(qint64 value) noexcept
31  : TTKTime()
32 {
33  initialize();
34  fromValue(value);
35 }
36 
37 TTKTime::TTKTime(int day, int hour, int min, int sec, int msec) noexcept
38  : TTKTime()
39 {
40  initialize();
41  fromValue(day, hour, min, sec, msec);
42 }
43 
44 TTKTime::TTKTime(const TTKTime &other) noexcept
45 {
46  copyToThis(other);
47 }
48 
49 TTKTime::TTKTime(TTKTime &&other) noexcept
50 {
51  copyToThis(other);
52 }
53 
54 bool TTKTime::isNull() const noexcept
55 {
56  return m_hour == 0 && m_minute == 0 && m_second == 0 && m_msecond == 0;
57 }
58 
59 bool TTKTime::isValid() const noexcept
60 {
61  return !isNull();
62 }
63 
64 TTKTime TTKTime::fromString(const QString &time, const QString &format) noexcept
65 {
66  TTKTime t;
67  const QTime &qtime = QTime::fromString(time, format);
68  t.fromValue(0, qtime.hour(), qtime.minute(), qtime.second(), qtime.msec());
69  return t;
70 }
71 
72 QString TTKTime::toString(qint64 time, const QString &format) noexcept
73 {
74  return TTKTime(time).toString(format);
75 }
76 
77 QString TTKTime::toString(const QString &format) const noexcept
78 {
79  return QTime(m_hour, m_minute, m_second, m_msecond).toString(format);
80 }
81 
82 void TTKTime::fromValue(int day, int hour, int min, int sec, int msec) noexcept
83 {
84  if(day < 0 || hour < 0 || hour > 24 || min < 0 || min > 60 || sec < 0 || sec > 60 || msec < 0 || msec > 1000)
85  {
86  return;
87  }
88 
89  m_day = day;
90  m_hour = hour;
91  m_minute = min;
92  m_second = sec;
93  m_msecond = msec;
94 }
95 
96 void TTKTime::fromValue(qint64 value) noexcept
97 {
98  if(value < 0)
99  {
100  return;
101  }
102 
103  m_msecond = value % TTK_DN_S2MS;
104  value /= TTK_DN_S2MS;
105 
106  m_day = value / TTK_DN_D2S;
107  value %= TTK_DN_D2S;
108 
109  m_hour = value / TTK_DN_H2S;
110  value %= TTK_DN_H2S;
111 
112  m_minute = value / TTK_DN_M2S;
113  value %= TTK_DN_M2S;
114 
115  m_second = value;
116 }
117 
118 qint64 TTKTime::toValue() const noexcept
119 {
121 }
122 
123 qint64 TTKTime::formatDuration(const QString &time) noexcept
124 {
125  return TTKTime::fromString(time, "mm:ss").toValue();
126 }
127 
128 QString TTKTime::formatDuration(qint64 time/*, bool greedy*/) noexcept
129 {
130  const TTKTime t(time);
131  if(time < TTK_DN_H2MS)
132  {
133  return t.toString("mm:ss");
134  }
135  else
136  {
137  const int min = t.day() * TTK_DN_D2M + t.hour() * TTK_DN_H2M + t.minute();
138  return QString::number(min).rightJustified(2, '0') + ":" + QString::number(t.second()).rightJustified(2, '0');
139  }
140 }
141 
142 TTKTime& TTKTime::operator= (const TTKTime &other) noexcept
143 {
144  copyToThis(other);
145  return *this;
146 }
147 
149 {
150  copyToThis(other);
151  return *this;
152 }
153 
154 TTKTime& TTKTime::operator+= (const TTKTime &other) noexcept
155 {
156  fromValue(toValue() + other.toValue());
157  return *this;
158 }
159 
160 TTKTime& TTKTime::operator+= (const int other) noexcept
161 {
162  fromValue(toValue() + other);
163  return *this;
164 }
165 
166 TTKTime& TTKTime::operator-= (const TTKTime &other) noexcept
167 {
168  fromValue(toValue() - other.toValue());
169  return *this;
170 }
171 
172 TTKTime& TTKTime::operator-= (const int other) noexcept
173 {
174  fromValue(toValue() - other);
175  return *this;
176 }
177 
178 TTKTime& TTKTime::operator*= (const int other) noexcept
179 {
180  fromValue(toValue() * other);
181  return *this;
182 }
183 
184 TTKTime& TTKTime::operator/= (const int other) noexcept
185 {
186  fromValue(toValue() / other);
187  return *this;
188 }
189 
190 TTKTime TTKTime::operator+ (const TTKTime &other) noexcept
191 {
192  return TTKTime(toValue() + other.toValue());
193 }
194 
195 TTKTime TTKTime::operator+ (const int other) noexcept
196 {
197  return TTKTime(toValue() + other);
198 }
199 
200 TTKTime TTKTime::operator- (const TTKTime &other) noexcept
201 {
202  return TTKTime(toValue() - other.toValue());
203 }
204 
205 TTKTime TTKTime::operator- (const int other) noexcept
206 {
207  return TTKTime(toValue() - other);
208 }
209 
210 TTKTime TTKTime::operator* (const int other) noexcept
211 {
212  return TTKTime(toValue() * other);
213 }
214 
215 TTKTime TTKTime::operator/ (const int other) noexcept
216 {
217  return TTKTime(toValue() / other);
218 }
219 
220 bool TTKTime::operator== (const TTKTime &other) const noexcept
221 {
222  return toValue() == other.toValue();
223 }
224 
225 bool TTKTime::operator!= (const TTKTime &other) const noexcept
226 {
227  return toValue() != other.toValue();
228 }
229 
230 void TTKTime::initialize() noexcept
231 {
232  m_day = 0;
233  m_hour = 0;
234  m_minute = 0;
235  m_second = 0;
236  m_msecond = 0;
237 }
238 
239 void TTKTime::copyToThis(const TTKTime &other) noexcept
240 {
241  m_day = other.m_day;
242  m_hour = other.m_hour;
243  m_minute = other.m_minute;
244  m_second = other.m_second;
245  m_msecond = other.m_msecond;
246 }
247 
248 
250 {
251  return QDateTime::currentMSecsSinceEpoch();
252 }
253 
254 QString TTKDateTime::format(const QString &time, const QString &format)
255 {
256  return QString::number(QDateTime::fromString(time, format).toMSecsSinceEpoch());
257 }
258 
259 QString TTKDateTime::format(qint64 time, const QString &format)
260 {
261  return QDateTime::fromMSecsSinceEpoch(time).toString(format);
262 }
TTKTime & operator*=(const int other) noexcept
Definition: ttktime.cpp:178
int minute() const noexcept
Definition: ttktime.h:82
TTKTime operator+(const TTKTime &other) noexcept
Definition: ttktime.cpp:190
TTKTime & operator/=(const int other) noexcept
Definition: ttktime.cpp:184
TTKTime & operator-=(const TTKTime &other) noexcept
Definition: ttktime.cpp:166
void fromValue(int day, int hour, int min, int sec, int msec=0) noexcept
Definition: ttktime.cpp:82
TTKTime() noexcept
Definition: ttktime.cpp:25
TTK_MODULE_EXPORT void initRandom()
Definition: ttktime.cpp:7
bool operator==(const TTKTime &other) const noexcept
Definition: ttktime.cpp:220
void copyToThis(const TTKTime &other) noexcept
Definition: ttktime.cpp:239
#define TTK_DN_D2M
Definition: ttkglobal.h:295
static TTKTime fromString(const QString &time, const QString &format) noexcept
Definition: ttktime.cpp:64
TTK_MODULE_EXPORT int random(int value=RAND_MAX)
Definition: ttktime.cpp:14
int m_day
Definition: ttktime.h:185
TTKTime operator*(const int other) noexcept
Definition: ttktime.cpp:210
int day() const noexcept
Definition: ttktime.h:74
#define TTK_DN_H2MS
Definition: ttkglobal.h:289
int hour() const noexcept
Definition: ttktime.h:78
int m_minute
Definition: ttktime.h:186
TTKTime & operator+=(const TTKTime &other) noexcept
Definition: ttktime.cpp:154
TTKTime & operator=(const TTKTime &other) noexcept
Definition: ttktime.cpp:142
#define TTK_DN_H2M
Definition: ttkglobal.h:287
int second() const noexcept
Definition: ttktime.h:86
#define TTK_DN_H2S
Definition: ttkglobal.h:288
bool isNull() const noexcept
Definition: ttktime.cpp:54
#define TTK_DN_D2S
Definition: ttkglobal.h:296
int m_second
Definition: ttktime.h:186
int m_msecond
Definition: ttktime.h:186
The class of the ttk time object.
Definition: ttktime.h:28
static qint64 currentTimestamp()
Definition: ttktime.cpp:249
#define TTK_DN_S2MS
Definition: ttkglobal.h:276
TTKTime operator-(const TTKTime &other) noexcept
Definition: ttktime.cpp:200
bool isValid() const noexcept
Definition: ttktime.cpp:59
void initialize() noexcept
Definition: ttktime.cpp:230
TTKTime operator/(const int other) noexcept
Definition: ttktime.cpp:215
static QString toString(qint64 time, const QString &format) noexcept
Definition: ttktime.cpp:72
#define const
Definition: zconf.h:233
#define TTK_DN_M2S
Definition: ttkglobal.h:281
static QString format(const QString &time, const QString &format)
Definition: ttktime.cpp:254
int m_hour
Definition: ttktime.h:185
bool operator!=(const TTKTime &other) const noexcept
Definition: ttktime.cpp:225
constexpr const _Tp & min(const _Tp &a, const _Tp &b)
Definition: ttkcompat.h:27
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123
qint64 toValue() const noexcept
Definition: ttktime.cpp:118