Jeff Garland wrote:
Johan Nilsson wrote:
Hi,
is there any way to force ptime stream input fail when the time duration part is not between 00:00:00 and 23:59:59?
[snip]
Since these are treated as a time_duration for i/o there isn't a switch in the library to do this. I think the easiest way to do this would be to break down the streaming into it's parts and check in your client code. Here's the sketch if how it can be trivially done:
ptime getTime(istream& is) { //assuming formatting is already set... date d; time_duration td; is >> d; is >> td; if (td > hours(24)) { throw .... } return ptime(d, td); }
Hmmm, upon closer thought this will also pass for times such as "22:99:00". I'd need to check all parts, which feels a bit disturbing. I realize that a "time_duration" doesn't necessarily correspond to a valid time of day. How about implementing something like boost::posix_time::time_of_day, which would validate such things on input? At least for me it's a bit unintuitive that it's possible to sucessfully stream in a ptime when the time part isn't really a valid clock time. Regards, Johan