P
Peter Jansson
Dear group,
I have been struggling to get a simple program for inserting and
extracting std::tm objects to/from streams to work. The code below tries
to read a std::tm object from a std::istringstream but fails to do so,
could anybody see what is wrong with the code? (Output follows the code.)
I fear that I have not completely grasped how the time_getXXX methods
should be used in the operator<< ?
With best regards,
Peter Jansson
// ---- CODE BEGIN ---- //
#include <ctime>
#include <iostream>
#include <locale>
#include <sstream>
#include <string>
std::istream& operator>>(std::istream& s,struct std::tm& t);
std:stream& operator<<(std:stream& s,const struct std::tm& t);
int main()
{
struct std::tm t={0};
t.tm_year=2006-1900;
t.tm_mon=1-1;
t.tm_mday=14;
t.tm_hour=19;
t.tm_min=22;
t.tm_sec=0;
std::cout<<"Date-time at start: "<<t<<'\n';
std::string dateTimeString("1976-07-02 05:45:03");
std::cout<<"Date-time to read from a stream: "<<dateTimeString<<'\n';
std::istringstream iss(dateTimeString);
iss>>t;
std::cout<<"Date-time after reading from a stream: "<<t<<'\n';
std::cout<<"Should be: 1976-07-02 05:45:03\n";
return 0;
}
std::istream& operator>>(std::istream& s,struct std::tm& t)
{
using namespace std;
istream::sentry cerberos(s);
if(cerberos) {
ios_base::iostate err = ios::goodbit;
typedef istreambuf_iterator<char> ist;
ist from=ist(s),end=ist();
const time_get<char>& tg=use_facet< time_get<char> >(s.getloc());
tg.get_date(from,end,s,err,&t);
tg.get_time(from,end,s,err,&t);
// Or should something like the following 3 lines be used?
//tg.get_year(from,end,s,err,&t);
//tg.get_date(from,end,s,err,&t);
//tg.get_time(from,end,s,err,&t);
s.setstate(err);
}
return s;
}
std:stream& operator<<(std:stream& s,const struct std::tm& t)
{
using namespace std;
ostream::sentry cerberos(s);
if(cerberos) {
const char* fmt="%Y-%m-%d %H:%M:%S";
use_facet< time_put<char> >(s.getloc()).put(s,s,s.fill(),&t,fmt,fmt+17);
}
return s;
}
// ---- CODE BEGIN ---- //
// ---- SAMPLE OUTPUT BEGIN ---- //
Date-time at start: 2006-01-14 19:22:00
Date-time to read from a stream: 1976-07-02 05:45:03
Date-time after reading from a stream: 2006-01-14 19:22:00
Should be: 1976-07-02 05:45:03
// ---- SAMPLE OUTPUT END ---- //
I have been struggling to get a simple program for inserting and
extracting std::tm objects to/from streams to work. The code below tries
to read a std::tm object from a std::istringstream but fails to do so,
could anybody see what is wrong with the code? (Output follows the code.)
I fear that I have not completely grasped how the time_getXXX methods
should be used in the operator<< ?
With best regards,
Peter Jansson
// ---- CODE BEGIN ---- //
#include <ctime>
#include <iostream>
#include <locale>
#include <sstream>
#include <string>
std::istream& operator>>(std::istream& s,struct std::tm& t);
std:stream& operator<<(std:stream& s,const struct std::tm& t);
int main()
{
struct std::tm t={0};
t.tm_year=2006-1900;
t.tm_mon=1-1;
t.tm_mday=14;
t.tm_hour=19;
t.tm_min=22;
t.tm_sec=0;
std::cout<<"Date-time at start: "<<t<<'\n';
std::string dateTimeString("1976-07-02 05:45:03");
std::cout<<"Date-time to read from a stream: "<<dateTimeString<<'\n';
std::istringstream iss(dateTimeString);
iss>>t;
std::cout<<"Date-time after reading from a stream: "<<t<<'\n';
std::cout<<"Should be: 1976-07-02 05:45:03\n";
return 0;
}
std::istream& operator>>(std::istream& s,struct std::tm& t)
{
using namespace std;
istream::sentry cerberos(s);
if(cerberos) {
ios_base::iostate err = ios::goodbit;
typedef istreambuf_iterator<char> ist;
ist from=ist(s),end=ist();
const time_get<char>& tg=use_facet< time_get<char> >(s.getloc());
tg.get_date(from,end,s,err,&t);
tg.get_time(from,end,s,err,&t);
// Or should something like the following 3 lines be used?
//tg.get_year(from,end,s,err,&t);
//tg.get_date(from,end,s,err,&t);
//tg.get_time(from,end,s,err,&t);
s.setstate(err);
}
return s;
}
std:stream& operator<<(std:stream& s,const struct std::tm& t)
{
using namespace std;
ostream::sentry cerberos(s);
if(cerberos) {
const char* fmt="%Y-%m-%d %H:%M:%S";
use_facet< time_put<char> >(s.getloc()).put(s,s,s.fill(),&t,fmt,fmt+17);
}
return s;
}
// ---- CODE BEGIN ---- //
// ---- SAMPLE OUTPUT BEGIN ---- //
Date-time at start: 2006-01-14 19:22:00
Date-time to read from a stream: 1976-07-02 05:45:03
Date-time after reading from a stream: 2006-01-14 19:22:00
Should be: 1976-07-02 05:45:03
// ---- SAMPLE OUTPUT END ---- //