summaryrefslogtreecommitdiff
path: root/src/timezone.cc
blob: ca0f5012e6ab80785d836d0bcc6b067ea038ce69 (plain)
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
#include "common.hh"

#include "geo_json.hh"
#include "timezone.hh"
#include "tz_info.hh"

namespace {

class TimezoneImpl : public Timezone {
public:
  TimezoneImpl(std::shared_ptr<Logger> logger,
               std::filesystem::path geojsondb,
               std::filesystem::path tzinfo_dir)
    : geojson_(GeoJson::create(logger, std::move(geojsondb))),
      tzinfo_(TzInfo::create(logger, std::move(tzinfo_dir))) {
  }

  std::optional<time_t> get_local_time(double lat, double lng,
                                       time_t utc_time) const override {
    auto tzid = geojson_->get_data(lat, lng, "tzid");
    if (tzid.has_value())
      return tzinfo_->get_local_time(tzid.value(), utc_time);
    return std::nullopt;
  }

private:
  std::unique_ptr<GeoJson> geojson_;
  std::unique_ptr<TzInfo> tzinfo_;
};

}  // namespace

std::unique_ptr<Timezone> Timezone::create(std::shared_ptr<Logger> logger,
                                           std::filesystem::path geojsondb,
                                           std::filesystem::path tzinfo_dir) {
  return std::make_unique<TimezoneImpl>(std::move(logger), std::move(geojsondb),
                                        std::move(tzinfo_dir));
}