1 // Copyright 2021 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "testing/scoped_set_tz.h"
6
7 #include <stdlib.h>
8 #include <time.h>
9
10 #include "build/build_config.h"
11 #include "third_party/base/check_op.h"
12
13 namespace {
14
15 constexpr char kTZ[] = "TZ";
16
17 #if BUILDFLAG(IS_WIN)
18 #define SETENV(name, value) _putenv_s(name, value)
19 #define TZSET _tzset
20 #define UNSETENV(name) _putenv_s(name, "")
21 #else
22 #define SETENV(name, value) setenv(name, value, 1)
23 #define TZSET tzset
24 #define UNSETENV(name) unsetenv(name)
25 #endif
26
27 } // namespace
28
ScopedSetTZ(const std::string & tz)29 ScopedSetTZ::ScopedSetTZ(const std::string& tz) {
30 const char* old_tz = getenv(kTZ);
31 if (old_tz)
32 old_tz_ = old_tz;
33
34 CHECK_EQ(0, SETENV(kTZ, tz.c_str()));
35 TZSET();
36 }
37
~ScopedSetTZ()38 ScopedSetTZ::~ScopedSetTZ() {
39 if (old_tz_.has_value())
40 CHECK_EQ(0, SETENV(kTZ, old_tz_.value().c_str()));
41 else
42 CHECK_EQ(0, UNSETENV(kTZ));
43 TZSET();
44 }
45