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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#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 <algorithm>
namespace td {
td_api::object_ptr<td_api::businessOpeningHoursInterval>
BusinessWorkHours::WorkHoursInterval::get_business_opening_hours_interval_object() const {
return td_api::make_object<td_api::businessOpeningHoursInterval>(start_minute_, end_minute_);
}
telegram_api::object_ptr<telegram_api::businessWeeklyOpen>
BusinessWorkHours::WorkHoursInterval::get_input_business_weekly_open() const {
return telegram_api::make_object<telegram_api::businessWeeklyOpen>(start_minute_, end_minute_);
}
BusinessWorkHours::BusinessWorkHours(telegram_api::object_ptr<telegram_api::businessWorkHours> &&work_hours) {
if (work_hours != nullptr) {
work_hours_ = transform(work_hours->weekly_open_,
[](const telegram_api::object_ptr<telegram_api::businessWeeklyOpen> &weekly_open) {
return WorkHoursInterval(weekly_open->start_minute_, weekly_open->end_minute_);
});
sanitize_work_hours();
time_zone_id_ = std::move(work_hours->timezone_id_);
}
}
BusinessWorkHours::BusinessWorkHours(td_api::object_ptr<td_api::businessOpeningHours> &&work_hours) {
if (work_hours != nullptr) {
work_hours_ = transform(work_hours->opening_hours_,
[](const td_api::object_ptr<td_api::businessOpeningHoursInterval> &interval) {
return WorkHoursInterval(interval->start_minute_, interval->end_minute_);
});
sanitize_work_hours();
time_zone_id_ = std::move(work_hours->time_zone_id_);
}
}
bool BusinessWorkHours::is_empty() const {
return work_hours_.empty();
}
td_api::object_ptr<td_api::businessOpeningHours> BusinessWorkHours::get_business_opening_hours_object() const {
if (is_empty()) {
return nullptr;
}
vector<td_api::object_ptr<td_api::businessOpeningHoursInterval>> intervals;
for (const auto &work_hour : work_hours_) {
auto interval = work_hour;
while (interval.start_minute_ / (24 * 60) + 1 < interval.end_minute_ / (24 * 60)) {
auto prefix = interval;
prefix.end_minute_ = (interval.start_minute_ / (24 * 60) + 1) * 24 * 60;
interval.start_minute_ = prefix.end_minute_;
intervals.push_back(prefix.get_business_opening_hours_interval_object());
}
intervals.push_back(interval.get_business_opening_hours_interval_object());
}
return td_api::make_object<td_api::businessOpeningHours>(time_zone_id_, std::move(intervals));
}
td_api::object_ptr<td_api::businessOpeningHours> BusinessWorkHours::get_local_business_opening_hours_object(
Td *td) const {
if (is_empty() || td->auth_manager_->is_bot()) {
return nullptr;
}
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();
}
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<telegram_api::businessWorkHours> BusinessWorkHours::get_input_business_work_hours() const {
if (is_empty()) {
return nullptr;
}
return telegram_api::make_object<telegram_api::businessWorkHours>(
0, false, time_zone_id_, transform(work_hours_, [](const WorkHoursInterval &interval) {
return interval.get_input_business_weekly_open();
}));
}
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) {
if (interval.start_minute_ >= interval.end_minute_ || interval.start_minute_ < 0 ||
interval.end_minute_ > 8 * 24 * 60) {
LOG(INFO) << "Ignore interval " << interval;
return true;
}
return false;
});
combine_work_hour_intervals();
}
void BusinessWorkHours::combine_work_hour_intervals() {
if (work_hours_.empty()) {
return;
}
// sort intervals
std::sort(work_hours_.begin(), work_hours_.end(), [](const WorkHoursInterval &lhs, const WorkHoursInterval &rhs) {
return lhs.start_minute_ < rhs.start_minute_;
});
// combine intersecting intervals
size_t j = 0;
for (size_t i = 1; i < work_hours_.size(); i++) {
CHECK(work_hours_[i].start_minute_ >= work_hours_[j].start_minute_);
if (work_hours_[i].start_minute_ <= work_hours_[j].end_minute_) {
work_hours_[j].end_minute_ = max(work_hours_[j].end_minute_, work_hours_[i].end_minute_);
} else {
work_hours_[++j] = work_hours_[i];
}
}
work_hours_.resize(j + 1);
// there must be no intervals longer than 1 week
for (auto &interval : work_hours_) {
interval.end_minute_ = min(interval.end_minute_, interval.start_minute_ + 7 * 24 * 60);
}
CHECK(!work_hours_.empty());
// if the last interval can be exactly merged with the first one, merge them
if (work_hours_[0].start_minute_ != 0 &&
work_hours_[0].start_minute_ + 7 * 24 * 60 == work_hours_.back().end_minute_) {
if (work_hours_.back().start_minute_ >= 7 * 24 * 60) {
work_hours_[0].start_minute_ = work_hours_.back().start_minute_ - 7 * 24 * 60;
work_hours_.pop_back();
CHECK(!work_hours_.empty());
} else {
work_hours_[0].start_minute_ = 0;
work_hours_.back().end_minute_ = 7 * 24 * 60;
}
}
// if there are intervals that intersect the first interval or start after the end of the week,
// then they must be normalized
auto max_minute = work_hours_[0].start_minute_ + 7 * 24 * 60;
if (work_hours_.back().end_minute_ > max_minute || work_hours_.back().start_minute_ >= 7 * 24 * 60) {
auto size = work_hours_.size();
for (size_t i = 0; i < size; i++) {
if (work_hours_[i].start_minute_ >= 7 * 24 * 60) {
work_hours_[i].start_minute_ -= 7 * 24 * 60;
work_hours_[i].end_minute_ -= 7 * 24 * 60;
} else if (work_hours_[i].end_minute_ > max_minute) {
work_hours_.emplace_back(max_minute - 7 * 24 * 60, work_hours_[i].end_minute_ - 7 * 24 * 60);
work_hours_[i].end_minute_ = max_minute;
}
}
LOG(INFO) << "Need to normalize " << work_hours_;
combine_work_hour_intervals();
}
}
bool operator==(const BusinessWorkHours::WorkHoursInterval &lhs, const BusinessWorkHours::WorkHoursInterval &rhs) {
return lhs.start_minute_ == rhs.start_minute_ && lhs.end_minute_ == rhs.end_minute_;
}
bool operator!=(const BusinessWorkHours::WorkHoursInterval &lhs, const BusinessWorkHours::WorkHoursInterval &rhs) {
return !(lhs == rhs);
}
StringBuilder &operator<<(StringBuilder &string_builder, const BusinessWorkHours::WorkHoursInterval &interval) {
return string_builder << '[' << interval.start_minute_ << ',' << interval.end_minute_ << ')';
}
bool operator==(const BusinessWorkHours &lhs, const BusinessWorkHours &rhs) {
return lhs.work_hours_ == rhs.work_hours_ && lhs.time_zone_id_ == rhs.time_zone_id_;
}
bool operator!=(const BusinessWorkHours &lhs, const BusinessWorkHours &rhs) {
return !(lhs == rhs);
}
StringBuilder &operator<<(StringBuilder &string_builder, const BusinessWorkHours &work_hours) {
return string_builder << "BusinessWorkHours[" << work_hours.work_hours_ << " in " << work_hours.time_zone_id_ << ']';
}
} // namespace td
|