xref: /llvm-project/libcxx/test/std/input.output/iostream.format/ext.manip/put_time.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
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 "platform_support.h" // locale name macros
20 
21 template <class CharT>
22 class testbuf
23     : public std::basic_streambuf<CharT>
24 {
25     typedef std::basic_streambuf<CharT> base;
26     std::basic_string<CharT> str_;
27 public:
28     testbuf()
29     {
30     }
31 
32     std::basic_string<CharT> str() const
33         {return std::basic_string<CharT>(base::pbase(), base::pptr());}
34 
35 protected:
36 
37     virtual typename base::int_type
38         overflow(typename base::int_type ch = base::traits_type::eof())
39         {
40             if (ch != base::traits_type::eof())
41             {
42                 int n = static_cast<int>(str_.size());
43                 str_.push_back(static_cast<CharT>(ch));
44                 str_.resize(str_.capacity());
45                 base::setp(const_cast<CharT*>(str_.data()),
46                            const_cast<CharT*>(str_.data() + str_.size()));
47                 base::pbump(n+1);
48             }
49             return ch;
50         }
51 };
52 
53 int main()
54 {
55     {
56         testbuf<char> sb;
57         std::ostream os(&sb);
58         os.imbue(std::locale(LOCALE_en_US_UTF_8));
59         std::tm t = {};
60         t.tm_sec = 59;
61         t.tm_min = 55;
62         t.tm_hour = 23;
63         t.tm_mday = 31;
64         t.tm_mon = 11;
65         t.tm_year = 161;
66         t.tm_wday = 6;
67         t.tm_isdst = 0;
68         os << std::put_time(&t, "%a %b %d %H:%M:%S %Y");
69         assert(sb.str() == "Sat Dec 31 23:55:59 2061");
70     }
71     {
72         testbuf<wchar_t> sb;
73         std::wostream os(&sb);
74         os.imbue(std::locale(LOCALE_en_US_UTF_8));
75         std::tm t = {};
76         t.tm_sec = 59;
77         t.tm_min = 55;
78         t.tm_hour = 23;
79         t.tm_mday = 31;
80         t.tm_mon = 11;
81         t.tm_year = 161;
82         t.tm_wday = 6;
83         os << std::put_time(&t, L"%a %b %d %H:%M:%S %Y");
84         assert(sb.str() == L"Sat Dec 31 23:55:59 2061");
85     }
86 }
87