Date/Time formatting using std::time_get::get() in VC++ 2010
I was wondering if anybody could help me with this one.
I am a little confused with the following code snippet. I am basically
using it to generate a tm structure out of a stringified date/time
input which looks like mm/dd/yy hh:mm (or dd/mm/yy hh:mm if the locale
is UK).
std::time_get::get() (which seems to be non-standard but available in
VC++ 2010) however takes in a format specifier that has to mention a
particular format at compile time. That seems to be incompatible with
the constructed locale object which fills itself with the appropriate
locale information based on the user's regional settings at runtime.
How do I ensure that the format specifier is in tune with the locale?
For example, std::time_get::get_date() works correctly if you enter
14/12/12 in the UK and fails expectedly when you run the same in the
US for this input. and unlike get(), get_date() does not ask for a
format specifier. I am trying to use time_get::get() because I don't
want to break my string into tokens and call get_date() and get_time()
individually.
(docs for time_get::get() is here:
http://msdn.microsoft.com/en-us/library/ee428007.aspx)
std::locale loc("");
std::stringstream date;
date.imbue(loc);
date << "14-12-11 12:22";
std::istreambuf_iterator<char> begin(date.rdbuf());
std::istreambuf_iterator<char> end;
// this example works only because the format specifier is right.
// Then how does the locale object help here?
std::string format_spec = "%d-%m-%y %H:%M";
std::ios_base::iostate st = std::ios::goodbit;
struct tm t = { 0 };
std::use_facet<std::time_get<char>>(loc).get(begin, end, date, st,
&t, format_spec.c_str(),
format_spec.c_str() +
format_spec.length());
if (st & std::ios::failbit)
{
std::cout << "Wrong input!!";
}
else
{
cout << "month = " << t.tm_mon + 1 << "; day = " << t.tm_mday <<
"; year = "
<< t.tm_year + 1900 << std::endl;
cout << "hour = " << t.tm_hour << "; min = " << t.tm_min << "; sec
= "
<< t.tm_sec << std::endl;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]