Boost logo

Boost :

From: Joachim Faulhaber (afojgo_at_[hidden])
Date: 2008-05-22 16:56:33


as suggested by Paul Bristow in his response to my proposal on
interval containers I started to integrate boost::date_time into my
library examples.

To my surprise I discovered that boost::date_time does not provide
operator ++ (--) for it's date and time template classes. So the boost
date and time classes are not Incrementable (Decrementable). One could
say: they lack a tic-tac ;-) Which is for *time* kind of a major

Now, Incrementability seems most fundamental to me. Therefore, in the
design of interval containers in my library, I avoided to reqire
Addability for interval template parameters. Requiring only
In(De)crementability I enable a broader set of instances for intervals
and interval containers.

Iterators for instance provide incrementation and decrementation but
no addability in general (some do).

Incrementability seems also more fundamental to me than Addability.
Which is to say that a concept or algebra that offers Addability
almost certainly also has a Incrementability, but not the other way
round. Take for instance the Peano Axioms of natural numbers, where
Incrementability is given in the form of the fundamental successor

Therefore I would strongly suggest, that a class template that
implements Addability/Subtractability (operators +, +=, -, -=) also
implements In(De)crementability (operators ++ and --).

This should apply even more to classes that resemble in their basic
characteristics an integral numeric type like date and time do.

For boost::date_time I tried to completed the code in this respect. I
added 6 lines of code:

counted_time_rep& operator ++(){++time_count_; return *this;}
counted_time_rep& operator --(){--time_count_; return *this;}

int_adapter& operator ++() {++value_; return *this;}
int_adapter& operator --() {--value_; return *this;}

time_type operator ++() { ++time_; return time_type(time_); }
time_type operator --() { --time_; return time_type(time_); }

With these minor additions to the code the 'party' example from my
interval template library works fine with boost::date_time:

#include <stdio.h>
#include <iostream>
#include <itl/itl_value.hpp>
#include <itl/string_set.hpp>
#include <itl/split_interval_map.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace std;
using namespace boost::posix_time;
using namespace itl;

typedef itl::set<string> GuestSetT;
typedef split_interval_map<ptime, GuestSetT>

void boost_party()
  GuestSetT mary_harry;

  GuestSetT diana_susan;

  GuestSetT peter;

  PartyAttendenceHistoryT party;

        time_from_string("2008-05-20 19:30:00.000"),
        time_from_string("2008-05-20 23:00:00.000")),
        mary_harry) );
        time_from_string("2008-05-20 20:10:00.000"),
        time_from_string("2008-05-21 00:00:00.000")),
        diana_susan) );
        time_from_string("2008-05-20 22:15:00.000"),
        time_from_string("2008-05-21 00:30:00.000")),
        peter) );

  PartyAttendenceHistoryT::iterator it = party.begin();
  while(it != party.end())
      interval<ptime> when = (*it).first;
      // Who is at the party within the time interval 'when' ?
      GuestSetT who = (*it++).second;
      cout << "[" << to_simple_string(when.first())
        << " - " << to_simple_string(when.last()) << "]"
        << ": " << who.as_string() << endl;

int main()
  cout << ">> Interval Template Library: Sample boost_party.cpp <<\n";
  cout << "-------------------------------------------------------\n";
  return 0;

Program output:

>> Interval Template Library: Sample boost_party.cpp <<
[2008-May-20 19:30:00 - 2008-May-20 20:09:59.999999]: Harry Mary
[2008-May-20 20:10:00 - 2008-May-20 22:14:59.999999]: Diana Harry Mary Susan
[2008-May-20 22:15:00 - 2008-May-20 22:59:59.999999]: Diana Harry Mary Peter

[2008-May-20 23:00:00 - 2008-May-20 23:59:59.999999]: Diana Peter Susan
[2008-May-21 00:00:00 - 2008-May-21 00:29:59.999999]: Peter


Interval Template Library (ITL)
download from

Boost list run by bdawes at, gregod at, cpdaniel at, john at