time.hh
/* time.hh - class definitions to work with time.
* Copyright 2003-2006 Bas Wijnen <wijnen@debian.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHEVEK_TIME_HH
#define SHEVEK_TIME_HH
#include <iostream>
#include <glibmm.h>
namespace shevek
{
sigc::connection schedule (sigc::slot0 <void> callback, int prio = Glib::PRIORITY_HIGH_IDLE, Glib::RefPtr <Glib::MainContext> context = Glib::MainContext::get_default () );
typedef int64_t timetype;
class relative_time;
class absolute_time
{
// number of seconds since epoch.
timetype m_seconds;
// number of nanoseconds. Should be less than 1000 000 000.
unsigned m_nanoseconds;
static bool l_schedule (sigc::slot0 <void> callback);
// let schedule use l_schedule
friend
sigc::connection schedule (sigc::slot0 <void> callback, int prio,
Glib::RefPtr <Glib::MainContext> context);
static unsigned s_digits;
public:
absolute_time ();
absolute_time (unsigned years, unsigned months, unsigned days, unsigned hours, unsigned minutes, unsigned seconds, unsigned nanoseconds = 0);
absolute_time (timetype seconds, unsigned nanoseconds);
static absolute_time create_from_local (unsigned years, unsigned months, unsigned days, unsigned hours, unsigned minutes, unsigned seconds, unsigned nanoseconds = 0);
static void set_digits (unsigned num);
static unsigned get_digits ();
absolute_time operator+ (relative_time that) const;
absolute_time operator- (relative_time that) const;
relative_time operator- (absolute_time that) const;
absolute_time &operator+= (relative_time that);
absolute_time &operator-= (relative_time that);
bool operator< (absolute_time that) const;
bool operator> (absolute_time that) const;
bool operator<= (absolute_time that) const;
bool operator>= (absolute_time that) const;
bool operator== (absolute_time that) const;
bool operator!= (absolute_time that) const;
unsigned nanoseconds () const;
unsigned local_second () const;
unsigned local_minute () const;
unsigned local_hour () const;
unsigned local_days () const;
unsigned local_day () const;
unsigned local_weekday () const;
unsigned local_month () const;
unsigned local_year () const;
unsigned second () const;
unsigned minute () const;
unsigned hour () const;
unsigned days () const;
unsigned day () const;
unsigned weekday () const;
unsigned month () const;
unsigned year () const;
timetype total () const;
sigc::connection schedule (sigc::slot0 <void> callback, Glib::RefPtr <Glib::MainContext> context = Glib::MainContext::get_default ());
friend std::ostream &operator<< (std::ostream &s, absolute_time t);
};
class relative_time
{
// number of seconds.
timetype m_seconds;
// number of nanoseconds. Should be less than 1000000000.
int m_nanoseconds;
static unsigned s_digits;
public:
relative_time ();
relative_time (timetype days, int hours, int minutes, int seconds, int nanoseconds = 0);
relative_time (timetype seconds, unsigned nanoseconds);
static void set_digits (unsigned num);
static unsigned get_digits ();
relative_time operator+ (relative_time that) const;
absolute_time operator+ (absolute_time that) const;
relative_time operator- (relative_time that) const;
relative_time operator- () const;
relative_time operator* (float c) const;
relative_time operator/ (float c) const;
relative_time operator% (relative_time that) const;
double operator/ (relative_time that) const;
relative_time &operator+= (relative_time that);
relative_time &operator-= (relative_time that);
relative_time &operator*= (float c);
relative_time &operator/= (float c);
relative_time &operator%= (relative_time that);
bool operator< (relative_time that) const;
bool operator> (relative_time that) const;
bool operator<= (relative_time that) const;
bool operator>= (relative_time that) const;
bool operator== (relative_time that) const;
bool operator!= (relative_time that) const;
unsigned nanoseconds () const;
unsigned seconds () const;
unsigned minutes () const;
unsigned hours () const;
unsigned days () const;
bool isnegative () const;
timetype total () const;
friend std::ostream &operator<< (std::ostream &s, relative_time t);
private:
// internal function to clean the seconds/nanoseconds
void l_clean ();
};
std::istream &operator>> (std::istream &s, absolute_time &t);
std::istream &operator>> (std::istream &s, relative_time &t);
}
#endif
shevek::relative_time::operator/
relative_time operator/(float c) const
Scale an interval.
shevek::relative_time::operator/=
relative_time & operator/=(float c)
Scale the interval.
shevek::relative_time::operator%=
relative_time & operator%=(relative_time that)
Modulo.
shevek::absolute_time::get_digits
static unsigned get_digits()
Get the number of digits which is used when printing.
shevek::absolute_time::operator==
bool operator==(absolute_time that) const
Compare two moments.
shevek::absolute_time::local_weekday
unsigned local_weekday() const
Get the day of the week in local time, range 0-6 where 0 means sunday.
shevek::absolute_time::operator!=
bool operator!=(absolute_time that) const
Compare two moments.
shevek::absolute_time::local_minute
unsigned local_minute() const
Get the minutes in local time.
shevek::relative_time::relative_time
relative_time()
The default constructor creates an interval of 0.
shevek::relative_time::operator*
relative_time operator*(float c) const
Scale an interval.
shevek::relative_time::total
timetype total() const
Total number of seconds, as encoded.
shevek::relative_time
Time interval.
Definition: time.hh:143
shevek::relative_time::operator<<
friend std::ostream & operator<<(std::ostream &s, relative_time t)
Write the interval to a std::ostream.
shevek::relative_time::operator+
relative_time operator+(relative_time that) const
Add two intervals.
shevek::absolute_time::absolute_time
absolute_time()
Create a new absolute_time containing the current time.
shevek
Definition: args.hh:52
shevek::absolute_time::local_second
unsigned local_second() const
Get the seconds in local time.
shevek::relative_time::operator==
bool operator==(relative_time that) const
shevek::relative_time::set_digits
static void set_digits(unsigned num)
Set number of digits to use when printing (for fractions of seconds).
shevek::absolute_time::operator+
absolute_time operator+(relative_time that) const
Add an interval to this moment.
shevek::relative_time::operator-
relative_time operator-() const
Negate an interval.
shevek::relative_time::seconds
unsigned seconds() const
Number of seconds.
shevek::absolute_time::operator>
bool operator>(absolute_time that) const
Compare two moments.
shevek::absolute_time::operator<=
bool operator<=(absolute_time that) const
Compare two moments.
shevek::relative_time::operator<=
bool operator<=(relative_time that) const
Compare with another interval.
shevek::absolute_time::schedule
friend sigc::connection schedule(sigc::slot0< void > callback, int prio, Glib::RefPtr< Glib::MainContext > context)
Schedule a callback for when the main loop has time.
shevek::absolute_time::operator<
bool operator<(absolute_time that) const
Compare two moments.
shevek::relative_time::operator+=
relative_time & operator+=(relative_time that)
Add an interval.
shevek::absolute_time::hour
unsigned hour() const
Get the hour in UTC.
shevek::absolute_time::local_month
unsigned local_month() const
Get the month in local time, range 1-12.
shevek::relative_time::operator*=
relative_time & operator*=(float c)
Scale the interval.
shevek::absolute_time::set_digits
static void set_digits(unsigned num)
Set number of digits to use when printing (for fractions of seconds)
shevek::absolute_time::operator>=
bool operator>=(absolute_time that) const
Compare two moments.
shevek::absolute_time::operator<<
friend std::ostream & operator<<(std::ostream &s, absolute_time t)
Write the time to a std::ostream.
shevek::absolute_time::operator-
absolute_time operator-(relative_time that) const
Subtract an interval from this moment.
shevek::relative_time::operator-=
relative_time & operator-=(relative_time that)
Subtract an interval.
shevek::absolute_time::local_days
unsigned local_days() const
Get the day of the year in local time, range 0-365.
shevek::relative_time::get_digits
static unsigned get_digits()
Get the number of digits that is used when printing.
shevek::absolute_time::local_year
unsigned local_year() const
Get the year in local time.
shevek::relative_time::days
unsigned days() const
Number of days.
shevek::absolute_time::month
unsigned month() const
Get the month in UTC, range 1-12.
shevek::relative_time::operator>=
bool operator>=(relative_time that) const
Compare with another interval.
shevek::absolute_time::year
unsigned year() const
Get the year in UTC.
shevek::absolute_time
The absolute_time class stores a date and time.
Definition: time.hh:36
shevek::relative_time::operator!=
bool operator!=(relative_time that) const
shevek::relative_time::operator%
relative_time operator%(relative_time that) const
Modulo operator for two intervals.
shevek::relative_time::minutes
unsigned minutes() const
Number of minutes.
shevek::relative_time::nanoseconds
unsigned nanoseconds() const
Number of nanoseconds.
shevek::absolute_time::create_from_local
static absolute_time create_from_local(unsigned years, unsigned months, unsigned days, unsigned hours, unsigned minutes, unsigned seconds, unsigned nanoseconds=0)
Semi-constructor which creates a new absolute_time given a date in local time.
shevek::relative_time::hours
unsigned hours() const
Number of hours.
shevek::absolute_time::local_day
unsigned local_day() const
Get the day of the month in local time, range 1-31.
shevek::absolute_time::second
unsigned second() const
Get the seconds in UTC.
shevek::absolute_time::days
unsigned days() const
Get the day of the year in UTC, range 0-365.
shevek::relative_time::isnegative
bool isnegative() const
Is this a negative interval?
shevek::relative_time::operator<
bool operator<(relative_time that) const
Compare with another interval.
shevek::absolute_time::operator-=
absolute_time & operator-=(relative_time that)
Subtract an interval from this moment.
shevek::relative_time::operator>
bool operator>(relative_time that) const
Compare with another interval.
shevek::absolute_time::total
timetype total() const
Total number of seconds since january 1970, as encoded.
shevek::absolute_time::operator+=
absolute_time & operator+=(relative_time that)
Add an interval to this moment.
shevek::absolute_time::minute
unsigned minute() const
Get the minutes in UTC.
shevek::absolute_time::weekday
unsigned weekday() const
Get the day of the week in UTC, range 0-6 where 0 means sunday.
shevek::absolute_time::local_hour
unsigned local_hour() const
Get the hour in local time.
shevek::absolute_time::day
unsigned day() const
Get the day of the month in UTC, range 1-31.
shevek::absolute_time::nanoseconds
unsigned nanoseconds() const
Get the nanoseconds.