libshevek
src/time.hh
00001 /* time.hh - class definitions to work with time.
00002  * Copyright 2003-2006 Bas Wijnen <wijnen@debian.org>
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation, either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef SHEVEK_TIME_HH
00019 #define SHEVEK_TIME_HH
00020 
00021 #include <iostream>
00022 #include <glibmm.h>
00023 
00024 namespace shevek
00025 {
00027   sigc::connection schedule (sigc::slot0 <void> callback, int prio = Glib::PRIORITY_HIGH_IDLE, Glib::RefPtr <Glib::MainContext> context = Glib::MainContext::get_default () );
00028 
00030   typedef int64_t timetype;
00031   class relative_time;
00032 
00034 
00036   class absolute_time
00037   {
00038     // number of seconds since epoch.
00039     timetype m_seconds;
00040     // number of nanoseconds.  Should be less than 1000 000 000.
00041     unsigned m_nanoseconds;
00042     static bool l_schedule (sigc::slot0 <void> callback);
00043     // let schedule use l_schedule
00044     friend
00045     sigc::connection schedule (sigc::slot0 <void> callback, int prio,
00046                                Glib::RefPtr <Glib::MainContext> context);
00047     static unsigned s_digits;
00048   public:
00050 
00053     absolute_time ();
00055 
00058     absolute_time (unsigned years, unsigned months, unsigned days, unsigned hours, unsigned minutes, unsigned seconds, unsigned nanoseconds = 0);
00060 
00063     absolute_time (timetype seconds, unsigned nanoseconds);
00065 
00067     static absolute_time create_from_local (unsigned years, unsigned months, unsigned days, unsigned hours, unsigned minutes, unsigned seconds, unsigned nanoseconds = 0);
00069     static void set_digits (unsigned num);
00071     static unsigned get_digits ();
00073     absolute_time operator+ (relative_time that) const;
00075     absolute_time operator- (relative_time that) const;
00077     relative_time operator- (absolute_time that) const;
00079     absolute_time &operator+= (relative_time that);
00081     absolute_time &operator-= (relative_time that);
00083     bool operator< (absolute_time that) const;
00085     bool operator> (absolute_time that) const;
00087     bool operator<= (absolute_time that) const;
00089     bool operator>= (absolute_time that) const;
00091 
00094     bool operator== (absolute_time that) const;
00096 
00099     bool operator!= (absolute_time that) const;
00101     unsigned nanoseconds () const;
00103     unsigned local_second () const;
00105     unsigned local_minute () const;
00107     unsigned local_hour () const;
00109     unsigned local_days () const;
00111     unsigned local_day () const;
00113     unsigned local_weekday () const;
00115     unsigned local_month () const;
00117     unsigned local_year () const;
00119     unsigned second () const;
00121     unsigned minute () const;
00123     unsigned hour () const;
00125     unsigned days () const;
00127     unsigned day () const;
00129     unsigned weekday () const;
00131     unsigned month () const;
00133     unsigned year () const;
00135     timetype total () const;
00137     sigc::connection schedule (sigc::slot0 <void> callback, Glib::RefPtr <Glib::MainContext> context = Glib::MainContext::get_default ());
00139     friend std::ostream &operator<< (std::ostream &s, absolute_time t);
00140   };
00141 
00143   class relative_time
00144   {
00145     // number of seconds.
00146     timetype m_seconds;
00147     // number of nanoseconds.  Should be less than 1000000000.
00148     int m_nanoseconds;
00149     static unsigned s_digits;
00150   public:
00152     relative_time ();
00154     relative_time (timetype days, int hours, int minutes, int seconds, int nanoseconds = 0);
00156 
00159     relative_time (timetype seconds, unsigned nanoseconds);
00161     static void set_digits (unsigned num);
00163     static unsigned get_digits ();
00165     relative_time operator+ (relative_time that) const;
00167     absolute_time operator+ (absolute_time that) const;
00169     relative_time operator- (relative_time that) const;
00171     relative_time operator- () const;
00173     relative_time operator* (float c) const;
00175     relative_time operator/ (float c) const;
00177     relative_time operator% (relative_time that) const;
00179     double operator/ (relative_time that) const;
00181     relative_time &operator+= (relative_time that);
00183     relative_time &operator-= (relative_time that);
00185     relative_time &operator*= (float c);
00187     relative_time &operator/= (float c);
00189     relative_time &operator%= (relative_time that);
00191     bool operator< (relative_time that) const;
00193     bool operator> (relative_time that) const;
00195     bool operator<= (relative_time that) const;
00197     bool operator>= (relative_time that) const;
00200     bool operator== (relative_time that) const;
00203     bool operator!= (relative_time that) const;
00205     unsigned nanoseconds () const;
00207     unsigned seconds () const;
00209     unsigned minutes () const;
00211     unsigned hours () const;
00213     unsigned days () const;
00215     bool isnegative () const;
00217     timetype total () const;
00219     friend std::ostream &operator<< (std::ostream &s, relative_time t);
00220   private:
00221     // internal function to clean the seconds/nanoseconds
00222     void l_clean ();
00223   };
00225   std::istream &operator>> (std::istream &s, absolute_time &t);
00227   std::istream &operator>> (std::istream &s, relative_time &t);
00228 }
00229 
00230 
00231 #endif