From 779a90d4381f95f44779a43bbb7db11307417a2b Mon Sep 17 00:00:00 2001 From: Alexey Arno Date: Fri, 26 Jun 2015 20:57:49 +0300 Subject: [PATCH] dbms: Server: Support for multiple time zones: take into account the existence of equivalent time zones [#METR-15618] --- libs/libcommon/src/DateLUT.cpp | 44 ++++++++++++++++++++++++------ libs/libcommon/src/DateLUTImpl.cpp | 7 +++-- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/libs/libcommon/src/DateLUT.cpp b/libs/libcommon/src/DateLUT.cpp index d45c8503815..e992d8969cf 100644 --- a/libs/libcommon/src/DateLUT.cpp +++ b/libs/libcommon/src/DateLUT.cpp @@ -1,8 +1,9 @@ #include #include -#include + #include #include +#include std::string DateLUT::default_time_zone; @@ -27,13 +28,35 @@ DateLUT::DateLUT() if (zone_id == nullptr) throw Poco::Exception("No time zone available."); + std::vector time_zones; 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)); + time_zones.push_back(*zone_id); zone_id = time_zone_ids->snext(status); } + + for (const auto & time_zone : time_zones) + { + auto count = TimeZone::countEquivalentIDs(time_zone); + + const UnicodeString & u_group_id = TimeZone::getEquivalentID(time_zone, 0); + std::string group_id; + u_group_id.toUTF8String(group_id); + + auto it = time_zone_to_group.find(group_id); + if (it == time_zone_to_group.end()) + { + for (auto i = 1; i < count; ++i) + { + const UnicodeString & u_equivalent_id = TimeZone::getEquivalentID(time_zone, i); + std::string equivalent_id; + u_equivalent_id.toUTF8String(equivalent_id); + time_zone_to_group.insert(std::make_pair(equivalent_id, group_id)); + } + } + + date_lut_impl_list.emplace(std::piecewise_construct, std::forward_as_tuple(group_id), std::forward_as_tuple(nullptr)); + } } DateLUTImpl & DateLUT::instance(const std::string & time_zone) @@ -44,16 +67,21 @@ DateLUTImpl & DateLUT::instance(const std::string & time_zone) DateLUTImpl & DateLUT::get(const std::string & time_zone) { - auto it = date_lut_impl_list.find(time_zone); - if (it == date_lut_impl_list.end()) + auto it = time_zone_to_group.find(time_zone); + if (it == time_zone_to_group.end()) throw Poco::Exception("Invalid time zone " + time_zone); + const auto & group_id = it->second; - auto & wrapper = it->second; + auto it2 = date_lut_impl_list.find(group_id); + if (it2 == date_lut_impl_list.end()) + throw Poco::Exception("Invalid group of equivalent time zones."); + + auto & wrapper = it2->second; DateLUTImpl * tmp = wrapper.load(std::memory_order_acquire); if (tmp == nullptr) { - std::lock_guard guard(mux); + std::lock_guard guard(mutex); tmp = wrapper.load(std::memory_order_acquire); if (tmp == nullptr) { diff --git a/libs/libcommon/src/DateLUTImpl.cpp b/libs/libcommon/src/DateLUTImpl.cpp index 33e4e07b48f..1045845dd7b 100644 --- a/libs/libcommon/src/DateLUTImpl.cpp +++ b/libs/libcommon/src/DateLUTImpl.cpp @@ -1,8 +1,9 @@ -#include #include #include -#include + #include +#include +#include namespace details { namespace { @@ -121,7 +122,7 @@ DateLUTImpl::DateLUTImpl(const std::string & time_zone) while (start_of_day <= DATE_LUT_MAX); /// Заполняем lookup таблицу для годов - memset(years_lut, 0, DATE_LUT_YEARS * sizeof(years_lut[0])); + ::memset(years_lut, 0, DATE_LUT_YEARS * sizeof(years_lut[0])); for (size_t day = 0; day < i && lut[day].year <= DATE_LUT_MAX_YEAR; ++day) { if (lut[day].month == 1 && lut[day].day_of_month == 1)