mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-08-31 06:26:18 +00:00
Display date of birth in user profiles.
This commit is contained in:
@@ -7,6 +7,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
*/
|
||||
#include "data/data_birthday.h"
|
||||
|
||||
#include "base/timer_rpl.h"
|
||||
#include "lang/lang_keys.h"
|
||||
|
||||
#include <QtCore/QDate>
|
||||
|
||||
namespace Data {
|
||||
namespace {
|
||||
|
||||
@@ -66,5 +71,62 @@ int Birthday::year() const {
|
||||
return _value / 10000;
|
||||
}
|
||||
|
||||
QString BirthdayText(Birthday date) {
|
||||
if (const auto year = date.year()) {
|
||||
return tr::lng_month_day_year(
|
||||
tr::now,
|
||||
lt_month,
|
||||
Lang::MonthSmall(date.month())(tr::now),
|
||||
lt_day,
|
||||
QString::number(date.day()),
|
||||
lt_year,
|
||||
QString::number(year));
|
||||
} else if (date) {
|
||||
return tr::lng_month_day(
|
||||
tr::now,
|
||||
lt_month,
|
||||
Lang::MonthSmall(date.month())(tr::now),
|
||||
lt_day,
|
||||
QString::number(date.day()));
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString BirthdayCake() {
|
||||
return QString::fromUtf8("\xf0\x9f\x8e\x82");
|
||||
}
|
||||
|
||||
int BirthdayAge(Birthday date) {
|
||||
if (!date.year()) {
|
||||
return 0;
|
||||
}
|
||||
const auto now = QDate::currentDate();
|
||||
const auto day = QDate(date.year(), date.month(), date.day());
|
||||
if (!day.isValid() || day >= now) {
|
||||
return 0;
|
||||
}
|
||||
auto age = now.year() - date.year();
|
||||
if (now < QDate(date.year() + age, date.month(), date.day())) {
|
||||
--age;
|
||||
}
|
||||
return age;
|
||||
}
|
||||
|
||||
bool IsBirthdayToday(Birthday date) {
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
const auto now = QDate::currentDate();
|
||||
return date.day() == now.day() && date.month() == now.month();
|
||||
}
|
||||
|
||||
rpl::producer<bool> IsBirthdayTodayValue(Birthday date) {
|
||||
return rpl::single() | rpl::then(base::timer_each(
|
||||
60 * crl::time(1000)
|
||||
)) | rpl::map([=] {
|
||||
return IsBirthdayToday(date);
|
||||
}) | rpl::distinct_until_changed();
|
||||
}
|
||||
|
||||
} // namespace Data
|
||||
|
||||
|
@@ -38,4 +38,10 @@ private:
|
||||
|
||||
};
|
||||
|
||||
[[nodiscard]] QString BirthdayText(Birthday date);
|
||||
[[nodiscard]] QString BirthdayCake();
|
||||
[[nodiscard]] int BirthdayAge(Birthday date);
|
||||
[[nodiscard]] bool IsBirthdayToday(Birthday date);
|
||||
[[nodiscard]] rpl::producer<bool> IsBirthdayTodayValue(Birthday date);
|
||||
|
||||
} // namespace Data
|
||||
|
Reference in New Issue
Block a user