| 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 <chrono> |
| 20 | #include <cassert> |
| 21 | #include <stdlib.h> |
| 22 | #include <string_view> |
| 23 | #include <string> |
| 24 | |
| 25 | #include "test_macros.h" |
| 26 | #include "assert_macros.h" |
| 27 | #include "concat_macros.h" |
| 28 | |
| 29 | #ifdef _WIN32 |
| 30 | static void set_tz(std::string zone) { |
| 31 | // Note Windows does not have setenv, only putenv |
| 32 | // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-s-wputenv-s?view=msvc-170 |
| 33 | // Unlike POSIX it does not mention the string of putenv becomes part |
| 34 | // of the environment. |
| 35 | |
| 36 | int status = _putenv_s("TZ" , zone.c_str()); |
| 37 | assert(status == 0); |
| 38 | } |
| 39 | |
| 40 | #else |
| 41 | static void set_tz(const std::string& zone) { |
| 42 | int status = setenv(name: "TZ" , value: zone.c_str(), replace: 1); |
| 43 | assert(status == 0); |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | static void test_zone(const std::string& zone) { |
| 48 | set_tz(zone); |
| 49 | const std::chrono::time_zone* tz = std::chrono::current_zone(); |
| 50 | assert(tz); |
| 51 | assert(tz->name() == zone); |
| 52 | } |
| 53 | |
| 54 | static void test_link(const std::string& link, std::string_view zone) { |
| 55 | set_tz(link); |
| 56 | const std::chrono::time_zone* tz = std::chrono::current_zone(); |
| 57 | assert(tz); |
| 58 | assert(tz->name() == zone); |
| 59 | } |
| 60 | |
| 61 | int main(int, const char**) { |
| 62 | const std::chrono::time_zone* tz = std::chrono::current_zone(); |
| 63 | // Returns a valid time zone, the value depends on the OS settings. |
| 64 | assert(tz); |
| 65 | // setting the environment to an invalid value returns the value of |
| 66 | // the OS setting. |
| 67 | set_tz("This is not a time zone" ); |
| 68 | assert(tz == std::chrono::current_zone()); |
| 69 | |
| 70 | const std::chrono::tzdb& db = std::chrono::get_tzdb(); |
| 71 | for (const auto& zone : db.zones) |
| 72 | test_zone(std::string{zone.name()}); |
| 73 | |
| 74 | for (const auto& link : db.links) |
| 75 | test_link(std::string{link.name()}, link.target()); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |