zone_info_source.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef CCTZ_ZONE_INFO_SOURCE_H_
  15. #define CCTZ_ZONE_INFO_SOURCE_H_
  16. #include <cstddef>
  17. #include <functional>
  18. #include <memory>
  19. #include <string>
  20. namespace cctz {
  21. // A stdio-like interface for providing zoneinfo data for a particular zone.
  22. class ZoneInfoSource {
  23. public:
  24. virtual ~ZoneInfoSource();
  25. virtual std::size_t Read(void* ptr, std::size_t size) = 0; // like fread()
  26. virtual int Skip(std::size_t offset) = 0; // like fseek()
  27. // Until the zoneinfo data supports versioning information, we provide
  28. // a way for a ZoneInfoSource to indicate it out-of-band. The default
  29. // implementation returns an empty string.
  30. virtual std::string Version() const;
  31. };
  32. } // namespace cctz
  33. namespace cctz_extension {
  34. // A function-pointer type for a factory that returns a ZoneInfoSource
  35. // given the name of a time zone and a fallback factory. Returns null
  36. // when the data for the named zone cannot be found.
  37. using ZoneInfoSourceFactory =
  38. std::unique_ptr<cctz::ZoneInfoSource> (*)(
  39. const std::string&,
  40. const std::function<std::unique_ptr<cctz::ZoneInfoSource>(
  41. const std::string&)>&);
  42. // The user can control the mapping of zone names to zoneinfo data by
  43. // providing a definition for cctz_extension::zone_info_source_factory.
  44. // For example, given functions my_factory() and my_other_factory() that
  45. // can return a ZoneInfoSource for a named zone, we could inject them into
  46. // cctz::load_time_zone() with:
  47. //
  48. // namespace cctz_extension {
  49. // namespace {
  50. // std::unique_ptr<cctz::ZoneInfoSource> CustomFactory(
  51. // const std::string& name,
  52. // const std::function<std::unique_ptr<cctz::ZoneInfoSource>(
  53. // const std::string& name)>& fallback_factory) {
  54. // if (auto zip = my_factory(name)) return zip;
  55. // if (auto zip = fallback_factory(name)) return zip;
  56. // if (auto zip = my_other_factory(name)) return zip;
  57. // return nullptr;
  58. // }
  59. // } // namespace
  60. // ZoneInfoSourceFactory zone_info_source_factory = CustomFactory;
  61. // } // namespace cctz_extension
  62. //
  63. // This might be used, say, to use zoneinfo data embedded in the program,
  64. // or read from a (possibly compressed) file archive, or both.
  65. //
  66. // cctz_extension::zone_info_source_factory() will be called:
  67. // (1) from the same thread as the cctz::load_time_zone() call,
  68. // (2) only once for any zone name, and
  69. // (3) serially (i.e., no concurrent execution).
  70. //
  71. // The fallback factory obtains zoneinfo data by reading files in ${TZDIR},
  72. // and it is used automatically when no zone_info_source_factory definition
  73. // is linked into the program.
  74. extern ZoneInfoSourceFactory zone_info_source_factory;
  75. } // namespace cctz_extension
  76. #endif // CCTZ_ZONE_INFO_SOURCE_H_