Coverage Report

Created: 2024-09-08 06:32

/src/cctz/src/time_zone_impl.cc
Line
Count
Source (jump to first uncovered line)
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
15
#include "time_zone_impl.h"
16
17
#include <deque>
18
#include <memory>
19
#include <mutex>
20
#include <string>
21
#include <unordered_map>
22
#include <utility>
23
24
#include "time_zone_fixed.h"
25
26
namespace cctz {
27
28
namespace {
29
30
// time_zone::Impls are linked into a map to support fast lookup by name.
31
using TimeZoneImplByName =
32
    std::unordered_map<std::string, const time_zone::Impl*>;
33
TimeZoneImplByName* time_zone_map = nullptr;
34
35
// Mutual exclusion for time_zone_map.
36
7.72k
std::mutex& TimeZoneMutex() {
37
  // This mutex is intentionally "leaked" to avoid the static deinitialization
38
  // order fiasco (std::mutex's destructor is not trivial on many platforms).
39
7.72k
  static std::mutex* time_zone_mutex = new std::mutex;
40
7.72k
  return *time_zone_mutex;
41
7.72k
}
42
43
}  // namespace
44
45
148
time_zone time_zone::Impl::UTC() {
46
148
  return time_zone(UTCImpl());
47
148
}
48
49
7.03k
bool time_zone::Impl::LoadTimeZone(const std::string& name, time_zone* tz) {
50
7.03k
  const Impl* const utc_impl = UTCImpl();
51
52
  // Check for UTC (which is never a key in time_zone_map).
53
7.03k
  auto offset = seconds::zero();
54
7.03k
  if (FixedOffsetFromName(name, &offset) && offset == seconds::zero()) {
55
243
    *tz = time_zone(utc_impl);
56
243
    return true;
57
243
  }
58
59
  // Check whether the time zone has already been loaded.
60
6.79k
  {
61
6.79k
    std::lock_guard<std::mutex> lock(TimeZoneMutex());
62
6.79k
    if (time_zone_map != nullptr) {
63
6.78k
      TimeZoneImplByName::const_iterator itr = time_zone_map->find(name);
64
6.78k
      if (itr != time_zone_map->end()) {
65
5.85k
        *tz = time_zone(itr->second);
66
5.85k
        return itr->second != utc_impl;
67
5.85k
      }
68
6.78k
    }
69
6.79k
  }
70
71
  // Load the new time zone (outside the lock).
72
931
  std::unique_ptr<const Impl> new_impl(new Impl(name));
73
74
  // Add the new time zone to the map.
75
931
  std::lock_guard<std::mutex> lock(TimeZoneMutex());
76
931
  if (time_zone_map == nullptr) time_zone_map = new TimeZoneImplByName;
77
931
  const Impl*& impl = (*time_zone_map)[name];
78
931
  if (impl == nullptr) {  // this thread won any load race
79
931
    impl = new_impl->zone_ ? new_impl.release() : utc_impl;
80
931
  }
81
931
  *tz = time_zone(impl);
82
931
  return impl != utc_impl;
83
6.79k
}
84
85
0
void time_zone::Impl::ClearTimeZoneMapTestOnly() {
86
0
  std::lock_guard<std::mutex> lock(TimeZoneMutex());
87
0
  if (time_zone_map != nullptr) {
88
    // Existing time_zone::Impl* entries are in the wild, so we can't delete
89
    // them. Instead, we move them to a private container, where they are
90
    // logically unreachable but not "leaked".  Future requests will result
91
    // in reloading the data.
92
0
    static auto* cleared = new std::deque<const time_zone::Impl*>;
93
0
    for (const auto& element : *time_zone_map) {
94
0
      cleared->push_back(element.second);
95
0
    }
96
0
    time_zone_map->clear();
97
0
  }
98
0
}
99
100
1
time_zone::Impl::Impl() : name_("UTC"), zone_(TimeZoneIf::UTC()) {}
101
102
time_zone::Impl::Impl(const std::string& name)
103
931
    : name_(name), zone_(TimeZoneIf::Make(name_)) {}
104
105
7.18k
const time_zone::Impl* time_zone::Impl::UTCImpl() {
106
7.18k
  static const Impl* utc_impl = new Impl;
107
7.18k
  return utc_impl;
108
7.18k
}
109
110
}  // namespace cctz