Gary Setter Guest
|
Posted: Tue May 24, 2005 1:18 pm Post subject: std::time_put<char >::put() gives me bad date |
|
|
Hi,
I'm trying to modify one of my programs to work with BCB5.5. The function
that creates a string with the current date is failing. I was using the
standard template library, Rogue Wave which came with the compiler. As an
experiment I tried the c runtime function strftime(), The strftime()
function worked.
This is the results, strftime() first, std::time_put<char >::put() second:
local date 05/24/05 date and time 05/24/05 07:56:33
local date 11/30/90 date and time 11/30/90 22:54:56
Most likely, I would just use strftime(). But I have to ask, am I using
std::time_put<char >::put() improperly? Could my code be improved?
Also, when macro _RWSTD_COMPILE_INSTANTIATE is defined, the put function is
defined in includerwtime.cc. However where is that function defined when
the above macro is not defined? I'm stumped. Does anyone here have an
answer?
The source follows.
Thanks,
Gary
#include <iostream>
#include <time.h>
#include <locale>
#include <sstream>
typedef std::time_put<char,std::ostreambuf_iterator timput;
const char *fmt ="local date %x date and time %c";
int main(int , char* )
{
time_t timer;
time(&timer);
tm *localTime = localtime(&timer);
{
char buf[80];
int cnt = strftime(buf,sizeof(buf),fmt,localTime);
if (cnt > 0)
std::cout << buf << 'n';
}
std::ostringstream os;
std::locale loc;
os.imbue(loc);
std::ostreambuf_iterator
const timput & tp =
std::use_facet<timput >(loc);
tp.put(it,os,' ',localTime,fmt,fmt + strlen(fmt));
std::cout
<< os.str().c_str()
<< 'n';
return 0;
}
|
|