File indexing completed on 2025-02-09 04:28:32

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 
0008 */
0009 
0010 #include "datetime.h"
0011 
0012 #include "util.h"
0013 
0014 #include <QDateTime>
0015 
0016 QVariant timeSince(const QDateTime &early, const QDateTime &late)
0017 {
0018     Q_ASSERT(early.isValid());
0019     Q_ASSERT(late.isValid());
0020 
0021     auto secsSince = early.secsTo(late);
0022 
0023     if (secsSince < 0)
0024         return SafeString(QStringLiteral("0 minutes"));
0025 
0026     // TODO: i18n
0027     QStringList singularNames;
0028     singularNames << QStringLiteral("year") << QStringLiteral("month") << QStringLiteral("week") << QStringLiteral("day") << QStringLiteral("hour")
0029                   << QStringLiteral("minute");
0030 
0031     QStringList pluralNames;
0032     pluralNames << QStringLiteral("years") << QStringLiteral("months") << QStringLiteral("weeks") << QStringLiteral("days") << QStringLiteral("hours")
0033                 << QStringLiteral("minutes");
0034 
0035     QList<int> seconds;
0036     seconds << (60 * 60 * 24 * 365) // year
0037             << (60 * 60 * 24 * 30) // month
0038             << (60 * 60 * 24 * 7) // week
0039             << (60 * 60 * 24) // day
0040             << (60 * 60) // hour
0041             << (60); // minute
0042 
0043     auto count = secsSince;
0044     auto i = 0;
0045     while (i < seconds.size()) {
0046         count = (secsSince / seconds.at(i));
0047         ++i;
0048         if (count != 0)
0049             break;
0050     }
0051     QString firstChunk;
0052 
0053     if (count != 1)
0054         firstChunk.append(QStringLiteral("%1 %2").arg(count).arg(pluralNames.at(i - 1)));
0055     else {
0056         firstChunk.append(QStringLiteral("%1 %2").arg(count).arg(singularNames.at(i - 1)));
0057     }
0058     if (seconds.size() > i) {
0059         auto count2 = (secsSince - (seconds.at(i - 1) * count)) / seconds.at(i);
0060         if (count2 != 0) {
0061             if (count2 > 1)
0062                 firstChunk.append(QStringLiteral(", %1 %2").arg(count2).arg(pluralNames.at(i)));
0063             else
0064                 firstChunk.append(QStringLiteral(", %1 %2").arg(count2).arg(singularNames.at(i)));
0065         }
0066     }
0067     return firstChunk;
0068 }
0069 
0070 QVariant DateFilter::doFilter(const QVariant &input, const QVariant &argument, bool autoescape) const
0071 {
0072     Q_UNUSED(autoescape)
0073     QDateTime d;
0074     if (input.userType() == QMetaType::QDateTime) {
0075         d = input.toDateTime();
0076     } else if (input.userType() == QMetaType::QDate) {
0077         d.setDate(input.toDate());
0078     } else if (input.userType() == QMetaType::QTime) {
0079         d.setTime(input.toTime());
0080     } else {
0081         d = QDateTime::fromString(getSafeString(input), QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz"));
0082     }
0083 
0084     auto argString = getSafeString(argument);
0085 
0086     if (!argString.get().isEmpty())
0087         return d.toString(argString);
0088 
0089     return d.toString(QStringLiteral("MMM. d, yyyy"));
0090 }
0091 
0092 QVariant TimeFilter::doFilter(const QVariant &input, const QVariant &argument, bool autoescape) const
0093 {
0094     Q_UNUSED(autoescape)
0095     QDateTime d;
0096     if (input.userType() == QMetaType::QDateTime) {
0097         d = input.toDateTime();
0098     } else if (input.userType() == QMetaType::QDate) {
0099         d.setDate(input.toDate());
0100     } else if (input.userType() == QMetaType::QTime) {
0101         d.setTime(input.toTime());
0102     } else {
0103         d = QDateTime::fromString(getSafeString(input), QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz"));
0104     }
0105 
0106     auto argString = getSafeString(argument);
0107     return d.toString(argString);
0108 }
0109 
0110 QVariant TimeSinceFilter::doFilter(const QVariant &input, const QVariant &argument, bool autoescape) const
0111 {
0112     Q_UNUSED(autoescape)
0113     QDateTime late;
0114     if (argument.userType() != qMetaTypeId<QDateTime>())
0115         late = QDateTime::currentDateTime();
0116     else
0117         late = argument.value<QDateTime>();
0118 
0119     auto early = input.value<QDateTime>();
0120     if (!early.isValid())
0121         return {};
0122     return timeSince(early, late);
0123 }
0124 
0125 QVariant TimeUntilFilter::doFilter(const QVariant &input, const QVariant &argument, bool autoescape) const
0126 {
0127     Q_UNUSED(autoescape)
0128     QDateTime early;
0129     if (argument.userType() != qMetaTypeId<QDateTime>())
0130         early = QDateTime::currentDateTime();
0131     else
0132         early = argument.value<QDateTime>();
0133 
0134     auto late = input.value<QDateTime>();
0135     if (!late.isValid())
0136         return {};
0137     return timeSince(early, late);
0138 }