File indexing completed on 2026-07-12 13:30:34

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 (unit == QLatin1String("Celsius") || (unit == QLatin1String("Use System Default") && QLocale().measurementSystem() == QLocale::MetricSystem)) {
0025         return QStringLiteral("℃");
0026     } else {
0027         return QStringLiteral("℉");
0028     }
0029 }
0030 
0031 QString Formatter::formatTemperature(qreal temperature, const QString &unit) const
0032 {
0033     return i18nc("A temperature", "%1°", Kweather::convertTemp(temperature, unit));
0034 }
0035 
0036 QString Formatter::formatTemperatureRounded(qreal temperature, const QString &unit) const
0037 {
0038     return i18nc("A temperature", "%1°", qRound(Kweather::convertTemp(temperature, unit)));
0039 }
0040 
0041 QString Formatter::formatWindSpeed(qreal speed, const QString &unit) const
0042 {
0043     if (unit == QLatin1String("kph")) {
0044         return i18n("%1 km/h", QString::number(speed, 'f', 1));
0045     } else if (unit == QLatin1String("mph")) {
0046         return i18n("%1 mph", QString::number(speed * 0.62, 'f', 1));
0047     } else {
0048         return i18n("%1 m/s", QString::number(speed * 1000 / 3600, 'f', 1));
0049     }
0050 }
0051 
0052 QString Formatter::formatPressure(qreal pressure, const QString &unit) const
0053 {
0054     if (unit == QLatin1String("hPa")) {
0055         return i18n("%1 hPa", QString::number(pressure, 'f', 1));
0056     } else {
0057         return i18n("%1 mmHg", QString::number(pressure * 0.7500638, 'f', 1));
0058     }
0059 }
0060 
0061 QString Formatter::formatSunriseTime(QDateTime date, const QString &timeZone) const
0062 {
0063     if (!date.isValid()) {
0064         return i18nc("sunrise time not available", "-");
0065     }
0066     return QLocale().toString(date.toTimeZone(QTimeZone(timeZone.toUtf8())).time(), QLocale::ShortFormat).toLower();
0067 }
0068 QString Formatter::formatPrecipitation(qreal precipitation, const QString &unit) const
0069 {
0070     if (unit == QStringLiteral("in")) {
0071         return i18nc("in as inches", "%1 in", QString::number(precipitation * 0.03937008, 'f', 2));
0072     } else {
0073         return i18nc("mm as millimeters", "%1 mm", QString::number(precipitation, 'f', 1));
0074     }
0075 }
0076 QString Formatter::formatHourlyCardDelegateTime(QDateTime date, const QString &timeZone) const
0077 {
0078     return QLocale().toString(date.toTimeZone(QTimeZone(timeZone.toUtf8())).time(), QLocale::ShortFormat).toLower();
0079 }