factory.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <contrib/libs/cctz/include/cctz/zone_info_source.h>
  2. #include <library/cpp/resource/resource.h>
  3. #include <util/generic/string.h>
  4. #include <util/stream/str.h>
  5. #include <util/string/builder.h>
  6. namespace cctz_extension {
  7. namespace {
  8. class TZoneInfoSource: public cctz::ZoneInfoSource {
  9. private:
  10. TZoneInfoSource(TString&& data)
  11. : TimeZoneData_(data)
  12. , Stream_(TimeZoneData_)
  13. {
  14. }
  15. public:
  16. static std::unique_ptr<cctz::ZoneInfoSource> LoadZone(const std::string& zoneName) {
  17. TString resourceName = TStringBuilder() << "/cctz/tzdata/"sv << zoneName;
  18. TString tzData;
  19. if (!NResource::FindExact(resourceName, &tzData)) {
  20. return nullptr;
  21. }
  22. return std::unique_ptr<cctz::ZoneInfoSource>(new TZoneInfoSource(std::move(tzData)));
  23. }
  24. size_t Read(void* buf, size_t size) override {
  25. return Stream_.Read(buf, size);
  26. }
  27. int Skip(size_t offset) override {
  28. Stream_.Skip(offset);
  29. return 0;
  30. }
  31. private:
  32. const TString TimeZoneData_;
  33. TStringInput Stream_;
  34. };
  35. std::unique_ptr<cctz::ZoneInfoSource> CustomFactory(
  36. const std::string& name,
  37. const std::function<std::unique_ptr<cctz::ZoneInfoSource>(const std::string& name)>& /*fallback*/
  38. )
  39. {
  40. std::unique_ptr<cctz::ZoneInfoSource> zis = TZoneInfoSource::LoadZone(name);
  41. if (zis) {
  42. return zis;
  43. }
  44. return nullptr;
  45. }
  46. } // namespace
  47. ZoneInfoSourceFactory zone_info_source_factory = CustomFactory;
  48. } // namespace cctz_extension