2015-09-29 19:21:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-17 22:53:46 +00:00
|
|
|
#include "DateLUTImpl.h"
|
2020-03-19 10:38:34 +00:00
|
|
|
|
|
|
|
#include "defines.h"
|
|
|
|
|
2019-08-22 03:24:05 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2015-09-29 19:21:02 +00:00
|
|
|
|
2020-03-19 10:38:34 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <unordered_map>
|
2017-06-23 20:22:35 +00:00
|
|
|
|
2017-01-22 09:27:51 +00:00
|
|
|
|
2016-11-13 19:34:31 +00:00
|
|
|
/// This class provides lazy initialization and lookup of singleton DateLUTImpl objects for a given timezone.
|
2019-08-22 03:24:05 +00:00
|
|
|
class DateLUT : private boost::noncopyable
|
2015-09-29 19:21:02 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Return singleton DateLUTImpl instance for the default time zone.
|
|
|
|
static ALWAYS_INLINE const DateLUTImpl & instance()
|
|
|
|
{
|
2019-08-22 03:24:05 +00:00
|
|
|
const auto & date_lut = getInstance();
|
2017-04-01 07:20:54 +00:00
|
|
|
return *date_lut.default_impl.load(std::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return singleton DateLUTImpl instance for a given time zone.
|
|
|
|
static ALWAYS_INLINE const DateLUTImpl & instance(const std::string & time_zone)
|
|
|
|
{
|
2019-08-22 03:24:05 +00:00
|
|
|
const auto & date_lut = getInstance();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (time_zone.empty())
|
|
|
|
return *date_lut.default_impl.load(std::memory_order_acquire);
|
|
|
|
|
|
|
|
return date_lut.getImplementation(time_zone);
|
|
|
|
}
|
|
|
|
static void setDefaultTimezone(const std::string & time_zone)
|
|
|
|
{
|
2019-08-22 03:24:05 +00:00
|
|
|
auto & date_lut = getInstance();
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & impl = date_lut.getImplementation(time_zone);
|
|
|
|
date_lut.default_impl.store(&impl, std::memory_order_release);
|
|
|
|
}
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
DateLUT();
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
private:
|
2019-08-22 03:24:05 +00:00
|
|
|
static DateLUT & getInstance();
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const DateLUTImpl & getImplementation(const std::string & time_zone) const;
|
2016-11-13 19:34:31 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using DateLUTImplPtr = std::unique_ptr<DateLUTImpl>;
|
2016-11-13 19:34:31 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Time zone name -> implementation.
|
|
|
|
mutable std::unordered_map<std::string, DateLUTImplPtr> impls;
|
|
|
|
mutable std::mutex mutex;
|
2016-11-13 19:34:31 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::atomic<const DateLUTImpl *> default_impl;
|
2015-09-29 19:21:02 +00:00
|
|
|
};
|