aboutsummaryrefslogtreecommitdiffstats
path: root/src/querykv1/schedule.cpp
diff options
context:
space:
mode:
authorLibravatar Rutger Broekhoff2024-05-02 20:27:40 +0200
committerLibravatar Rutger Broekhoff2024-05-02 20:27:40 +0200
commit17a3ea880402338420699e03bcb24181e4ff3924 (patch)
treeda666ef91e0b60d20aa0b01529644c136fd1f4ab /src/querykv1/schedule.cpp
downloadoeuf-17a3ea880402338420699e03bcb24181e4ff3924.tar.gz
oeuf-17a3ea880402338420699e03bcb24181e4ff3924.zip
Initial commit
Based on dc4ba6a
Diffstat (limited to 'src/querykv1/schedule.cpp')
-rw-r--r--src/querykv1/schedule.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/querykv1/schedule.cpp b/src/querykv1/schedule.cpp
new file mode 100644
index 0000000..2bcfe0a
--- /dev/null
+++ b/src/querykv1/schedule.cpp
@@ -0,0 +1,63 @@
1// vim:set sw=2 ts=2 sts et:
2
3#include <iostream>
4#include <string_view>
5#include <unordered_map>
6#include <vector>
7
8#include "daterange.hpp"
9#include "schedule.hpp"
10
11using namespace std::string_view_literals;
12
13void schedule(const Options &options, Kv1Records &records, Kv1Index &index) {
14 FILE *out = stdout;
15 if (options.output_file_path != "-"sv)
16 out = fopen(options.output_file_path, "wb");
17 if (!out) {
18 fprintf(stderr, "Open %s: %s\n", options.output_file_path, strerrordesc_np(errno));
19 exit(EXIT_FAILURE);
20 }
21
22 std::cerr << "Generating schedule for " << options.line_planning_number << std::endl;
23
24 std::unordered_multimap<std::string, Kv1PeriodGroupValidity> period_group_validities;
25 for (const auto &pegr : records.period_group_validities)
26 period_group_validities.insert({ pegr.key.period_group_code, pegr });
27 std::unordered_multimap<std::string, Kv1PublicJourney> public_journeys;
28 for (const auto &pujo : records.public_journeys)
29 public_journeys.insert({ pujo.key.timetable_version_code, pujo });
30
31 std::cout << "line_planning_number,journey_number,date,departure_time" << std::endl;
32 for (const auto &tive : records.timetable_versions) {
33 std::vector<DateRange> tive_pegrval_ranges;
34
35 auto pegrval_range = period_group_validities.equal_range(tive.key.period_group_code);
36 for (auto it = pegrval_range.first; it != pegrval_range.second; it++) {
37 const auto &[_, pegrval] = *it;
38 tive_pegrval_ranges.emplace_back(pegrval.key.valid_from, pegrval.valid_thru);
39 }
40
41 DateRangeSeq seq(tive_pegrval_ranges.begin(), tive_pegrval_ranges.end());
42 seq = seq.clampFrom(tive.valid_from);
43 if (tive.valid_thru)
44 seq = seq.clampThru(*tive.valid_thru);
45
46 for (const auto &range : seq) for (auto date : range) {
47 auto weekday = std::chrono::year_month_weekday(std::chrono::sys_days(date)).weekday();
48
49 auto pujo_range = public_journeys.equal_range(tive.key.timetable_version_code);
50 for (auto itt = pujo_range.first; itt != pujo_range.second; itt++) {
51 const auto &[_, pujo] = *itt;
52
53 if (pujo.key.line_planning_number == options.line_planning_number && pujo.key.day_type.size() == 7
54 && pujo.key.day_type[weekday.iso_encoding() - 1] == static_cast<char>('0' + weekday.iso_encoding())) {
55 std::cout << pujo.key.line_planning_number << "," << pujo.key.journey_number << ","
56 << date << "," << pujo.departure_time << std::endl;
57 }
58 }
59 }
60 }
61
62 if (options.output_file_path != "-"sv) fclose(out);
63}