X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Futil.cpp;fp=src%2Fcommon%2Futil.cpp;h=a2c4e25ed76b89eb11f17106b761fbc2212aeb46;hp=b42f4fbf5781a616be1cefe5bcbf3bafec737ee2;hb=fa56ee7fc1b94ea27da6b27c919d6df1c0e0490d;hpb=cb63e88d483f74c8f2f6e06c35c9d18be45cebaa diff --git a/src/common/util.cpp b/src/common/util.cpp index b42f4fbf..a2c4e25e 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -363,3 +363,29 @@ bool scopeMatch(const QString &string, const QString &scopeRule, const bool &isR return matches || (invertedRuleFound && !normalRuleFound); } } + + +QString tryFormatUnixEpoch(const QString &possibleEpochDate) +{ + // Does the string resemble a Unix epoch? Parse as 64-bit time + qint64 secsSinceEpoch = possibleEpochDate.toLongLong(); + if (secsSinceEpoch == 0) { + // Parsing either failed, or '0' was sent. No need to distinguish; either way, it's not + // useful as epoch. + // See https://doc.qt.io/qt-5/qstring.html#toLongLong + return possibleEpochDate; + } + + // Time checks out, parse it + QDateTime date; +#if QT_VERSION >= 0x050800 + date.setSecsSinceEpoch(secsSinceEpoch); +#else + // toSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now. + // See https://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch + date.setMSecsSinceEpoch(secsSinceEpoch * 1000); +#endif + + // Return the localized date/time + return date.toString(); +}