Improve time handling in NeoChat
This introduces a new NeoChatDateTime object that wraps a QDateTime. The intent is that it can be passed to QML and has a series of functions that format the QDateTime into the various string representations we need. This means we only have to send the single object to QML and then the correct string can be grabbed from there, simplifying the backend. It is also easy to add a new representation if needed as a function with a QString output and Q_PROPERTY can be added and then it will be available.
This commit is contained in:
52
src/libneochat/neochatdatetime.cpp
Normal file
52
src/libneochat/neochatdatetime.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
// SPDX-FileCopyrightText: 2026 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
|
||||
#include "neochatdatetime.h"
|
||||
|
||||
#include <KFormat>
|
||||
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
NeoChatDateTime::NeoChatDateTime(QDateTime dateTime)
|
||||
: m_dateTime(dateTime)
|
||||
{
|
||||
}
|
||||
|
||||
QDateTime NeoChatDateTime::dateTime() const
|
||||
{
|
||||
return m_dateTime;
|
||||
}
|
||||
|
||||
QString NeoChatDateTime::hourMinuteString() const
|
||||
{
|
||||
return m_dateTime.toLocalTime().toString(u"hh:mm"_s);
|
||||
}
|
||||
|
||||
QString NeoChatDateTime::shortDateTime() const
|
||||
{
|
||||
return QLocale().toString(m_dateTime.toLocalTime(), QLocale::ShortFormat);
|
||||
}
|
||||
|
||||
QString NeoChatDateTime::relativeDate() const
|
||||
{
|
||||
KFormat formatter;
|
||||
return formatter.formatRelativeDate(m_dateTime.toLocalTime().date(), QLocale::ShortFormat);
|
||||
}
|
||||
|
||||
QString NeoChatDateTime::relativeDateTime() const
|
||||
{
|
||||
KFormat formatter;
|
||||
return formatter.formatRelativeDateTime(m_dateTime.toLocalTime(), QLocale::ShortFormat);
|
||||
}
|
||||
|
||||
bool NeoChatDateTime::isValid() const
|
||||
{
|
||||
return m_dateTime.isValid();
|
||||
}
|
||||
|
||||
bool NeoChatDateTime::operator==(const QDateTime &right) const
|
||||
{
|
||||
return m_dateTime == right;
|
||||
}
|
||||
|
||||
#include "moc_neochatdatetime.cpp"
|
||||
Reference in New Issue
Block a user