File indexing completed on 2024-04-28 04:05:21

0001 /*
0002     SPDX-FileCopyrightText: 2015 Jakob Gruber <jakob.gruber@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "elapsedtime.h"
0008 
0009 QString Time::toString(const QString &format) const {
0010     const int hours = m_seconds / m_secs_per_hour;
0011     int i = m_seconds % m_secs_per_hour;
0012     const int minutes = i / m_secs_per_minute;
0013     const int seconds = i % m_secs_per_minute;
0014 
0015     return format.arg(hours).arg(QString::number(minutes), 2, QLatin1Char('0'))
0016             .arg(QString::number(seconds), 2, QLatin1Char('0'));
0017 }
0018 
0019 ElapsedTime::ElapsedTime() : m_elapsed(0), m_start(0),
0020     m_paused(false), m_stopped(false),
0021     m_next_penalty(10), m_penalty_multiplier(2)
0022 {
0023 }
0024 
0025 void ElapsedTime::addPenaltyTime() {
0026     static const int penalty_cap = 60 * 60; /* 1 hour */
0027 
0028     m_elapsed += m_next_penalty;
0029     if (m_next_penalty < penalty_cap) {
0030         m_next_penalty *= m_penalty_multiplier;
0031     }
0032 }
0033 
0034 void ElapsedTime::start() {
0035     if (m_stopped) {
0036         return;
0037     }
0038     m_start_date = QDateTime::currentDateTime();
0039     m_start = QDateTime::currentMSecsSinceEpoch();
0040 }
0041 
0042 int ElapsedTime::currentTimesliceSecs() const {
0043     return (QDateTime::currentMSecsSinceEpoch() - m_start) / 1000;
0044 }
0045 
0046 void ElapsedTime::pause(bool paused) {
0047     if (paused == m_paused || m_stopped) {
0048         return;
0049     }
0050     m_paused = paused;
0051     if (paused) {
0052         m_elapsed += currentTimesliceSecs();
0053     } else {
0054         m_start = QDateTime::currentMSecsSinceEpoch();
0055     }
0056 }
0057 
0058 void ElapsedTime::stop() {
0059     m_elapsed += currentTimesliceSecs();
0060     m_stopped = true;
0061 }
0062 
0063 int ElapsedTime::elapsedSecs() const {
0064     int elapsed = m_elapsed;
0065     if (!m_paused && !m_stopped) {
0066         elapsed += currentTimesliceSecs();
0067     }
0068     return elapsed;
0069 }
0070 
0071 QDateTime ElapsedTime::startDate() const {
0072     return m_start_date;
0073 }