From 1a69975131d85cdfb235ec1ad959b52fcf7ea167 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 29 Nov 2023 09:09:36 +0300 Subject: [PATCH] Replaced Qt format of dates in statistics with internal format. --- Telegram/Resources/langs/lang.strings | 5 ++ .../data/data_statistics_chart.cpp | 8 +-- .../info_statistics_inner_widget.cpp | 8 +-- .../SourceFiles/statistics/chart_widget.cpp | 12 ++-- .../statistics/statistics_format_values.cpp | 68 +++++++++++++++++++ .../statistics/statistics_format_values.h | 16 +++++ .../widgets/point_details_widget.cpp | 36 ++-------- .../statistics/widgets/point_details_widget.h | 2 - Telegram/cmake/td_ui.cmake | 2 + 9 files changed, 110 insertions(+), 47 deletions(-) create mode 100644 Telegram/SourceFiles/statistics/statistics_format_values.cpp create mode 100644 Telegram/SourceFiles/statistics/statistics_format_values.h diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index cd78ed4d1..4df401e10 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -4337,6 +4337,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_stats_story_title" = "Story Statistic"; "lng_stats_zoom_out" = "Zoom Out"; +"lng_stats_day_month_year" = "{days_count} {month} {year}"; +"lng_stats_day_month" = "{days_count} {month}"; +"lng_stats_weekday_day_month_year" = "{day}, {days_count} {month} {year}"; +"lng_stats_weekday_day_month_time" = "{day}, {days_count} {month} {time}"; + "lng_stats_overview_title" = "Overview"; "lng_stats_overview_member_count" = "Followers"; "lng_stats_overview_mean_view_count" = "Views Per Post"; diff --git a/Telegram/SourceFiles/data/data_statistics_chart.cpp b/Telegram/SourceFiles/data/data_statistics_chart.cpp index fbfc42b81..62f0354dd 100644 --- a/Telegram/SourceFiles/data/data_statistics_chart.cpp +++ b/Telegram/SourceFiles/data/data_statistics_chart.cpp @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "data/data_statistics_chart.h" +#include "statistics/statistics_format_values.h" + #include #include @@ -44,20 +46,18 @@ void StatisticalChart::measure() { const auto dateCount = int((end - start) / timeStep) + 10; daysLookup.reserve(dateCount); constexpr auto kOneDay = 3600 * 24 * 1000; - const auto formatter = u"d MMM"_q; for (auto i = 0; i < dateCount; i++) { const auto r = (start + (i * timeStep)) / 1000; - const auto dateTime = QDateTime::fromSecsSinceEpoch(r); if (timeStep == 1) { daysLookup.push_back( QString(((i < 10) ? u"0%1:00"_q : u"%1:00"_q).arg(i))); } else if (timeStep < kOneDay) { + const auto dateTime = QDateTime::fromSecsSinceEpoch(r); daysLookup.push_back(u"%1:%2"_q .arg(dateTime.time().hour(), 2, 10, QChar('0')) .arg(dateTime.time().minute(), 2, 10, QChar('0'))); } else { - const auto date = dateTime.date(); - daysLookup.push_back(QLocale().toString(date, formatter)); + daysLookup.push_back(Statistic::LangDayMonth(r)); } } diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp b/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp index 3fba3ab22..29d0ff3a7 100644 --- a/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp +++ b/Telegram/SourceFiles/info/statistics/info_statistics_inner_widget.cpp @@ -27,6 +27,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "settings/settings_common.h" // CreateLottieIcon. #include "statistics/chart_widget.h" #include "statistics/statistics_common.h" +#include "statistics/statistics_format_values.h" #include "statistics/widgets/chart_header_widget.h" #include "ui/layers/generic_box.h" #include "ui/rect.h" @@ -289,14 +290,11 @@ void AddHeader( header->setSubTitle({}); return; } - const auto formatter = u"d MMM yyyy"_q; - const auto from = QDateTime::fromSecsSinceEpoch(startDate); - const auto to = QDateTime::fromSecsSinceEpoch(endDate); - header->setSubTitle(QLocale().toString(from.date(), formatter) + header->setSubTitle(Statistic::LangDayMonthYear(startDate) + ' ' + QChar(8212) + ' ' - + QLocale().toString(to.date(), formatter)); + + Statistic::LangDayMonthYear(endDate)); } void FillOverview( diff --git a/Telegram/SourceFiles/statistics/chart_widget.cpp b/Telegram/SourceFiles/statistics/chart_widget.cpp index e3e57796c..4b13f1316 100644 --- a/Telegram/SourceFiles/statistics/chart_widget.cpp +++ b/Telegram/SourceFiles/statistics/chart_widget.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/qt/qt_key_modifiers.h" #include "lang/lang_keys.h" #include "statistics/chart_lines_filter_controller.h" +#include "statistics/statistics_format_values.h" #include "statistics/view/abstract_chart_view.h" #include "statistics/view/chart_view_factory.h" #include "statistics/view/stack_chart_common.h" @@ -72,22 +73,19 @@ void FillLineColorsByKey(Data::StatisticalChart &chartData) { if (leftTimestamp < kOneDay) { return {}; } - const auto formatter = u"d MMM yyyy"_q; - const auto leftDateTime = QDateTime::fromSecsSinceEpoch( - leftTimestamp / 1000); - const auto leftText = QLocale().toString(leftDateTime.date(), formatter); + const auto leftText = LangDayMonthYear(leftTimestamp / 1000); if ((xIndexMin == xIndexMax) && !chartData.weekFormat) { return leftText; } else { constexpr auto kSevenDays = 3600 * 24 * 7; - const auto rightDateTime = QDateTime::fromSecsSinceEpoch(0 + const auto rightTimestamp = 0 + (chartData.x[xIndexMax] / 1000) - + (chartData.weekFormat ? kSevenDays : 0)); + + (chartData.weekFormat ? kSevenDays : 0); return leftText + ' ' + QChar(8212) + ' ' - + QLocale().toString(rightDateTime.date(), formatter); + + LangDayMonthYear(rightTimestamp); } } diff --git a/Telegram/SourceFiles/statistics/statistics_format_values.cpp b/Telegram/SourceFiles/statistics/statistics_format_values.cpp new file mode 100644 index 000000000..1b33784d7 --- /dev/null +++ b/Telegram/SourceFiles/statistics/statistics_format_values.cpp @@ -0,0 +1,68 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "statistics/statistics_format_values.h" + +#include "base/unixtime.h" +#include "lang/lang_keys.h" + +namespace Statistic { + +QString LangDayMonthYear(crl::time seconds) { + const auto date = base::unixtime::parse(seconds).date(); + return tr::lng_stats_day_month_year( + tr::now, + lt_days_count, + QString::number(date.day()), + lt_month, + Lang::MonthSmall(date.month())(tr::now), + lt_year, + QString::number(date.year())); +} + +QString LangDayMonth(crl::time seconds) { + const auto date = base::unixtime::parse(seconds).date(); + return tr::lng_stats_day_month( + tr::now, + lt_days_count, + QString::number(date.day()), + lt_month, + Lang::MonthSmall(date.month())(tr::now)); +} + +QString LangDetailedDayMonth(crl::time seconds) { + const auto dateTime = base::unixtime::parse(seconds); + if (dateTime.toUTC().time().hour() || dateTime.toUTC().time().minute()) { + constexpr auto kOneDay = 3600 * 24; + if (seconds < kOneDay) { + return QLocale().toString(dateTime, QLocale::ShortFormat); + } + return tr::lng_stats_weekday_day_month_time( + tr::now, + lt_day, + Lang::Weekday(dateTime.date().dayOfWeek())(tr::now), + lt_days_count, + QString::number(dateTime.date().day()), + lt_month, + Lang::MonthSmall(dateTime.date().month())(tr::now), + lt_time, + QLocale().toString(dateTime.time(), QLocale::ShortFormat)); + } else { + return tr::lng_stats_weekday_day_month_year( + tr::now, + lt_day, + Lang::Weekday(dateTime.date().dayOfWeek())(tr::now), + lt_days_count, + QString::number(dateTime.date().day()), + lt_month, + Lang::MonthSmall(dateTime.date().month())(tr::now), + lt_year, + QString::number(dateTime.date().year())); + } +} + +} // namespace Statistic diff --git a/Telegram/SourceFiles/statistics/statistics_format_values.h b/Telegram/SourceFiles/statistics/statistics_format_values.h new file mode 100644 index 000000000..7fa881e5c --- /dev/null +++ b/Telegram/SourceFiles/statistics/statistics_format_values.h @@ -0,0 +1,16 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +namespace Statistic { + +[[nodiscard]] QString LangDayMonthYear(crl::time seconds); +[[nodiscard]] QString LangDayMonth(crl::time seconds); +[[nodiscard]] QString LangDetailedDayMonth(crl::time seconds); + +} // namespace Statistic diff --git a/Telegram/SourceFiles/statistics/widgets/point_details_widget.cpp b/Telegram/SourceFiles/statistics/widgets/point_details_widget.cpp index 4baa07539..c820aab97 100644 --- a/Telegram/SourceFiles/statistics/widgets/point_details_widget.cpp +++ b/Telegram/SourceFiles/statistics/widgets/point_details_widget.cpp @@ -7,9 +7,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "statistics/widgets/point_details_widget.h" -#include "ui/cached_round_corners.h" #include "statistics/statistics_common.h" +#include "statistics/statistics_format_values.h" #include "statistics/view/stack_linear_chart_common.h" +#include "ui/cached_round_corners.h" #include "ui/effects/ripple_animation.h" #include "ui/painter.h" #include "ui/rect.h" @@ -22,32 +23,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Statistic { namespace { -[[nodiscard]] QString FormatTimestamp( - float64 timestamp, - const QString &longFormat, - const QString &shortFormat) { - const auto dateTime = QDateTime::fromSecsSinceEpoch(timestamp / 1000); - if (dateTime.toUTC().time().hour() || dateTime.toUTC().time().minute()) { - return QLocale().toString(dateTime, longFormat); - } else { - return QLocale().toString(dateTime.date(), shortFormat); - } -} - [[nodiscard]] QString FormatWeek(float64 timestamp) { constexpr auto kSevenDays = 3600 * 24 * 7; - const auto leftFormatter = u"d MMM"_q; - const auto rightFormatter = u"d MMM yyyy"_q; timestamp /= 1000; - return QLocale().toString( - QDateTime::fromSecsSinceEpoch(timestamp).date(), - leftFormatter) + return LangDayMonth(timestamp) + ' ' + QChar(8212) + ' ' - + QLocale().toString( - QDateTime::fromSecsSinceEpoch(timestamp + kSevenDays).date(), - rightFormatter); + + LangDayMonthYear(timestamp + kSevenDays); } void PaintShadow(QPainter &p, int radius, const QRect &r) { @@ -149,9 +132,7 @@ PointDetailsWidget::PointDetailsWidget( , _zoomEnabled(zoomEnabled) , _chartData(chartData) , _textStyle(st::statisticsDetailsPopupStyle) -, _headerStyle(st::statisticsDetailsPopupHeaderStyle) -, _longFormat(u"ddd, d MMM hh:mm"_q) -, _shortFormat(u"ddd, d MMM yyyy"_q) { +, _headerStyle(st::statisticsDetailsPopupHeaderStyle) { if (zoomEnabled) { rpl::single(rpl::empty_value()) | rpl::then( @@ -209,10 +190,7 @@ PointDetailsWidget::PointDetailsWidget( _headerStyle, _chartData.weekFormat ? FormatWeek(_chartData.x.front()) - : FormatTimestamp( - _chartData.x.front(), - _longFormat, - _shortFormat)); + : LangDetailedDayMonth(_chartData.x.front() / 1000)); maxNameTextWidth = std::max( maxHeaderText.maxWidth() + st::statisticsDetailsPopupPadding.left(), @@ -278,7 +256,7 @@ void PointDetailsWidget::setXIndex(int xIndex) { ? _chartData.getDayString(xIndex) : _chartData.weekFormat ? FormatWeek(timestamp) - : FormatTimestamp(timestamp, _longFormat, _shortFormat)); + : LangDetailedDayMonth(timestamp / 1000)); } _lines.clear(); diff --git a/Telegram/SourceFiles/statistics/widgets/point_details_widget.h b/Telegram/SourceFiles/statistics/widgets/point_details_widget.h index 219ef7183..52ed7dd34 100644 --- a/Telegram/SourceFiles/statistics/widgets/point_details_widget.h +++ b/Telegram/SourceFiles/statistics/widgets/point_details_widget.h @@ -46,8 +46,6 @@ private: const Data::StatisticalChart &_chartData; const style::TextStyle &_textStyle; const style::TextStyle &_headerStyle; - const QString _longFormat; - const QString _shortFormat; Ui::Text::String _header; void invalidateCache(); diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index 9c533fc82..bacd9fe15 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -186,6 +186,8 @@ PRIVATE statistics/statistics_common.h statistics/statistics_data_deserialize.cpp statistics/statistics_data_deserialize.h + statistics/statistics_format_values.cpp + statistics/statistics_format_values.h statistics/view/abstract_chart_view.cpp statistics/view/abstract_chart_view.h statistics/view/bar_chart_view.cpp