data_birthday.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "data/data_birthday.h"
  8. #include "base/timer_rpl.h"
  9. #include "lang/lang_keys.h"
  10. #include <QtCore/QDate>
  11. namespace Data {
  12. namespace {
  13. [[nodiscard]] bool Validate(int day, int month, int year) {
  14. if (year != 0
  15. && (year < Birthday::kYearMin || year > Birthday::kYearMax)) {
  16. return false;
  17. } else if (day < 1) {
  18. return false;
  19. } else if (month == 2) {
  20. if (day == 29) {
  21. return !year
  22. || (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
  23. }
  24. return day <= 28;
  25. } else if (month == 4 || month == 6 || month == 9 || month == 11) {
  26. return day <= 30;
  27. } else if (month > 0 && month <= 12) {
  28. return day <= 31;
  29. }
  30. return false;
  31. }
  32. [[nodiscard]] int Serialize(int day, int month, int year) {
  33. return day + month * 100 + year * 10000;
  34. }
  35. } // namespace
  36. Birthday::Birthday(int day, int month, int year)
  37. : _value(Validate(day, month, year) ? Serialize(day, month, year) : 0) {
  38. }
  39. Birthday Birthday::FromSerialized(int value) {
  40. return Birthday(value % 100, (value / 100) % 100, value / 10000);
  41. }
  42. int Birthday::serialize() const {
  43. return _value;
  44. }
  45. bool Birthday::valid() const {
  46. return _value != 0;
  47. }
  48. int Birthday::day() const {
  49. return _value % 100;
  50. }
  51. int Birthday::month() const {
  52. return (_value / 100) % 100;
  53. }
  54. int Birthday::year() const {
  55. return _value / 10000;
  56. }
  57. QString BirthdayText(Birthday date) {
  58. if (const auto year = date.year()) {
  59. return tr::lng_month_day_year(
  60. tr::now,
  61. lt_month,
  62. Lang::MonthSmall(date.month())(tr::now),
  63. lt_day,
  64. QString::number(date.day()),
  65. lt_year,
  66. QString::number(year));
  67. } else if (date) {
  68. return tr::lng_month_day(
  69. tr::now,
  70. lt_month,
  71. Lang::MonthSmall(date.month())(tr::now),
  72. lt_day,
  73. QString::number(date.day()));
  74. }
  75. return QString();
  76. }
  77. QString BirthdayCake() {
  78. return QString::fromUtf8("\xf0\x9f\x8e\x82");
  79. }
  80. int BirthdayAge(Birthday date) {
  81. if (!date.year()) {
  82. return 0;
  83. }
  84. const auto now = QDate::currentDate();
  85. const auto day = QDate(date.year(), date.month(), date.day());
  86. if (!day.isValid() || day >= now) {
  87. return 0;
  88. }
  89. auto age = now.year() - date.year();
  90. if (now < QDate(date.year() + age, date.month(), date.day())) {
  91. --age;
  92. }
  93. return age;
  94. }
  95. bool IsBirthdayToday(Birthday date) {
  96. if (!date) {
  97. return false;
  98. }
  99. const auto now = QDate::currentDate();
  100. return date.day() == now.day() && date.month() == now.month();
  101. }
  102. rpl::producer<bool> IsBirthdayTodayValue(Birthday date) {
  103. return rpl::single() | rpl::then(base::timer_each(
  104. 60 * crl::time(1000)
  105. )) | rpl::map([=] {
  106. return IsBirthdayToday(date);
  107. }) | rpl::distinct_until_changed();
  108. }
  109. } // namespace Data