 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Siegfried Heintze Guest
|
Posted: Sat Feb 11, 2006 12:06 pm Post subject: How to read/write date times? |
|
|
The following pair of functions work, except when I read the date/time
previously written by operator<<, it gets the year wrong. Perhaps it is
expecting a four digit year when it reads.
How can I modify my code below to expect a two digit year? How can I control
the format operator>> expects to find?
Thanks,
Siegfried
template <typename T> inline
std::basic_ostream<T>& operator<<(std::basic_ostream<T>& os, tm const&
tmbuf)
{
typename std::basic_ostream<T>::sentry cerberus(os);
if (!bool(cerberus)) return os;
const char fmt[] = "%x %X";
std::time_put<T> const& timeFacet = std::use_facet< std::time_put<T>
| Quote: | ( os.getloc() );
if (timeFacet.put(os, os, os.fill(), |
&tmbuf,fmt,fmt+sizeof(fmt)-1).failed())
//if (timeFacet.put(os, os, os.fill(), &tmbuf,'x').failed())
os.setstate(os.badbit);
return os;
}
template <typename T> inline
std::basic_istream<T>& operator>>(std::basic_istream<T>& s,struct std::tm&
t)
{
using namespace std;
typename std::basic_istream<T>::sentry cerberos(s);
if(cerberos) {
ios_base::iostate err = ios::goodbit;
const time_get<T>& tg=use_facet< time_get<T> >(s.getloc());
typedef istreambuf_iterator<T> ist;
ist from(s),end;
ist last=tg.get_date(from,end,s,err,&t);
++from;
tg.get_time(from,end,s,err,&t);
s.setstate(err);
}
return s;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sat Feb 11, 2006 6:06 pm Post subject: Re: How to read/write date times? |
|
|
In article <43ed6ac7$1@mamba.>, Siegfried Heintze
<siegfried (AT) heintze (DOT) com> wrote:
| Quote: | The following pair of functions work, except when I read the date/time
previously written by operator<<, it gets the year wrong. Perhaps it is
expecting a four digit year when it reads.
How can I modify my code below to expect a two digit year? How can I control
the format operator>> expects to find?
[snip]
typedef istreambuf_iterator<T> ist;
ist from(s),end;
ist last=tg.get_date(from,end,s,err,&t);
++from;
tg.get_time(from,end,s,err,&t);
s.setstate(err);
}
return s;
}
perhaps it should be tg.get_time(last,end,s,err,&t); |
not
++from ;
tg.get_time(from,end,...);
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|