Boost logo

Boost :

From: sipan_at_[hidden]
Date: 2000-04-15 18:41:48


I found it usefull to have general "time" class. I use it as
"timer"(I used to call it stopwatch) and as a general time type
that can be used in C++ wrappers for system wait calls (sleep,
select, poll etc.) In particular, I had to use it with C++ analog
of pthread_cond_timedwait function, which takes "timespec" wich
is a POSIX4 time structure simular to "timeval" structure used
by UNIX functions like select, gettimeofday, etc. The only
difference is the precision:

struct timespec {
        long int tv_sec;
        long int tv_nsec;
};

struct timeval {
        long int tv_sec;
        long int tv_usec;
};

I've modified definition I use so that its style is closer
to boost style(e.g. I call it TimeSpec here I propose to
call it boost::time). As I just want to know if you like that
proposal, I am posting here just the class defenition without
proper #include-s and #ifdef-s and without implementation(which
is trivial and I have working version).

// C++ wrapper for POSIX 4 "timespec" structure

class time : public timespec
{

public:

        enum set_mode
        {
                zero,
                now
        };

        enum time_mode
        {
                system,
                process
        };

        enum time_units
        {
                nsec,
                usec,
                msec,
                sec,
                default
        };

private:

        time_mode M_regime;
        time_units M_units;
        void M_normalize();

public:

        // time(time::now) will set to current time
        time(time_mode m = system,
             time_set s = zero,
             time_units u = msec);

        // Set to current time
        time& now();

        // Set to time to zero [ Epoch (00:00:00 UTC, January 1, 1970) ]
        time& zero();

        // Convert into parts of a second
        int get(time_units = default);

        // now - *this
        int elapsed(time_units = default);

        // Set time
        time& set(int t, time_units u = msec);

        // Get part that is less the second [T%(sec)]
        int extra(time_units u = msec);

        // Get value in parts of s second
        time& add(int t, time_units u = msec);

        // Get date in the printable format
        char* date();

        // Arithmetic operators
        time& operator+=(const time& t);
        time& operator-=(const time& t);

        time operator+(const time& t);
        time operator-(const time& t);

        // Comparison
        bool operator==(const time& t) const;
        bool operator!=(const time& t) const;
        bool operator<(const time& t) const;
        bool operator>(const time& t) const;
        bool operator<=(const time& t) const;
        bool operator>=(const time& t) const;

};


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk