Debian Codegraph @

libcxx/test/std/time/time.zone/time.zone.db/time.zone.db.access/current_zone.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-filesystem, no-localization, no-tzdb
11
12 // XFAIL: libcpp-has-no-experimental-tzdb
13 // XFAIL: availability-tzdb-missing
14
15 // <chrono>
16
17 // const time_zone* current_zone();
18
19 #include <cassert>
20 #include <chrono>
21 #include <string_view>
22 #include <stdlib.h>
23
24 #include "test_macros.h"
25 #include "assert_macros.h"
26 #include "concat_macros.h"
27
28 #ifdef _WIN32
29 static void set_tz(std::string zone) {
30 // Note Windows does not have setenv, only putenv
31 // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-s-wputenv-s?view=msvc-170
32 // Unlike POSIX it does not mention the string of putenv becomes part
33 // of the environment.
34
35 int status = _putenv_s("TZ", zone.c_str());
36 assert(status == 0);
37 }
38
39 #else
40 static void set_tz(const std::string& zone) {
41 int status = setenv("TZ", zone.c_str(), 1);
42 assert(status == 0);
43 }
44 #endif
45
46 static void test_zone(const std::string& zone) {
47 set_tz(zone);
48 const std::chrono::time_zone* tz = std::chrono::current_zone();
49 assert(tz);
50 assert(tz->name() == zone);
51 }
52
53 static void test_link(const std::string& link, std::string_view zone) {
54 set_tz(link);
55 const std::chrono::time_zone* tz = std::chrono::current_zone();
56 assert(tz);
57 assert(tz->name() == zone);
58 }
59
60 int main(int, const char**) {
61 const std::chrono::time_zone* tz = std::chrono::current_zone();
62 // Returns a valid time zone, the value depends on the OS settings.
63 assert(tz);
64 // setting the environment to an invalid value returns the value of
65 // the OS setting.
66 set_tz("This is not a time zone");
67 assert(tz == std::chrono::current_zone());
68
69 const std::chrono::tzdb& db = std::chrono::get_tzdb();
70 for (const auto& zone : db.zones)
71 test_zone(std::string{zone.name()});
72
73 for (const auto& link : db.links)
74 test_link(std::string{link.name()}, link.target());
75
76 return 0;
77 }