diff options
Diffstat (limited to 'accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp')
-rw-r--r-- | accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp index 929b7be6..22b3469b 100644 --- a/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp +++ b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp | |||
@@ -42,9 +42,11 @@ void Chrono::update() { | |||
42 | } | 42 | } |
43 | 43 | ||
44 | void Chrono::setTime(const std::chrono::time_point<std::chrono::system_clock>& targetTime) { | 44 | void Chrono::setTime(const std::chrono::time_point<std::chrono::system_clock>& targetTime) { |
45 | using namespace std::chrono_literals; | ||
46 | |||
45 | auto currentTime = std::chrono::system_clock::now(); | 47 | auto currentTime = std::chrono::system_clock::now(); |
46 | auto offset = std::chrono::duration_cast<std::chrono::milliseconds>(targetTime - currentTime); | 48 | auto offset = std::chrono::duration_cast<std::chrono::milliseconds>(targetTime - currentTime); |
47 | this->currentTime = abs(offset.count()) < 500 ? targetTime : currentTime; | 49 | this->currentTime = abs(offset) < 500ms ? targetTime : currentTime; |
48 | 50 | ||
49 | switch (this->mPrecision) { | 51 | switch (this->mPrecision) { |
50 | case Chrono::Hours: this->currentTime = std::chrono::time_point_cast<std::chrono::hours>(this->currentTime); | 52 | case Chrono::Hours: this->currentTime = std::chrono::time_point_cast<std::chrono::hours>(this->currentTime); |
@@ -56,33 +58,26 @@ void Chrono::setTime(const std::chrono::time_point<std::chrono::system_clock>& t | |||
56 | } | 58 | } |
57 | 59 | ||
58 | void Chrono::schedule(const std::chrono::time_point<std::chrono::system_clock>& targetTime) { | 60 | void Chrono::schedule(const std::chrono::time_point<std::chrono::system_clock>& targetTime) { |
61 | using namespace std::chrono_literals; | ||
62 | |||
59 | auto currentTime = std::chrono::system_clock::now(); | 63 | auto currentTime = std::chrono::system_clock::now(); |
60 | auto offset = std::chrono::duration_cast<std::chrono::milliseconds>(targetTime - currentTime); | 64 | auto offset = std::chrono::duration_cast<std::chrono::milliseconds>(targetTime - currentTime); |
61 | auto nextTime = abs(offset.count()) < 500 ? targetTime : currentTime; | 65 | auto nextTime = abs(offset) < 500ms ? targetTime : currentTime; |
62 | |||
63 | { | ||
64 | using namespace std::chrono_literals; | ||
65 | 66 | ||
66 | switch (this->mPrecision) { | 67 | switch (this->mPrecision) { |
67 | case Chrono::Hours: nextTime = std::chrono::time_point_cast<std::chrono::hours>(nextTime) + 1h; | 68 | case Chrono::Hours: nextTime = std::chrono::time_point_cast<std::chrono::hours>(nextTime) + 1h; |
68 | case Chrono::Minutes: nextTime = std::chrono::time_point_cast<std::chrono::minutes>(nextTime) + 1min; | 69 | case Chrono::Minutes: nextTime = std::chrono::time_point_cast<std::chrono::minutes>(nextTime) + 1min; |
69 | case Chrono::Seconds: nextTime = std::chrono::time_point_cast<std::chrono::seconds>(nextTime) + 1s; | 70 | case Chrono::Seconds: nextTime = std::chrono::time_point_cast<std::chrono::seconds>(nextTime) + 1s; |
70 | } | ||
71 | } | 71 | } |
72 | 72 | ||
73 | this->targetTime = nextTime; | 73 | this->targetTime = nextTime; |
74 | auto delay = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - currentTime); | 74 | this->timer.start(std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - currentTime)); |
75 | this->timer.start(delay); | ||
76 | } | 75 | } |
77 | 76 | ||
78 | QString Chrono::format() const { return this->mFormat; } | 77 | QString Chrono::format(const QString& fmt) const { |
79 | void Chrono::setFormat(QString format) { | 78 | return QString::fromStdString(std::format(std::runtime_format(fmt.toStdString()), std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::time_point_cast<std::chrono::seconds>(this->currentTime)))); |
80 | if (format == this->mFormat) return; | ||
81 | this->mFormat = format; | ||
82 | emit this->formatChanged(); | ||
83 | this->update(); | ||
84 | } | 79 | } |
85 | 80 | ||
86 | QString Chrono::date() const { | 81 | QDateTime Chrono::date() const { |
87 | return QString::fromStdString(std::format(std::runtime_format(this->mFormat.toStdString()), std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::time_point_cast<std::chrono::seconds>(this->currentTime)))); | 82 | return QDateTime::fromStdTimePoint(std::chrono::time_point_cast<std::chrono::milliseconds>(this->currentTime)); |
88 | } | 83 | } |