diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 5447c6788..b14a12eb8 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -661,10 +661,11 @@ businessOpeningHours time_zone_id:string opening_hours:vector BusinessInfo::get_business_info_object( } return td_api::make_object( location_.get_business_location_object(), work_hours_.get_business_opening_hours_object(), + work_hours_.get_local_business_opening_hours_object(td), greeting_message_.get_business_greeting_message_settings_object(td), away_message_.get_business_away_message_settings_object(td), intro_.get_business_start_page_object(td)); } diff --git a/td/telegram/BusinessWorkHours.cpp b/td/telegram/BusinessWorkHours.cpp index 5634e5745..3fd35476a 100644 --- a/td/telegram/BusinessWorkHours.cpp +++ b/td/telegram/BusinessWorkHours.cpp @@ -6,9 +6,15 @@ // #include "td/telegram/BusinessWorkHours.h" +#include "td/telegram/AuthManager.h" +#include "td/telegram/OptionManager.h" +#include "td/telegram/Td.h" +#include "td/telegram/TimeZoneManager.h" + #include "td/utils/algorithm.h" #include "td/utils/format.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" #include @@ -68,6 +74,46 @@ td_api::object_ptr BusinessWorkHours::get_business return td_api::make_object(time_zone_id_, std::move(intervals)); } +td_api::object_ptr BusinessWorkHours::get_local_business_opening_hours_object( + Td *td) const { + if (is_empty() || td->auth_manager_->is_bot()) { + return nullptr; + } + + auto offset = narrow_cast((td->time_zone_manager_->get_time_zone_offset(time_zone_id_) - + td->option_manager_->get_option_integer("utc_time_offset")) / + 60); + if (offset == 0) { + return get_business_opening_hours_object(); + } + + BusinessWorkHours local_work_hours; + for (auto &interval : work_hours_) { + auto start_minute = interval.start_minute_ - offset; + auto end_minute = interval.end_minute_ - offset; + if (start_minute < 0) { + if (end_minute <= 24 * 60) { + start_minute += 7 * 24 * 60; + end_minute += 7 * 24 * 60; + } else { + local_work_hours.work_hours_.emplace_back(start_minute + 7 * 24 * 60, 7 * 24 * 60); + start_minute = 0; + } + } else if (end_minute > 8 * 24 * 60) { + if (start_minute >= 7 * 24 * 60) { + start_minute -= 7 * 24 * 60; + end_minute -= 7 * 24 * 60; + } else { + local_work_hours.work_hours_.emplace_back(0, end_minute - 7 * 24 * 60); + end_minute = 7 * 24 * 60; + } + } + local_work_hours.work_hours_.emplace_back(start_minute, end_minute); + } + local_work_hours.sanitize_work_hours(); + return local_work_hours.get_business_opening_hours_object(); +} + telegram_api::object_ptr BusinessWorkHours::get_input_business_work_hours() const { if (is_empty()) { return nullptr; diff --git a/td/telegram/BusinessWorkHours.h b/td/telegram/BusinessWorkHours.h index b386334dc..e938d16f2 100644 --- a/td/telegram/BusinessWorkHours.h +++ b/td/telegram/BusinessWorkHours.h @@ -14,6 +14,8 @@ namespace td { +class Td; + class BusinessWorkHours { public: BusinessWorkHours() = default; @@ -26,6 +28,8 @@ class BusinessWorkHours { td_api::object_ptr get_business_opening_hours_object() const; + td_api::object_ptr get_local_business_opening_hours_object(Td *td) const; + telegram_api::object_ptr get_input_business_work_hours() const; template diff --git a/td/telegram/TimeZoneManager.cpp b/td/telegram/TimeZoneManager.cpp index db3e61888..c977f5ae3 100644 --- a/td/telegram/TimeZoneManager.cpp +++ b/td/telegram/TimeZoneManager.cpp @@ -15,6 +15,7 @@ #include "td/utils/algorithm.h" #include "td/utils/buffer.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" #include "td/utils/tl_helpers.h" namespace td { @@ -109,6 +110,16 @@ void TimeZoneManager::tear_down() { parent_.reset(); } +int32 TimeZoneManager::get_time_zone_offset(const string &time_zone_id) { + load_time_zones(); + for (auto &time_zone : time_zones_.time_zones_) { + if (time_zone.id_ == time_zone_id) { + return time_zone.utc_offset_; + } + } + return narrow_cast(G()->get_option_integer("utc_time_offset")); +} + void TimeZoneManager::get_time_zones(Promise> &&promise) { load_time_zones(); if (time_zones_.hash_ != 0) { diff --git a/td/telegram/TimeZoneManager.h b/td/telegram/TimeZoneManager.h index c73fbbfc5..4cc23c34f 100644 --- a/td/telegram/TimeZoneManager.h +++ b/td/telegram/TimeZoneManager.h @@ -28,6 +28,8 @@ class TimeZoneManager final : public Actor { TimeZoneManager &operator=(TimeZoneManager &&) = delete; ~TimeZoneManager() final; + int32 get_time_zone_offset(const string &time_zone_id); + void get_time_zones(Promise> &&promise); void reload_time_zones(Promise> &&promise);