|
Boost Users : |
From: Jeff Garland (jeff_at_[hidden])
Date: 2003-11-17 12:10:12
On Mon, 17 Nov 2003 16:23:14 +0100, Michael Haubenwallner wrote
> Hi,
>
> I'm preparing my company's step from C to C++, and when i use
> boost::date_time::*, there's a great danger to make mistakes
> like this with boost-1.30.2:
>
> #include <time.h>
>
> #include <iostream>
> #include <boost/date_time/posix_time/posix_time.hpp>
>
> using namespace boost::posix_time;
> using namespace boost::gregorian;
>
> int main()
> {
> time_t now = time(0);
>
> try {
> ptime time(now); // mistake
>
> cerr << to_simple_string(time) << endl;
> } catch (exception const& e) {
> cerr << "error: " << e.what() << endl;
> }
> }
>
> The problem is that the time_t in this case is treated as a day-count
> from which a gregorian::date() is implicitly constructed.
It would be better to convert things like:
ptime time(second_clock::universal_time());
In the next release you can convert from time_t if you insist like this:
time_t now_t = time(0);
ptime now = from_time_t(now_t);
> When digging through the gregorian::date, i see that the
> constructors of date_time::date() from date_int_type and
> date_rep_type protected.
>
> So, wouldn't it be better to make the gregorian::date() constructors
> from date_int_type and date_rep_type explicit, to get a compile-time
> error with this sample rather than the out-of-range-exception
> "Year is out of valid range: 1400..10000" ?
>
> And another reason to make this constructor explicit might be that
> date_rep_type is the internal representation of date(), so when
> constructing date() with a day_count, you have to know the epoch,
> and then constructing date() should be done explicitly...
>
> What do you think about this ?
I'll look into it. On first blush this would probably be reasonable.
Jeff
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net