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