Definition at line 25 of file time_facets_test.cpp. Referenced by time_put_get(). {
{
typedef time_put<char, ostreambuf_iterator<char, char_traits<char> > > time_put_facet;
CPPUNIT_ASSERT( has_facet<time_put_facet>(loc) );
const time_put_facet& tmp = use_facet<time_put_facet>(loc);
struct tm xmas = { 0, 0, 12, 25, 11, 93 };
ostringstream ostr;
ostr.imbue(loc);
string format = "%B %d %Y";
time_put_facet::iter_type ret = tmp.put(ostr, ostr, ' ', &xmas, format.data(), format.data() + format.size());
CPPUNIT_ASSERT( !ret.failed() );
/*
* In other words, user conformation is required for reliable parsing
* of user-entered dates and times, but machine-generated formats can be
* parsed reliably. This allows parsers to be aggressive about interpreting
* user variations on standard format.
*
* ISO/IEC 14882, 22.2.5.1
*/
typedef time_get<char, istreambuf_iterator<char, char_traits<char> > > time_get_facet;
CPPUNIT_ASSERT( has_facet<time_get_facet>(loc) );
const time_get_facet& tmg = use_facet<time_get_facet>(loc);
basic_ios<char> io(0);
io.imbue(loc);
istringstream istr( ostr.str() );
istreambuf_iterator<char, char_traits<char> > i( istr );
istreambuf_iterator<char, char_traits<char> > e;
ios_base::iostate err = ios_base::goodbit;
struct tm other = { 15, 20, 9, 14, 7, 105 };
i = tmg.get_monthname( i, e, io, err, &other );
CPPUNIT_ASSERT( err == ios_base::goodbit );
CPPUNIT_ASSERT( other.tm_mon == xmas.tm_mon );
++i; ++i; ++i; ++i; // skip day of month and spaces around it
i = tmg.get_year( i, e, io, err, &other );
CPPUNIT_ASSERT( err == ios_base::eofbit );
CPPUNIT_ASSERT( other.tm_year == xmas.tm_year );
ostringstream ostrX;
ostrX.imbue(loc);
format = "%x %X";
ret = tmp.put(ostrX, ostrX, ' ', &xmas, format.data(), format.data() + format.size());
CPPUNIT_ASSERT( !ret.failed() );
istringstream istrX( ostrX.str() );
istreambuf_iterator<char, char_traits<char> > j( istrX );
err = ios_base::goodbit;
struct tm yet_more = { 15, 20, 9, 14, 7, 105 };
j = tmg.get_date( j, e, io, err, &yet_more );
CPPUNIT_ASSERT( err == ios_base::goodbit );
CPPUNIT_ASSERT( yet_more.tm_sec != xmas.tm_sec );
CPPUNIT_ASSERT( yet_more.tm_min != xmas.tm_min );
CPPUNIT_ASSERT( yet_more.tm_hour != xmas.tm_hour );
CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday );
CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon );
CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year );
++j; // skip space
j = tmg.get_time( j, e, io, err, &yet_more );
CPPUNIT_ASSERT( err == ios_base::eofbit || err == ios_base::goodbit );
CPPUNIT_ASSERT( yet_more.tm_sec == xmas.tm_sec );
CPPUNIT_ASSERT( yet_more.tm_min == xmas.tm_min );
CPPUNIT_ASSERT( yet_more.tm_hour == xmas.tm_hour );
CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday );
CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon );
CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year );
}
# if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T)
{
typedef time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_put_facet;
CPPUNIT_ASSERT( has_facet<time_put_facet>(loc) );
const time_put_facet& tmp = use_facet<time_put_facet>(loc);
struct tm xmas = { 0, 0, 12, 25, 11, 93 };
wostringstream ostr;
ostr.imbue(loc);
wstring format = L"%B %d %Y";
time_put_facet::iter_type ret = tmp.put(ostr, ostr, ' ', &xmas, format.data(), format.data() + format.size());
CPPUNIT_ASSERT( !ret.failed() );
/*
* In other words, user conformation is required for reliable parsing
* of user-entered dates and times, but machine-generated formats can be
* parsed reliably. This allows parsers to be aggressive about interpreting
* user variations on standard format.
*
* ISO/IEC 14882, 22.2.5.1
*/
typedef time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_get_facet;
CPPUNIT_ASSERT( has_facet<time_get_facet>(loc) );
const time_get_facet& tmg = use_facet<time_get_facet>(loc);
// Intentional instantiation with char to show a bug in a previous STLport version.
basic_ios<char> io(0);
io.imbue(loc);
wistringstream istr( ostr.str() );
istreambuf_iterator<wchar_t, char_traits<wchar_t> > i( istr );
istreambuf_iterator<wchar_t, char_traits<wchar_t> > e;
ios_base::iostate err = ios_base::goodbit;
struct tm other = { 15, 20, 9, 14, 7, 105 };
i = tmg.get_monthname( i, e, io, err, &other );
CPPUNIT_ASSERT( err == ios_base::goodbit );
CPPUNIT_ASSERT( other.tm_mon == xmas.tm_mon );
++i; ++i; ++i; ++i; // skip day of month and spaces around it
i = tmg.get_year( i, e, io, err, &other );
CPPUNIT_ASSERT( err == ios_base::eofbit );
CPPUNIT_ASSERT( other.tm_year == xmas.tm_year );
wostringstream ostrX;
ostrX.imbue(loc);
format = L"%x %X";
ret = tmp.put(ostrX, ostrX, ' ', &xmas, format.data(), format.data() + format.size());
CPPUNIT_ASSERT( !ret.failed() );
wistringstream istrX( ostrX.str() );
istreambuf_iterator<wchar_t, char_traits<wchar_t> > j( istrX );
err = ios_base::goodbit;
struct tm yet_more = { 15, 20, 9, 14, 7, 105 };
j = tmg.get_date( j, e, io, err, &yet_more );
CPPUNIT_ASSERT( err == ios_base::goodbit );
CPPUNIT_ASSERT( yet_more.tm_sec != xmas.tm_sec );
CPPUNIT_ASSERT( yet_more.tm_min != xmas.tm_min );
CPPUNIT_ASSERT( yet_more.tm_hour != xmas.tm_hour );
CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday );
CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon );
CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year );
++j; // skip space
j = tmg.get_time( j, e, io, err, &yet_more );
CPPUNIT_ASSERT( err == ios_base::eofbit || err == ios_base::goodbit );
CPPUNIT_ASSERT( yet_more.tm_sec == xmas.tm_sec );
CPPUNIT_ASSERT( yet_more.tm_min == xmas.tm_min );
CPPUNIT_ASSERT( yet_more.tm_hour == xmas.tm_hour );
CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday );
CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon );
CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year );
}
# endif
}
|