xref: /llvm-project/libcxx/test/std/input.output/iostream.format/ext.manip/put_time.pass.cpp (revision f4c1258d5633fcf06385ff3fd1f4bf57ab971964)
15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
961c115f3SDaniel Sanders // REQUIRES: locale.en_US.UTF-8
1061c115f3SDaniel Sanders 
115a83710eSEric Fiselier // <iomanip>
125a83710eSEric Fiselier 
135a83710eSEric Fiselier // template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
145a83710eSEric Fiselier 
155a83710eSEric Fiselier #include <iomanip>
165a127cdcSMarshall Clow #include <ostream>
175a83710eSEric Fiselier #include <cassert>
185a83710eSEric Fiselier 
197fc6a556SMarshall Clow #include "test_macros.h"
205a83710eSEric Fiselier #include "platform_support.h" // locale name macros
215a83710eSEric Fiselier 
225a83710eSEric Fiselier template <class CharT>
235a83710eSEric Fiselier class testbuf
245a83710eSEric Fiselier     : public std::basic_streambuf<CharT>
255a83710eSEric Fiselier {
265a83710eSEric Fiselier     typedef std::basic_streambuf<CharT> base;
275a83710eSEric Fiselier     std::basic_string<CharT> str_;
285a83710eSEric Fiselier public:
testbuf()295a83710eSEric Fiselier     testbuf()
305a83710eSEric Fiselier     {
315a83710eSEric Fiselier     }
325a83710eSEric Fiselier 
str() const335a83710eSEric Fiselier     std::basic_string<CharT> str() const
345a83710eSEric Fiselier         {return std::basic_string<CharT>(base::pbase(), base::pptr());}
355a83710eSEric Fiselier 
365a83710eSEric Fiselier protected:
375a83710eSEric Fiselier 
385a83710eSEric Fiselier     virtual typename base::int_type
overflow(typename base::int_type ch=base::traits_type::eof ())3955467c46SStephan T. Lavavej         overflow(typename base::int_type ch = base::traits_type::eof())
405a83710eSEric Fiselier         {
4155467c46SStephan T. Lavavej             if (ch != base::traits_type::eof())
425a83710eSEric Fiselier             {
43baa547b9SStephan T. Lavavej                 int n = static_cast<int>(str_.size());
4455467c46SStephan T. Lavavej                 str_.push_back(static_cast<CharT>(ch));
455a83710eSEric Fiselier                 str_.resize(str_.capacity());
465a83710eSEric Fiselier                 base::setp(const_cast<CharT*>(str_.data()),
475a83710eSEric Fiselier                            const_cast<CharT*>(str_.data() + str_.size()));
485a83710eSEric Fiselier                 base::pbump(n+1);
495a83710eSEric Fiselier             }
5055467c46SStephan T. Lavavej             return ch;
515a83710eSEric Fiselier         }
525a83710eSEric Fiselier };
535a83710eSEric Fiselier 
main(int,char **)542df59c50SJF Bastien int main(int, char**)
555a83710eSEric Fiselier {
565a83710eSEric Fiselier     {
575a83710eSEric Fiselier         testbuf<char> sb;
585a83710eSEric Fiselier         std::ostream os(&sb);
595a83710eSEric Fiselier         os.imbue(std::locale(LOCALE_en_US_UTF_8));
603245e1f3SEric Fiselier         std::tm t = {};
615a83710eSEric Fiselier         t.tm_sec = 59;
625a83710eSEric Fiselier         t.tm_min = 55;
635a83710eSEric Fiselier         t.tm_hour = 23;
645a83710eSEric Fiselier         t.tm_mday = 31;
655a83710eSEric Fiselier         t.tm_mon = 11;
665a83710eSEric Fiselier         t.tm_year = 161;
675a83710eSEric Fiselier         t.tm_wday = 6;
685a83710eSEric Fiselier         t.tm_isdst = 0;
695a83710eSEric Fiselier         os << std::put_time(&t, "%a %b %d %H:%M:%S %Y");
705a83710eSEric Fiselier         assert(sb.str() == "Sat Dec 31 23:55:59 2061");
715a83710eSEric Fiselier     }
72*f4c1258dSLouis Dionne #ifndef TEST_HAS_NO_WIDE_CHARACTERS
735a83710eSEric Fiselier     {
745a83710eSEric Fiselier         testbuf<wchar_t> sb;
755a83710eSEric Fiselier         std::wostream os(&sb);
765a83710eSEric Fiselier         os.imbue(std::locale(LOCALE_en_US_UTF_8));
773245e1f3SEric Fiselier         std::tm t = {};
785a83710eSEric Fiselier         t.tm_sec = 59;
795a83710eSEric Fiselier         t.tm_min = 55;
805a83710eSEric Fiselier         t.tm_hour = 23;
815a83710eSEric Fiselier         t.tm_mday = 31;
825a83710eSEric Fiselier         t.tm_mon = 11;
835a83710eSEric Fiselier         t.tm_year = 161;
845a83710eSEric Fiselier         t.tm_wday = 6;
855a83710eSEric Fiselier         os << std::put_time(&t, L"%a %b %d %H:%M:%S %Y");
865a83710eSEric Fiselier         assert(sb.str() == L"Sat Dec 31 23:55:59 2061");
875a83710eSEric Fiselier     }
88*f4c1258dSLouis Dionne #endif
892df59c50SJF Bastien 
902df59c50SJF Bastien   return 0;
915a83710eSEric Fiselier }
92