File indexing completed on 2024-05-19 05:55:48

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
0003  * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
0004  * SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "formatter.h"
0010 
0011 #include <KLocalizedString>
0012 #include <QString>
0013 #include <QTimeZone>
0014 
0015 #include "global.h"
0016 
0017 double Formatter::convertTemp(qreal temperature, const QString &unit) const
0018 {
0019     return KWeather::convertTemp(temperature, unit);
0020 }
0021 
0022 QString Formatter::formatTemperatureUnitDegrees(const QString &unit) const
0023 {
0024     if (KWeather::isCelsius(unit)) {
0025         return QStringLiteral("℃");
0026     } else {
0027         return QStringLiteral("℉");
0028     }
0029 }
0030 
0031 QString Formatter::formatTemperature(qreal temperature, const QString &unit) const
0032 {
0033     // only have decimals when in celsius
0034     if (KWeather::isCelsius(unit)) {
0035         QString formattedTemperature = QLocale().toString(KWeather::convertTemp(temperature, unit), 'f', 1);
0036         return ki18nc("A temperature", "%1°").subs(formattedTemperature).toString();
0037     } else {
0038         return formatTemperatureRounded(temperature, unit);
0039     }
0040 }
0041 
0042 QString Formatter::formatTemperatureRounded(qreal temperature, const QString &unit) const
0043 {
0044     return i18nc("A temperature", "%1°", qRound(KWeather::convertTemp(temperature, unit)));
0045 }
0046 
0047 QString Formatter::formatWindSpeed(qreal speed, const QString &unit) const
0048 {
0049     QString formattedSpeed;
0050 
0051     if (unit == QLatin1String("kph")) {
0052         formattedSpeed = QLocale().toString(speed, 'f', 1);
0053         return ki18n("%1 km/h").subs(formattedSpeed).toString();
0054     } else if (unit == QLatin1String("mph")) {
0055         formattedSpeed = QLocale().toString(speed * 0.62, 'f', 1);
0056         return ki18n("%1 mph").subs(formattedSpeed).toString();
0057     } else {
0058         formattedSpeed = QLocale().toString(speed * 1000 / 3600, 'f', 1);
0059         return ki18n("%1 m/s").subs(formattedSpeed).toString();
0060     }
0061 }
0062 
0063 QString Formatter::formatPressure(qreal pressure, const QString &unit) const
0064 {
0065     QString formattedPressure;
0066 
0067     if (unit == QLatin1String("hPa")) {
0068         formattedPressure = QLocale().toString(pressure, 'f', 1);
0069         return ki18n("%1 hPa").subs(formattedPressure).toString();
0070     } else {
0071         formattedPressure = QLocale().toString(pressure * 0.7500638, 'f', 1);
0072         return ki18n("%1 mmHg").subs(formattedPressure).toString();
0073     }
0074 }
0075 
0076 QString Formatter::formatPercent(qreal percentage) const
0077 {
0078     return i18nc("%1 represents percent value, % is the percent sign", "%1%", percentage);
0079 }
0080 
0081 QString Formatter::formatDouble(qreal number) const
0082 {
0083     return i18n("%1", QLocale().toString(number));
0084 }
0085 
0086 QString Formatter::formatSunriseTime(QDateTime date, const QString &timeZone) const
0087 {
0088     if (!date.isValid()) {
0089         return i18nc("sunrise time not available", "-");
0090     }
0091     return QLocale().toString(date.toTimeZone(QTimeZone(timeZone.toUtf8())).time(), QLocale::ShortFormat).toLower();
0092 }
0093 
0094 QString Formatter::formatPrecipitation(qreal precipitation, const QString &unit) const
0095 {
0096     QString formattedPrecipitation;
0097 
0098     if (unit == QStringLiteral("in")) {
0099         formattedPrecipitation = QLocale().toString(precipitation * 0.03937008, 'f', 2);
0100         return ki18nc("in as inches", "%1 in").subs(formattedPrecipitation).toString();
0101     } else {
0102         formattedPrecipitation = QLocale().toString(precipitation, 'f', 1);
0103         return ki18nc("mm as millimeters", "%1 mm").subs(formattedPrecipitation).toString();
0104     }
0105 }
0106 
0107 QString Formatter::formatHourlyCardDelegateTime(QDateTime date, const QString &timeZone) const
0108 {
0109     return QLocale().toString(date.toTimeZone(QTimeZone(timeZone.toUtf8())).time(), QLocale::ShortFormat).toLower();
0110 }
0111 
0112 #include "moc_formatter.cpp"