2009-05-28 18:19:51 +00:00
|
|
|
#include <Yandex/DateLUT.h>
|
2014-07-08 23:52:53 +00:00
|
|
|
#include <Poco/Exception.h>
|
2015-06-26 15:11:31 +00:00
|
|
|
#include <Yandex/likely.h>
|
|
|
|
#include <unicode/timezone.h>
|
|
|
|
#include <unicode/unistr.h>
|
2009-05-28 18:19:51 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
std::string DateLUT::default_time_zone;
|
2009-05-28 18:19:51 +00:00
|
|
|
|
2013-08-11 03:40:14 +00:00
|
|
|
DateLUT::DateLUT()
|
2009-05-28 18:19:51 +00:00
|
|
|
{
|
2015-06-26 15:11:31 +00:00
|
|
|
using namespace icu;
|
2014-11-06 02:39:48 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
std::unique_ptr<TimeZone> tz(TimeZone::createDefault());
|
|
|
|
if (tz == nullptr)
|
|
|
|
throw Poco::Exception("Failed to query the host time zone.");
|
2014-11-06 02:39:48 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
UnicodeString u_out;
|
|
|
|
tz->getID(u_out);
|
|
|
|
u_out.toUTF8String(default_time_zone);
|
2014-11-06 02:39:48 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
std::unique_ptr<StringEnumeration> time_zone_ids(TimeZone::createEnumeration());
|
|
|
|
if (time_zone_ids == nullptr)
|
|
|
|
throw Poco::Exception("Failed to query the list of time zones.");
|
2013-08-11 03:40:14 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
const UnicodeString * zone_id = time_zone_ids->snext(status);
|
|
|
|
if (zone_id == nullptr)
|
|
|
|
throw Poco::Exception("No time zone available.");
|
2013-08-11 03:40:14 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
while ((zone_id != nullptr) && (status == U_ZERO_ERROR))
|
|
|
|
{
|
|
|
|
std::string zone_id_str;
|
|
|
|
zone_id->toUTF8String(zone_id_str);
|
|
|
|
date_lut_impl_list.emplace(std::piecewise_construct, std::forward_as_tuple(zone_id_str), std::forward_as_tuple(nullptr));
|
|
|
|
zone_id = time_zone_ids->snext(status);
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 03:40:14 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
DateLUTImpl & DateLUT::instance(const std::string & time_zone)
|
|
|
|
{
|
|
|
|
auto & date_lut = Singleton<DateLUT>::instance();
|
|
|
|
return date_lut.get(time_zone);
|
|
|
|
}
|
2013-08-11 03:40:14 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
DateLUTImpl & DateLUT::get(const std::string & time_zone)
|
|
|
|
{
|
|
|
|
auto it = date_lut_impl_list.find(time_zone);
|
|
|
|
if (it == date_lut_impl_list.end())
|
|
|
|
throw Poco::Exception("Invalid time zone " + time_zone);
|
2013-08-11 03:40:14 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
auto & wrapper = it->second;
|
2013-08-11 03:40:14 +00:00
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
DateLUTImpl * tmp = wrapper.load(std::memory_order_acquire);
|
|
|
|
if (tmp == nullptr)
|
2013-08-11 03:40:14 +00:00
|
|
|
{
|
2015-06-26 15:11:31 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mux);
|
|
|
|
tmp = wrapper.load(std::memory_order_acquire);
|
|
|
|
if (tmp == nullptr)
|
|
|
|
{
|
|
|
|
tmp = new DateLUTImpl(time_zone);
|
|
|
|
wrapper.store(tmp, std::memory_order_release);
|
|
|
|
}
|
2013-08-11 03:40:14 +00:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:11:31 +00:00
|
|
|
return *tmp;
|
2009-05-28 18:19:51 +00:00
|
|
|
}
|
2015-06-26 15:11:31 +00:00
|
|
|
|