| 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 | // REQUIRES: locale.en_US.UTF-8 |
| 10 | |
| 11 | // <iomanip> |
| 12 | |
| 13 | // template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt); |
| 14 | |
| 15 | #include <iomanip> |
| 16 | #include <ostream> |
| 17 | #include <cassert> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | #include "platform_support.h" // locale name macros |
| 21 | |
| 22 | template <class CharT> |
| 23 | class testbuf |
| 24 | : public std::basic_streambuf<CharT> |
| 25 | { |
| 26 | typedef std::basic_streambuf<CharT> base; |
| 27 | std::basic_string<CharT> str_; |
| 28 | public: |
| 29 | testbuf() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | std::basic_string<CharT> str() const |
| 34 | {return std::basic_string<CharT>(base::pbase(), base::pptr());} |
| 35 | |
| 36 | protected: |
| 37 | |
| 38 | virtual typename base::int_type |
| 39 | overflow(typename base::int_type ch = base::traits_type::eof()) |
| 40 | { |
| 41 | if (ch != base::traits_type::eof()) |
| 42 | { |
| 43 | int n = static_cast<int>(str_.size()); |
| 44 | str_.push_back(static_cast<CharT>(ch)); |
| 45 | str_.resize(str_.capacity()); |
| 46 | base::setp(const_cast<CharT*>(str_.data()), |
| 47 | const_cast<CharT*>(str_.data() + str_.size())); |
| 48 | base::pbump(n+1); |
| 49 | } |
| 50 | return ch; |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | int main(int, char**) |
| 55 | { |
| 56 | { |
| 57 | testbuf<char> sb; |
| 58 | std::ostream os(&sb); |
| 59 | os.imbue(std::locale(LOCALE_en_US_UTF_8)); |
| 60 | std::tm t = {}; |
| 61 | t.tm_sec = 59; |
| 62 | t.tm_min = 55; |
| 63 | t.tm_hour = 23; |
| 64 | t.tm_mday = 31; |
| 65 | t.tm_mon = 11; |
| 66 | t.tm_year = 161; |
| 67 | t.tm_wday = 6; |
| 68 | t.tm_isdst = 0; |
| 69 | os << std::put_time(tmb: &t, fmt: "%a %b %d %H:%M:%S %Y" ); |
| 70 | assert(sb.str() == "Sat Dec 31 23:55:59 2061" ); |
| 71 | } |
| 72 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 73 | { |
| 74 | testbuf<wchar_t> sb; |
| 75 | std::wostream os(&sb); |
| 76 | os.imbue(std::locale(LOCALE_en_US_UTF_8)); |
| 77 | std::tm t = {}; |
| 78 | t.tm_sec = 59; |
| 79 | t.tm_min = 55; |
| 80 | t.tm_hour = 23; |
| 81 | t.tm_mday = 31; |
| 82 | t.tm_mon = 11; |
| 83 | t.tm_year = 161; |
| 84 | t.tm_wday = 6; |
| 85 | os << std::put_time(tmb: &t, fmt: L"%a %b %d %H:%M:%S %Y" ); |
| 86 | assert(sb.str() == L"Sat Dec 31 23:55:59 2061" ); |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |