-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
136 lines (117 loc) · 3.8 KB
/
Copy pathmainwindow.cpp
File metadata and controls
136 lines (117 loc) · 3.8 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "settingsdialog.h"
#include <timediff/timediff.hpp>
#include <sstream>
#include <iomanip>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, settingsDialog(nullptr)
, updateTimer(new QTimer(this))
, useTime2(false)
{
ui->setupUi(this);
setWindowTitle("TimeDiff - UTC Time Difference");
// 初始化time1为当前UTC时间
timediff::DateTime now = timediff::now_utc();
std::ostringstream oss;
oss << std::setfill('0') << std::setw(4) << now.year
<< std::setw(2) << now.month
<< std::setw(2) << now.day
<< std::setw(2) << now.hour
<< std::setw(2) << now.minute
<< std::setw(2) << now.second;
time1Str = QString::fromStdString(oss.str());
// 连接设置按钮
connect(ui->settingsButton, &QPushButton::clicked, this, &MainWindow::onSettingsButtonClicked);
// 设置定时器每秒更新
connect(updateTimer, &QTimer::timeout, this, &MainWindow::updateTimeDiff);
updateTimer->start(50);
// 立即更新一次
updateTimeDiff();
}
MainWindow::~MainWindow()
{
delete settingsDialog;
delete ui;
}
void MainWindow::onSettingsButtonClicked()
{
if (!settingsDialog) {
settingsDialog = new SettingsDialog(this);
}
// 设置当前值
settingsDialog->setTime1(time1Str);
settingsDialog->setTime2(time2Str);
settingsDialog->setUseTime2(useTime2);
// 显示对话框
if (settingsDialog->exec() == QDialog::Accepted) {
// 获取新值
time1Str = settingsDialog->getTime1();
time2Str = settingsDialog->getTime2();
useTime2 = settingsDialog->getUseTime2();
// 立即更新显示
updateTimeDiff();
}
}
void MainWindow::updateTimeDiff()
{
try {
// 解析time1
if (time1Str.isEmpty()) {
ui->labelDiff->setText("Time1 is required");
return;
}
timediff::DateTime time1 = timediff::parse_utc_string(time1Str.toStdString());
// 获取time2
timediff::DateTime time2;
if (useTime2 && !time2Str.isEmpty()) {
time2 = timediff::parse_utc_string(time2Str.toStdString());
} else {
time2 = timediff::now_utc();
}
// 计算时间差
timediff::MixedDuration duration = timediff::diff_mixed(time1, time2, false);
// 格式化并显示
QString displayText = formatMixedDuration(
duration.years, duration.months, duration.days,
duration.hours, duration.minutes, duration.seconds
);
ui->labelDiff->setText(displayText);
} catch (...) {
ui->labelDiff->setText("Error calculating time difference");
}
}
QString MainWindow::formatDateTime(const QString &timeStr) const
{
if (timeStr.length() != 14) {
return timeStr;
}
return timeStr.mid(0, 4) + "-" + timeStr.mid(4, 2) + "-" + timeStr.mid(6, 2) + " " +
timeStr.mid(8, 2) + ":" + timeStr.mid(10, 2) + ":" + timeStr.mid(12, 2);
}
QString MainWindow::formatMixedDuration(int64_t years, int64_t months, int64_t days,
int64_t hours, int64_t minutes, int64_t seconds) const
{
QStringList parts;
if (years != 0) {
parts << QString("%1 y").arg(years);
}
if (months != 0) {
parts << QString("%1 mon").arg(months);
}
if (days != 0) {
parts << QString("%1 d").arg(days);
}
if (hours != 0) {
parts << QString("%1 h").arg(hours);
}
if (minutes != 0) {
parts << QString("%1 min").arg(minutes);
}
if (seconds != 0 || parts.isEmpty()) {
parts << QString("%1 s").arg(seconds);
}
return parts.join(" ");
}