Boost logo

Boost Users :

From: Jean-Pierre Bergamin (james_at_[hidden])
Date: 2008-04-02 03:51:30


Hello everyone

While implementing a job scheduler that has to start jobs at specific times
(i.e. daily at 08:15) and during specific time ranges (every Monday between
16:20 and 18:00), the need for two new concepts that represents a time
(detached from a date) and a time range between two time_of_day came up.
While time_duration almost fits these needs, there are some things that we
had to take into account:

- A time_of_day must be normalized to only allow hours >= 0 && hours < 24
- Only the substract operator makes sense to calculate the duration between
to times
  (15:30 - 12:00 = 3 hours and 30 minutes). 12:20 + 15:32 does not make
sense.

The implicit conversion to time_duration also allows the usage of
time_of_day to create ptime instances (which is IMHO more "understandable"
then using time_duration):
ptime some_time(date(2008, 4, 1), time_of_day(14, 20, 0));

The implemented classes time_of_day (derived from time_duration) and
time_range are very lightweight and allow the user to do things like:

BOOST_AUTO_TEST_CASE( time_of_day_test2 )
{
        time_of_day t1(15,0,0);
        time_of_day t2(40,0,0); // normalized to 16:00:00
        BOOST_MESSAGE("t1 " << t1); // t1 15:00:00
        BOOST_MESSAGE("t2 " << t2); // t2 16:00:00

        t2 += hours(3) + minutes(30);

        BOOST_MESSAGE("t1 " << t1); // t1 15:00:00
        BOOST_MESSAGE("t2 " << t2); // t2 19:30:00

        time_duration d1(t2 - t1); // Duration from 15:00 to 19:30 = 4:30
hour
        time_duration d2(t1 - t2); // Duration from 19:30 to 15:00 = 19:30
hours

        BOOST_MESSAGE("d1 " << d1); // d1 04:30:00
        BOOST_MESSAGE("d2 " << d2); // d2 19:30:00

        time_range afternoon(t1, t2); // From 15:00 to 19:30
        time_range over_night(t2, t1); // From 19:30 to 15:00 (over
midnight)

        ptime p1(date(2008, 4, 1), time_of_day(14, 20, 0));
        ptime p2(date(2008, 4, 1), time_of_day(16, 40, 0));

        BOOST_CHECK(afternoon.contains(p1) == false);
        BOOST_CHECK(afternoon.contains(p2) == true);

        BOOST_CHECK(over_night.contains(p1) == true);
        BOOST_CHECK(over_night.contains(p2) == false);

        time_range tr1(time_of_day(15, 0, 0), time_of_day(18, 0, 0)); //
15:00 to 18:00
        time_range tr2(time_of_day(16, 0, 0), time_of_day(17, 0, 0)); //
16:00 to 17:00
        time_range tr3(time_of_day(17, 0, 0), time_of_day(19, 0, 0)); //
17:00 to 19:00

        BOOST_CHECK(tr1.contains(tr2) == true);
        BOOST_CHECK(tr2.contains(tr1) == false);
        BOOST_CHECK(tr2.contains(tr3) == false);
}

I think these concepts would fit well into the date time lib. What do you
think?

Regards

James


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