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// http://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
15#include "time_zone_impl.h"
16
17#include <mutex>
18#include <string>
19#include <unordered_map>
20#include <utility>
21
22#include "time_zone_fixed.h"
23
24namespace cctz {
25
26namespace {
27
28// time_zone::Impls are linked into a map to support fast lookup by name.
29using TimeZoneImplByName =
30 std::unordered_map<std::string, const time_zone::Impl*>;
31TimeZoneImplByName* time_zone_map = nullptr;
32
33// Mutual exclusion for time_zone_map.
34std::mutex time_zone_mutex;
35
36} // namespace
37
38time_zone time_zone::Impl::UTC() {
39 return time_zone(UTCImpl());
40}
41
42bool time_zone::Impl::LoadTimeZone(const std::string& name, time_zone* tz) {
43 const time_zone::Impl* const utc_impl = UTCImpl();
44
45 // First check for UTC (which is never a key in time_zone_map).
46 auto offset = sys_seconds::zero();
47 if (FixedOffsetFromName(name, &offset) && offset == sys_seconds::zero()) {
48 *tz = time_zone(utc_impl);
49 return true;
50 }
51
52 // Then check, under a shared lock, whether the time zone has already
53 // been loaded. This is the common path. TODO: Move to shared_mutex.
54 {
55 std::lock_guard<std::mutex> lock(time_zone_mutex);
56 if (time_zone_map != nullptr) {
57 TimeZoneImplByName::const_iterator itr = time_zone_map->find(name);
58 if (itr != time_zone_map->end()) {
59 *tz = time_zone(itr->second);
60 return itr->second != utc_impl;
61 }
62 }
63 }
64
65 // Now check again, under an exclusive lock.
66 std::lock_guard<std::mutex> lock(time_zone_mutex);
67 if (time_zone_map == nullptr) time_zone_map = new TimeZoneImplByName;
68 const Impl*& impl = (*time_zone_map)[name];
69 if (impl == nullptr) {
70 // The first thread in loads the new time zone.
71 Impl* new_impl = new Impl(name);
72 new_impl->zone_ = TimeZoneIf::Load(new_impl->name_);
73 if (new_impl->zone_ == nullptr) {
74 delete new_impl; // free the nascent Impl
75 impl = utc_impl; // and fallback to UTC
76 } else {
77 impl = new_impl; // install new time zone
78 }
79 }
80 *tz = time_zone(impl);
81 return impl != utc_impl;
82}
83
84const time_zone::Impl& time_zone::Impl::get(const time_zone& tz) {
85 if (tz.impl_ == nullptr) {
86 // Dereferencing an implicit-UTC time_zone is expected to be
87 // rare, so we don't mind paying a small synchronization cost.
88 return *UTCImpl();
89 }
90 return *tz.impl_;
91}
92
93void time_zone::Impl::ClearTimeZoneMapTestOnly() {
94 std::lock_guard<std::mutex> lock(time_zone_mutex);
95 if (time_zone_map != nullptr) {
96 // Existing time_zone::Impl* entries are in the wild, so we simply
97 // leak them. Future requests will result in reloading the data.
98 time_zone_map->clear();
99 }
100}
101
102time_zone::Impl::Impl(const std::string& name) : name_(name) {}
103
104const time_zone::Impl* time_zone::Impl::UTCImpl() {
105 static Impl* utc_impl = [] {
106 Impl* impl = new Impl("UTC");
107 impl->zone_ = TimeZoneIf::Load(impl->name_); // never fails
108 return impl;
109 }();
110 return utc_impl;
111}
112
113} // namespace cctz
114