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> T9 get_time(struct tm* tmb, const charT* fmt); 13 14 #include <iomanip> 15 #include <cassert> 16 17 #include "platform_support.h" // locale name macros 18 19 template <class CharT> 20 struct testbuf 21 : public std::basic_streambuf<CharT> 22 { 23 typedef std::basic_string<CharT> string_type; 24 typedef std::basic_streambuf<CharT> base; 25 private: 26 string_type str_; 27 public: 28 29 testbuf() {} 30 testbuf(const string_type& str) 31 : str_(str) 32 { 33 base::setg(const_cast<CharT*>(str_.data()), 34 const_cast<CharT*>(str_.data()), 35 const_cast<CharT*>(str_.data()) + str_.size()); 36 } 37 }; 38 39 int main() 40 { 41 { 42 testbuf<char> sb(" Sat Dec 31 23:55:59 2061"); 43 std::istream is(&sb); 44 is.imbue(std::locale(LOCALE_en_US_UTF_8)); 45 std::tm t = {0}; 46 is >> std::get_time(&t, "%c"); 47 assert(t.tm_sec == 59); 48 assert(t.tm_min == 55); 49 assert(t.tm_hour == 23); 50 assert(t.tm_mday == 31); 51 assert(t.tm_mon == 11); 52 assert(t.tm_year == 161); 53 assert(t.tm_wday == 6); 54 assert(is.eof()); 55 assert(!is.fail()); 56 } 57 #if !defined(__ANDROID__) 58 // Remove tests setlocale() to other than "", "C", and "POSIX" 59 // for Android 60 { 61 testbuf<wchar_t> sb(L" Sat Dec 31 23:55:59 2061"); 62 std::wistream is(&sb); 63 is.imbue(std::locale(LOCALE_en_US_UTF_8)); 64 std::tm t = {0}; 65 is >> std::get_time(&t, L"%c"); 66 assert(t.tm_sec == 59); 67 assert(t.tm_min == 55); 68 assert(t.tm_hour == 23); 69 assert(t.tm_mday == 31); 70 assert(t.tm_mon == 11); 71 assert(t.tm_year == 161); 72 assert(t.tm_wday == 6); 73 assert(is.eof()); 74 assert(!is.fail()); 75 } 76 #endif 77 } 78