Add time to the next open and close for business.

This commit is contained in:
levlam 2024-04-30 20:12:39 +03:00
parent 7682cf6e61
commit c42bf62038
4 changed files with 41 additions and 8 deletions

View File

@ -651,8 +651,8 @@ businessStartPage title:string message:string sticker:sticker = BusinessStartPag
inputBusinessStartPage title:string message:string sticker:InputFile = InputBusinessStartPage;
//@description Describes an interval of time when the business is open
//@start_minute The first minute of the interval since start of the week; 0-7*24*60
//@end_minute The first minute after the end of the interval since start of the week; 1-8*24*60
//@start_minute The minute's sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0-7*24*60
//@end_minute The minute's sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 1-8*24*60
businessOpeningHoursInterval start_minute:int32 end_minute:int32 = BusinessOpeningHoursInterval;
//@description Describes opening hours of a business @time_zone_id Unique time zone identifier @opening_hours Intervals of the time when the business is open
@ -661,11 +661,14 @@ businessOpeningHours time_zone_id:string opening_hours:vector<businessOpeningHou
//@description Contains information about a Telegram Business account
//@location Location of the business; may be null if none
//@opening_hours Opening hours of the business; may be null if none. The hours are guaranteed to be valid and has already been split by week days
//@local_opening_hours Opening hours of the business in the local time; may be null if none. The hours are guaranteed to be valid and has already been split by week days. Local time zone identifier will be empty
//@local_opening_hours Opening hours of the business in the local time; may be null if none. The hours are guaranteed to be valid and has already been split by week days.
//-Local time zone identifier will be empty. An updateUserFullInfo update is not triggered when value of this field changes
//@next_open_in Time left before the business will open the next time, in seconds; 0 if unknown. An updateUserFullInfo update is not triggered when value of this field changes
//@next_close_in Time left before the business will close the next time, in seconds; 0 if unknown. An updateUserFullInfo update is not triggered when value of this field changes
//@greeting_message_settings The greeting message; may be null if none or the Business account is not of the current user
//@away_message_settings The away message; may be null if none or the Business account is not of the current user
//@start_page Information about start page of the account; may be null if none
businessInfo location:businessLocation opening_hours:businessOpeningHours local_opening_hours:businessOpeningHours greeting_message_settings:businessGreetingMessageSettings away_message_settings:businessAwayMessageSettings start_page:businessStartPage = BusinessInfo;
businessInfo location:businessLocation opening_hours:businessOpeningHours local_opening_hours:businessOpeningHours next_open_in:int32 next_close_in:int32 greeting_message_settings:businessGreetingMessageSettings away_message_settings:businessAwayMessageSettings start_page:businessStartPage = BusinessInfo;
//@description Contains information about a business chat link

View File

@ -7,6 +7,7 @@
#include "td/telegram/BusinessInfo.h"
#include "td/telegram/Dependencies.h"
#include "td/telegram/Global.h"
namespace td {
@ -14,9 +15,11 @@ td_api::object_ptr<td_api::businessInfo> BusinessInfo::get_business_info_object(
if (is_empty()) {
return nullptr;
}
auto unix_time = G()->unix_time();
return td_api::make_object<td_api::businessInfo>(
location_.get_business_location_object(), work_hours_.get_business_opening_hours_object(),
work_hours_.get_local_business_opening_hours_object(td),
work_hours_.get_local_business_opening_hours_object(td), work_hours_.get_next_open_close_in(td, unix_time, false),
work_hours_.get_next_open_close_in(td, unix_time, true),
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));
}

View File

@ -80,9 +80,9 @@ td_api::object_ptr<td_api::businessOpeningHours> BusinessWorkHours::get_local_bu
return nullptr;
}
auto offset = narrow_cast<int32>((td->time_zone_manager_->get_time_zone_offset(time_zone_id_) -
td->option_manager_->get_option_integer("utc_time_offset")) /
60);
auto offset = (td->time_zone_manager_->get_time_zone_offset(time_zone_id_) -
narrow_cast<int32>(td->option_manager_->get_option_integer("utc_time_offset"))) /
60;
if (offset == 0) {
return get_business_opening_hours_object();
}
@ -124,6 +124,31 @@ telegram_api::object_ptr<telegram_api::businessWorkHours> BusinessWorkHours::get
}));
}
int32 BusinessWorkHours::get_next_open_close_in(Td *td, int32 unix_time, bool is_close) const {
if (is_empty()) {
return 0;
}
auto get_week_time = [](int32 time) {
const auto week_length = 7 * 86400;
return ((time % week_length) + week_length) % week_length;
};
// the Unix time 0 was on a Thursday, the first Monday was at 4 * 86400
auto current_week_time = get_week_time(unix_time - 4 * 86400);
auto offset = td->time_zone_manager_->get_time_zone_offset(time_zone_id_);
int32 result = 1000000000;
for (auto &interval : work_hours_) {
auto change_week_time = get_week_time((is_close ? interval.end_minute_ : interval.start_minute_) * 60 - offset);
auto wait_time = change_week_time - current_week_time;
if (wait_time < 0) {
wait_time += 7 * 86400;
}
if (wait_time < result) {
result = wait_time;
}
}
return result;
}
void BusinessWorkHours::sanitize_work_hours() {
// remove invalid work hour intervals
td::remove_if(work_hours_, [](const WorkHoursInterval &interval) {

View File

@ -32,6 +32,8 @@ class BusinessWorkHours {
telegram_api::object_ptr<telegram_api::businessWorkHours> get_input_business_work_hours() const;
int32 get_next_open_close_in(Td *td, int32 unix_time, bool is_close) const;
template <class StorerT>
void store(StorerT &storer) const;