put_time.pass.cpp revision f5256e16dfc425c1d466f6308d4026d529ce9e0b
1//===----------------------------------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10// <iomanip> 11 12// template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt); 13 14#include <iomanip> 15#include <cassert> 16 17template <class CharT> 18class testbuf 19 : public std::basic_streambuf<CharT> 20{ 21 typedef std::basic_streambuf<CharT> base; 22 std::basic_string<CharT> str_; 23public: 24 testbuf() 25 { 26 } 27 28 std::basic_string<CharT> str() const 29 {return std::basic_string<CharT>(base::pbase(), base::pptr());} 30 31protected: 32 33 virtual typename base::int_type 34 overflow(typename base::int_type __c = base::traits_type::eof()) 35 { 36 if (__c != base::traits_type::eof()) 37 { 38 int n = str_.size(); 39 str_.push_back(__c); 40 str_.resize(str_.capacity()); 41 base::setp(const_cast<CharT*>(str_.data()), 42 const_cast<CharT*>(str_.data() + str_.size())); 43 base::pbump(n+1); 44 } 45 return __c; 46 } 47}; 48 49int main() 50{ 51 { 52 testbuf<char> sb; 53 std::ostream os(&sb); 54 os.imbue(std::locale("en_US")); 55 std::tm t = {0}; 56 t.tm_sec = 59; 57 t.tm_min = 55; 58 t.tm_hour = 23; 59 t.tm_mday = 31; 60 t.tm_mon = 11; 61 t.tm_year = 161; 62 t.tm_wday = 6; 63 os << std::put_time(&t, "%c"); 64 assert(sb.str() == "Sat Dec 31 23:55:59 2061"); 65 } 66 { 67 testbuf<wchar_t> sb; 68 std::wostream os(&sb); 69 os.imbue(std::locale("en_US")); 70 std::tm t = {0}; 71 t.tm_sec = 59; 72 t.tm_min = 55; 73 t.tm_hour = 23; 74 t.tm_mday = 31; 75 t.tm_mon = 11; 76 t.tm_year = 161; 77 t.tm_wday = 6; 78 os << std::put_time(&t, L"%c"); 79 assert(sb.str() == L"Sat Dec 31 23:55:59 2061"); 80 } 81} 82