-
Notifications
You must be signed in to change notification settings - Fork 796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Complete TODO : Show more user friendly labels #1002
Open
nishanthkarthik
wants to merge
7
commits into
zealdocs:main
Choose a base branch
from
nishanthkarthik:readable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
85c60b8
Complete TODO : Show more user friendly labels
nishanthkarthik 7d4e43c
Refactor ReadableInterval, fix clang-tidy warnings
nishanthkarthik fbe8126
Refactor ReadableInterval : Add tr, fix more bugs
nishanthkarthik cd0a789
make QDateTime epoch compatible with qt < 5.7
nishanthkarthik cb9b499
fix lgtm warning, promote qint8 to int
nishanthkarthik e8eed26
Merge branch 'master' of https://github.com/zealdocs/zeal into readable
nishanthkarthik 23a76a1
Update time calculation using std::chrono
nishanthkarthik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/**************************************************************************** | ||
** | ||
** Copyright (C) 2015-2018 Oleg Shparber | ||
** Copyright (C) 2013-2014 Jerzy Kozera | ||
** Contact: https://go.zealdocs.org/l/contact | ||
** | ||
** This file is part of Zeal. | ||
** | ||
** Zeal is free software: you can redistribute it and/or modify | ||
** it under the terms of the GNU General Public License as published by | ||
** the Free Software Foundation, either version 3 of the License, or | ||
** (at your option) any later version. | ||
** | ||
** Zeal is distributed in the hope that it will be useful, | ||
** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
** GNU General Public License for more details. | ||
** | ||
** You should have received a copy of the GNU General Public License | ||
** along with Zeal. If not, see <https://www.gnu.org/licenses/>. | ||
** | ||
****************************************************************************/ | ||
|
||
#include "readableinterval.h" | ||
|
||
#include <QStringBuilder> | ||
#include <QStringList> | ||
|
||
using namespace Zeal::Util; | ||
|
||
ReadableInterval::ReadableInterval(QDateTime timestamp, QDateTime reference) : | ||
m_timestamp(timestamp), | ||
m_reference(reference) | ||
{ | ||
m_delta = m_reference.toSecsSinceEpoch() - m_timestamp.toSecsSinceEpoch(); | ||
} | ||
|
||
ReadableInterval::ReadableInterval(QDateTime timestamp) : | ||
m_timestamp(timestamp) | ||
{ | ||
m_reference = QDateTime::currentDateTime(); | ||
m_delta = m_reference.toSecsSinceEpoch() - m_timestamp.toSecsSinceEpoch(); | ||
} | ||
|
||
QString ReadableInterval::pluralForm(QString word, qint64 quantity) | ||
{ | ||
return word + (quantity > 1 ? "s" : ""); | ||
} | ||
|
||
void ReadableInterval::computeDateTimeComponents() | ||
{ | ||
m_isPast = m_delta > 0; | ||
m_year = m_delta / SECONDSPERYEAR; | ||
m_day = (m_delta % SECONDSPERYEAR) / SECONDSPERDAY; | ||
m_hour = ((m_delta % SECONDSPERYEAR) % SECONDSPERDAY) / SECONDSPERHOUR; | ||
m_min = (((m_delta % SECONDSPERYEAR) % SECONDSPERDAY) % SECONDSPERHOUR) / SECONDSPERMINUTE; | ||
m_sec = (((m_delta % SECONDSPERYEAR) % SECONDSPERDAY) % SECONDSPERHOUR) % SECONDSPERMINUTE; | ||
} | ||
|
||
QString ReadableInterval::toReadableString() | ||
{ | ||
if (m_delta == 0) | ||
return ZERO_INTERVAL_STRING; | ||
else { | ||
QStringList list; | ||
qint8 fieldCount = 0; | ||
computeDateTimeComponents(); | ||
if (m_year && ++fieldCount <= MAX_FIELDS_DISPLAYED) | ||
list.append(QStringLiteral("%1 %2").arg(m_year).arg(pluralForm(YEAR, m_year))); | ||
if (m_day && ++fieldCount <= MAX_FIELDS_DISPLAYED) | ||
list.append(QStringLiteral("%1 %2").arg(m_day).arg(pluralForm(DAY, m_day))); | ||
if (m_hour && ++fieldCount <= MAX_FIELDS_DISPLAYED) | ||
list.append(QStringLiteral("%1 %2").arg(m_hour).arg(pluralForm(HOUR, m_hour))); | ||
if (m_min && ++fieldCount <= MAX_FIELDS_DISPLAYED) | ||
list.append(QStringLiteral("%1 %2").arg(m_min).arg(pluralForm(MIN, m_min))); | ||
if (m_sec && ++fieldCount <= MAX_FIELDS_DISPLAYED) | ||
list.append(QStringLiteral("%1 %2").arg(m_sec).arg(pluralForm(SEC, m_sec))); | ||
return QStringLiteral("%1 %2").arg(list.join(JOIN_SEQ)).arg(m_isPast ? PAST_INTERVAL_STRING : FUTURE_INTERVAL_STRING); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/**************************************************************************** | ||
** | ||
** Copyright (C) 2015-2018 Oleg Shparber | ||
** Contact: https://go.zealdocs.org/l/contact | ||
** | ||
** This file is part of Zeal. | ||
** | ||
** Zeal is free software: you can redistribute it and/or modify | ||
** it under the terms of the GNU General Public License as published by | ||
** the Free Software Foundation, either version 3 of the License, or | ||
** (at your option) any later version. | ||
** | ||
** Zeal is distributed in the hope that it will be useful, | ||
** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
** GNU General Public License for more details. | ||
** | ||
** You should have received a copy of the GNU General Public License | ||
** along with Zeal. If not, see <https://www.gnu.org/licenses/>. | ||
** | ||
****************************************************************************/ | ||
|
||
#ifndef ZEAL_UTIL_READABLEINTERVAL_H | ||
#define ZEAL_UTIL_READABLEINTERVAL_H | ||
|
||
#include <QDateTime> | ||
|
||
namespace Zeal { | ||
namespace Util { | ||
|
||
class ReadableInterval | ||
{ | ||
public: | ||
ReadableInterval(QDateTime timestamp, QDateTime reference); | ||
ReadableInterval(QDateTime timestamp); | ||
|
||
QString toReadableString(); | ||
|
||
private: | ||
void computeDateTimeComponents(); | ||
QString pluralForm(QString word, qint64 quantity); | ||
|
||
QDateTime m_timestamp, m_reference; | ||
qint64 m_delta, m_year, m_day, m_hour, m_min, m_sec; | ||
bool m_isPast; | ||
|
||
const qint16 SECONDSPERMINUTE = 60; | ||
const qint16 SECONDSPERHOUR = SECONDSPERMINUTE * 60; | ||
const qint32 SECONDSPERDAY = SECONDSPERHOUR * 24; | ||
const qint32 SECONDSPERYEAR = SECONDSPERDAY * 365; | ||
|
||
const qint8 MAX_FIELDS_DISPLAYED = 3; | ||
|
||
QString ZERO_INTERVAL_STRING = "now"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these should just be constant strings in an anonymous namespace in the source file. |
||
QString PAST_INTERVAL_STRING = "ago"; | ||
QString FUTURE_INTERVAL_STRING = "from now"; | ||
QString YEAR = "Year"; | ||
QString DAY = "Day"; | ||
QString HOUR = "Hour"; | ||
QString MIN = "Minute"; | ||
QString SEC = "Second"; | ||
QString JOIN_SEQ= ", "; | ||
}; | ||
|
||
} // namespace Util | ||
} // namespace Zeal | ||
|
||
#endif // ZEAL_UTIL_READABLEINTERVAL_H |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just make the whole thing a static method?