libshevek
time.hh
1 /* time.hh - class definitions to work with time.
2  * Copyright 2003-2006 Bas Wijnen <wijnen@debian.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SHEVEK_TIME_HH
19 #define SHEVEK_TIME_HH
20 
21 #include <iostream>
22 #include <glibmm.h>
23 
24 namespace shevek
25 {
27  sigc::connection schedule (sigc::slot0 <void> callback, int prio = Glib::PRIORITY_HIGH_IDLE, Glib::RefPtr <Glib::MainContext> context = Glib::MainContext::get_default () );
28 
30  typedef int64_t timetype;
31  class relative_time;
32 
34 
37  {
38  // number of seconds since epoch.
39  timetype m_seconds;
40  // number of nanoseconds. Should be less than 1000 000 000.
41  unsigned m_nanoseconds;
42  static bool l_schedule (sigc::slot0 <void> callback);
43  // let schedule use l_schedule
44  friend
45  sigc::connection schedule (sigc::slot0 <void> callback, int prio,
46  Glib::RefPtr <Glib::MainContext> context);
47  static unsigned s_digits;
48  public:
50 
53  absolute_time ();
55 
58  absolute_time (unsigned years, unsigned months, unsigned days, unsigned hours, unsigned minutes, unsigned seconds, unsigned nanoseconds = 0);
60 
63  absolute_time (timetype seconds, unsigned nanoseconds);
65 
67  static absolute_time create_from_local (unsigned years, unsigned months, unsigned days, unsigned hours, unsigned minutes, unsigned seconds, unsigned nanoseconds = 0);
69  static void set_digits (unsigned num);
71  static unsigned get_digits ();
83  bool operator< (absolute_time that) const;
85  bool operator> (absolute_time that) const;
87  bool operator<= (absolute_time that) const;
89  bool operator>= (absolute_time that) const;
91 
94  bool operator== (absolute_time that) const;
96 
99  bool operator!= (absolute_time that) const;
101  unsigned nanoseconds () const;
103  unsigned local_second () const;
105  unsigned local_minute () const;
107  unsigned local_hour () const;
109  unsigned local_days () const;
111  unsigned local_day () const;
113  unsigned local_weekday () const;
115  unsigned local_month () const;
117  unsigned local_year () const;
119  unsigned second () const;
121  unsigned minute () const;
123  unsigned hour () const;
125  unsigned days () const;
127  unsigned day () const;
129  unsigned weekday () const;
131  unsigned month () const;
133  unsigned year () const;
135  timetype total () const;
137  sigc::connection schedule (sigc::slot0 <void> callback, Glib::RefPtr <Glib::MainContext> context = Glib::MainContext::get_default ());
139  friend std::ostream &operator<< (std::ostream &s, absolute_time t);
140  };
141 
144  {
145  // number of seconds.
146  timetype m_seconds;
147  // number of nanoseconds. Should be less than 1000000000.
148  int m_nanoseconds;
149  static unsigned s_digits;
150  public:
152  relative_time ();
154  relative_time (timetype days, int hours, int minutes, int seconds, int nanoseconds = 0);
156 
159  relative_time (timetype seconds, unsigned nanoseconds);
161  static void set_digits (unsigned num);
163  static unsigned get_digits ();
171  relative_time operator- () const;
173  relative_time operator* (float c) const;
175  relative_time operator/ (float c) const;
179  double operator/ (relative_time that) const;
185  relative_time &operator*= (float c);
187  relative_time &operator/= (float c);
191  bool operator< (relative_time that) const;
193  bool operator> (relative_time that) const;
195  bool operator<= (relative_time that) const;
197  bool operator>= (relative_time that) const;
200  bool operator== (relative_time that) const;
203  bool operator!= (relative_time that) const;
205  unsigned nanoseconds () const;
207  unsigned seconds () const;
209  unsigned minutes () const;
211  unsigned hours () const;
213  unsigned days () const;
215  bool isnegative () const;
217  timetype total () const;
219  friend std::ostream &operator<< (std::ostream &s, relative_time t);
220  private:
221  // internal function to clean the seconds/nanoseconds
222  void l_clean ();
223  };
225  std::istream &operator>> (std::istream &s, absolute_time &t);
227  std::istream &operator>> (std::istream &s, relative_time &t);
228 }
229 
230 
231 #endif
relative_time operator/(float c) const
Scale an interval.
relative_time & operator/=(float c)
Scale the interval.
relative_time & operator%=(relative_time that)
Modulo.
static unsigned get_digits()
Get the number of digits which is used when printing.
bool operator==(absolute_time that) const
Compare two moments.
unsigned local_weekday() const
Get the day of the week in local time, range 0-6 where 0 means sunday.
bool operator!=(absolute_time that) const
Compare two moments.
unsigned local_minute() const
Get the minutes in local time.
relative_time()
The default constructor creates an interval of 0.
relative_time operator*(float c) const
Scale an interval.
timetype total() const
Total number of seconds, as encoded.
Time interval.
Definition: time.hh:143
friend std::ostream & operator<<(std::ostream &s, relative_time t)
Write the interval to a std::ostream.
relative_time operator+(relative_time that) const
Add two intervals.
absolute_time()
Create a new absolute_time containing the current time.
Definition: args.hh:52
unsigned local_second() const
Get the seconds in local time.
bool operator==(relative_time that) const
static void set_digits(unsigned num)
Set number of digits to use when printing (for fractions of seconds).
absolute_time operator+(relative_time that) const
Add an interval to this moment.
relative_time operator-() const
Negate an interval.
unsigned seconds() const
Number of seconds.
bool operator>(absolute_time that) const
Compare two moments.
bool operator<=(absolute_time that) const
Compare two moments.
bool operator<=(relative_time that) const
Compare with another interval.
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.
bool operator<(absolute_time that) const
Compare two moments.
relative_time & operator+=(relative_time that)
Add an interval.
unsigned hour() const
Get the hour in UTC.
unsigned local_month() const
Get the month in local time, range 1-12.
relative_time & operator*=(float c)
Scale the interval.
static void set_digits(unsigned num)
Set number of digits to use when printing (for fractions of seconds)
bool operator>=(absolute_time that) const
Compare two moments.
friend std::ostream & operator<<(std::ostream &s, absolute_time t)
Write the time to a std::ostream.
absolute_time operator-(relative_time that) const
Subtract an interval from this moment.
relative_time & operator-=(relative_time that)
Subtract an interval.
unsigned local_days() const
Get the day of the year in local time, range 0-365.
static unsigned get_digits()
Get the number of digits that is used when printing.
unsigned local_year() const
Get the year in local time.
unsigned days() const
Number of days.
unsigned month() const
Get the month in UTC, range 1-12.
bool operator>=(relative_time that) const
Compare with another interval.
unsigned year() const
Get the year in UTC.
The absolute_time class stores a date and time.
Definition: time.hh:36
bool operator!=(relative_time that) const
relative_time operator%(relative_time that) const
Modulo operator for two intervals.
unsigned minutes() const
Number of minutes.
unsigned nanoseconds() const
Number of nanoseconds.
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.
unsigned hours() const
Number of hours.
unsigned local_day() const
Get the day of the month in local time, range 1-31.
unsigned second() const
Get the seconds in UTC.
unsigned days() const
Get the day of the year in UTC, range 0-365.
bool isnegative() const
Is this a negative interval?
bool operator<(relative_time that) const
Compare with another interval.
absolute_time & operator-=(relative_time that)
Subtract an interval from this moment.
bool operator>(relative_time that) const
Compare with another interval.
timetype total() const
Total number of seconds since january 1970, as encoded.
absolute_time & operator+=(relative_time that)
Add an interval to this moment.
unsigned minute() const
Get the minutes in UTC.
unsigned weekday() const
Get the day of the week in UTC, range 0-6 where 0 means sunday.
unsigned local_hour() const
Get the hour in local time.
unsigned day() const
Get the day of the month in UTC, range 1-31.
unsigned nanoseconds() const
Get the nanoseconds.