aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cctz/tzdata/factory.cpp
blob: 5a4b4a27b676e95782f23d9ee1f7c733f42ad2ca (plain) (blame)
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
#include <contrib/libs/cctz/include/cctz/zone_info_source.h>

#include <library/cpp/resource/resource.h>

#include <util/generic/string.h>
#include <util/stream/str.h>
#include <util/string/builder.h>

namespace cctz_extension {
    namespace {

        class TZoneInfoSource: public cctz::ZoneInfoSource {
        private:
            TZoneInfoSource(TString&& data)
                : TimeZoneData_(data)
                , Stream_(TimeZoneData_)
            {
            }

        public:
            static std::unique_ptr<cctz::ZoneInfoSource> LoadZone(const std::string& zoneName) {
                TString resourceName = TStringBuilder() << "/cctz/tzdata/"sv << zoneName;
                TString tzData;
                if (!NResource::FindExact(resourceName, &tzData)) {
                    return nullptr;
                }
                return std::unique_ptr<cctz::ZoneInfoSource>(new TZoneInfoSource(std::move(tzData)));
            }

            size_t Read(void* buf, size_t size) override {
                return Stream_.Read(buf, size);
            }

            int Skip(size_t offset) override {
                Stream_.Skip(offset);
                return 0;
            }

        private:
            const TString TimeZoneData_;
            TStringInput Stream_;
        };

        std::unique_ptr<cctz::ZoneInfoSource> CustomFactory(
            const std::string& name,
            const std::function<std::unique_ptr<cctz::ZoneInfoSource>(const std::string& name)>& /*fallback*/
        )
        {
            std::unique_ptr<cctz::ZoneInfoSource> zis = TZoneInfoSource::LoadZone(name);
            if (zis) {
                return zis;
            }
            return nullptr;
        }

    } // namespace

    ZoneInfoSourceFactory zone_info_source_factory = CustomFactory;

} // namespace cctz_extension