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