-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.cpp
More file actions
43 lines (37 loc) · 1.03 KB
/
Copy pathtimestamp.cpp
File metadata and controls
43 lines (37 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "timestamp.h"
TimeStamp::TimeStamp(QObject *parent)
: QObject{parent},
m_timeStamp(""),
m_tempTimeStamp(""),
m_timer(new QTimer(this))
{
m_timer.setInterval(1000);
connect(&m_timer, &QTimer::timeout, this, [=](){
TimeStamp::getTimeStamp();
TimeStamp::setTimeStamp(m_tempTimeStamp);
});
m_timer.start();
}
QString TimeStamp::timeStamp() const
{
return m_timeStamp;
}
void TimeStamp::setTimeStamp(const QString &newTimeStamp)
{
if (m_timeStamp == newTimeStamp)
return;
m_timeStamp = newTimeStamp;
emit timeStampChanged(m_timeStamp);
}
void TimeStamp::getTimeStamp()
{
std::time_t now = std::time(nullptr);
struct tm *timeStruct = std::localtime(&now);
if (timeStruct != nullptr) {
QTime time(timeStruct->tm_hour, timeStruct->tm_min, timeStruct->tm_sec);
m_tempTimeStamp = time.toString("hh:mm:ss");
} else {
// Xử lý lỗi khi không thể lấy thời gian
m_tempTimeStamp = "Error: Unable to get timestamp";
}
}