-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Expand file tree
/
Copy pathdata_birthday.cpp
More file actions
132 lines (112 loc) · 2.85 KB
/
Copy pathdata_birthday.cpp
File metadata and controls
132 lines (112 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
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 "data/data_birthday.h"
#include "base/timer_rpl.h"
#include "lang/lang_keys.h"
#include <QtCore/QDate>
namespace Data {
namespace {
[[nodiscard]] bool Validate(int day, int month, int year) {
if (year != 0
&& (year < Birthday::kYearMin || year > Birthday::kYearMax)) {
return false;
} else if (day < 1) {
return false;
} else if (month == 2) {
if (day == 29) {
return !year
|| (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
return day <= 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return day <= 30;
} else if (month > 0 && month <= 12) {
return day <= 31;
}
return false;
}
[[nodiscard]] int Serialize(int day, int month, int year) {
return day + month * 100 + year * 10000;
}
} // namespace
Birthday::Birthday(int day, int month, int year)
: _value(Validate(day, month, year) ? Serialize(day, month, year) : 0) {
}
Birthday Birthday::FromSerialized(int value) {
return Birthday(value % 100, (value / 100) % 100, value / 10000);
}
int Birthday::serialize() const {
return _value;
}
bool Birthday::valid() const {
return _value != 0;
}
int Birthday::day() const {
return _value % 100;
}
int Birthday::month() const {
return (_value / 100) % 100;
}
int Birthday::year() const {
return _value / 10000;
}
QString BirthdayText(Birthday date, bool fullMonth) {
const auto wrapMonth = fullMonth
? Lang::MonthDay
: Lang::MonthSmall;
if (const auto year = date.year()) {
return tr::lng_month_day_year(
tr::now,
lt_month,
wrapMonth(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,
wrapMonth(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