Boost logo

Boost Users :

From: Michael Haubenwallner (michael.haubenwallner_at_[hidden])
Date: 2003-11-17 10:23:14


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.

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 ?

    Michael Haubenwallner

Michael Haubenwallner                    SALOMON Automation GmbH
Forschung & Entwicklung                  A-8114 Friesach bei Graz
T +43 3127 200 308                       F +43 3127 200 22

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at