chrono.h

Go to the documentation of this file.
00001 // OSB library ********************************************* -*- C++ -*-
00006 /*
00007   AUTHOR(S): Andreas Huggel (ahu)
00008              Rene Schneider (rsn)
00009              Stephan Broennimann (vb)
00010 
00011   RCS information
00012    $Name: OSB_060808 $
00013    $Revision: 1.74 $
00014 
00015   License
00016    OSB rating and billing library for communication networks
00017    Copyright (C) 2004, 2005, 2006  OSB systems
00018 
00019    This file may be distributed and/or modify under the terms of the
00020    GNU General Public License (GPL) as published by the Free Software
00021    Foundation which is provided in the file LICENSE.GPL included in the
00022    packaging of this file.
00023 
00024    The file is distributed in the hope that it will be useful, but WITHOUT
00025    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00026    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00027    for more details.
00028 
00029    Holders of a OSB Commercial License may use this file under the terms
00030    and conditions of this commercial license.
00031  */
00032 #ifndef _CHRONO_H_
00033 #define _CHRONO_H_
00034 
00035 // *********************************************************************
00036 // included header files
00037 // + standard includes
00038 #include <string>
00039 
00040 // + local headers
00041 
00042 // *********************************************************************
00043 // namespace extensions
00044 namespace OSB_LIB {
00045 
00046 // *********************************************************************
00047 // type definitions
00048 
00050     enum Weekday { Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun };
00051 
00053     enum Month { Jan = 1, Feb, Mar, Apr, May, Jun,
00054                      Jul, Aug, Sep, Oct, Nov, Dec };
00055 
00056 // *********************************************************************
00057 // class declarations
00058 
00064     class Date {
00065     public:
00074         static const int daytab_[2][13];
00075     public:
00077         inline Date();
00078 
00086         Date(int dd, Month mm, int yy);
00095         explicit Date(
00096             const std::string& src,
00097             const std::string& format = "%d-%m-%Y"
00098         );
00116         Date(Weekday dd, int ww, Month mm, int yy);
00117 
00118     public:
00120 
00121 
00122         static Date today();
00129         static inline const Date min();
00135         static inline const Date max();
00137 
00139 
00140 
00146         bool isSet() const;
00153         bool notSet() const;
00155         int day() const;
00157         Month month() const;
00159         int year() const;
00161         int dayOfYear() const;
00163         Weekday dayOfWeek() const;
00165         inline void getDmy(int& day, Month& mon, int& year) const;
00166 
00180         std::string str(
00181             const std::string& format = "%d-%m-%Y"
00182         ) const;
00184 
00186 
00187 
00188         Date& addYears(int n);
00190         Date& addMonths(int n);
00192         Date& addDays(long n);
00194 
00196 
00197 
00198         long operator- (const Date& b) const { return j_ -  b.j_; }
00200         bool operator==(const Date& b) const { return j_ == b.j_; }
00202         bool operator!=(const Date& b) const { return j_ != b.j_; }
00204         bool operator< (const Date& b) const { return j_ <  b.j_; }
00206         bool operator> (const Date& b) const { return j_ >  b.j_; }
00208         bool operator<=(const Date& b) const { return j_ <= b.j_; }
00210         bool operator>=(const Date& b) const { return j_ >= b.j_; }
00212 
00213     private:
00221         static const int baseYear_;
00223         static const int monday_;
00225         static const int minDay_;
00227         static const int maxDay_;
00228 
00229     private:
00231         explicit Date(long days);
00233         void dmy2j(int dd, Month mm, int yy);
00235         void j2dmy(int &dd, Month& mm, int& yy) const;
00236 
00237     private:
00243         long j_;
00244     };                                  // class Date
00245 
00247 
00248 
00249     inline bool leapyear(int year);
00251     inline int lastDay(Month, int year);
00253 
00259 
00260     Date& operator+=(Date&, long);
00262     Date& operator-=(Date&, long);
00264     Date& operator++(Date&);
00266     Date  operator++(Date&, int);
00268     Date& operator--(Date&);
00270     Date  operator--(Date&, int);
00272     Date  operator+ (Date, long);
00274     Date  operator- (Date, long);
00276 
00282     class Time {
00283     public:
00289         explicit Time(int sec = 0);
00296         Time(int hour, int min, int sec);
00304         explicit Time(const std::string& src);
00305 
00306     public:
00308         static Time now();
00309 
00311 
00312 
00313         int hour() const;
00315         int minute() const;
00317         int second() const;
00319         long numSeconds() const;
00321         std::string str() const;
00323         inline void getHms(int& hour, int& min, int& sec) const
00324         {
00325             return t2hms(hour, min, sec);
00326         }
00328 
00330 
00331 
00332         Time& addHours(int);
00334         Time& addMinutes(int);
00336         Time& addSeconds(long);
00338 
00340 
00341 
00342         Time& operator+ (const Time&);
00344         Time& operator+= (const Time&);
00346         Time& operator- (const Time&);
00348         Time& operator-= (const Time&);
00350 
00352 
00353 
00354         bool operator==(const Time&) const;
00356         bool operator!=(const Time&) const;
00358         bool operator< (const Time&) const;
00360         bool operator> (const Time&) const;
00362         bool operator<=(const Time&) const;
00364         bool operator>=(const Time&) const;
00366 
00367     private:
00369         long t_;
00371         void hms2t(int hour, int min, int sec);
00373         void t2hms(int &hour, int &min, int& sec) const;
00374     };
00375 
00386     class DateTime {
00387     public:
00395         class Offset {
00397             friend class DateTime;
00398         private:
00400             enum Status {
00402                 notSet = 0,
00404                 gmtOnly,
00406                 gmtSys
00407             };
00408         public:
00410             inline Offset();
00416             inline Offset(long utcOff);
00418             inline bool isSet() const;
00424             inline long utcOff() const;
00425         private:
00427             Status status_;
00429             long   offGmt_;
00431             long   offSys_;
00432         };
00433     public:
00435         DateTime() {}
00436 
00446         explicit inline DateTime(
00447             const Date&  date,
00448             const Time&  time   = Time(),
00449                   Offset offset = Offset()
00450         );
00451 
00462         explicit DateTime(
00463             time_t sec,
00464             Offset offset = Offset()
00465         );
00466 
00480         explicit DateTime(
00481             const std::string& src,
00482             const std::string& format = "%d-%m-%Y %T",
00483             Offset offset = Offset()
00484         );
00485 
00486     public:
00488 
00489 
00490         static DateTime now();
00492         static inline const DateTime min();
00494         static inline const DateTime max();
00496 
00498         static inline long getTimezone();
00499 
00501 
00502 
00508         inline bool isSet() const;
00515         inline bool notSet() const;
00517         const Date& date() const { return d_; }
00519         const Time& time() const { return t_; }
00521         int day() const;
00523         Month month() const;
00525         int year() const;
00527         int hour() const;
00529         int minute() const;
00531         int second() const;
00533         const Offset& offset() const;
00535         std::string strLocale() const;
00537         std::string str() const;
00539         std::string strTs() const;
00550         std::string strTsLoc() const;
00552 
00554 
00555 
00556         DateTime& addDays(long);
00558         DateTime& addMonths(int);
00560         DateTime& addYears(int);
00562         DateTime& addHours(int);
00564         DateTime& addMinutes(int);
00566         DateTime& addSeconds(long);
00568 
00570 
00571 
00572         DateTime& operator+ (const Time&);
00574         DateTime& operator+= (const Time&);
00576         DateTime& operator- (const Time&);
00578         DateTime& operator-= (const Time&);
00580 
00582 
00583 
00584         bool operator==(const DateTime&) const;
00586         bool operator!=(const DateTime&) const;
00588         bool operator< (const DateTime&) const;
00590         bool operator> (const DateTime&) const;
00592         bool operator<=(const DateTime&) const;
00594         bool operator>=(const DateTime&) const;
00596 
00597     private:
00607         void normalize();
00608 
00613         void applyOffset();
00614     private:
00616         Date d_;
00618         Time t_;
00620         Offset offset_;
00621     };                                  // class DateTime
00622 }                                       // namespace OSB_LIB
00623 
00624 // *********************************************************************
00625 // inline definitions
00626 namespace OSB_LIB {
00627     inline bool leapyear(int yy)
00628     {
00629         return yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0;
00630     }
00631 
00632     inline int lastDay(Month mm, int yy)
00633     {
00634         int rc = 0;
00635         if (mm < 1 || mm > 12) {
00636             rc = -1;            // could I throw an exception here?
00637         }
00638         else {
00639             rc = Date::daytab_[leapyear(yy)][mm];
00640         }
00641         return rc;
00642     }
00643 
00644     // ********************************************************************
00645     // class Date
00646     inline Date::Date() : j_(minDay_-1)
00647     {
00648         // empty
00649     }
00650 
00651     inline Date::Date(long days) : j_(days)
00652     {
00653         // empty
00654     }
00655 
00656     inline const Date Date::min()
00657     {
00658         return Date(minDay_);
00659     }
00660 
00661     inline const Date Date::max()
00662     {
00663         return Date(maxDay_);
00664     }
00665 
00666     inline bool Date::notSet() const
00667     {
00668         return j_ == minDay_-1;
00669     }
00670 
00671     inline bool Date::isSet() const
00672     {
00673         return !notSet();
00674     }
00675 
00676     inline Date& Date::addDays(long n)
00677     {
00678         j_ += n;
00679         return *this;
00680     }
00681 
00682     inline void Date::getDmy(int& day, Month& mon, int& year) const
00683     {
00684         return j2dmy(day, mon, year);
00685     }
00686 
00687     // ********************************************************************
00688     // class Time
00689     inline Time::Time(int sec)
00690       : t_(sec)
00691     {
00692         // empty
00693     }
00694 
00695     inline long Time::numSeconds() const
00696     {
00697         return t_;
00698     }
00699 
00700     // ********************************************************************
00701     // class DateTime
00702     inline DateTime::DateTime(
00703         const Date&  date,
00704         const Time&  time,
00705               Offset offset
00706     ) : d_(date), t_(time), offset_(offset)
00707     {
00708         normalize();
00709         if (offset_.isSet()) applyOffset();
00710     }
00711 
00712     const DateTime DateTime::min()
00713     {
00714         return DateTime(Date::min());
00715     }
00716 
00717     const DateTime DateTime::max()
00718     {
00719         return DateTime(Date::max());
00720     }
00721 
00722     long DateTime::getTimezone()
00723     {
00724         tzset();
00725         return -1*::timezone;
00726     }
00727 
00728     inline bool DateTime::isSet() const
00729     {
00730         return d_.isSet();
00731     }
00732 
00733     inline bool DateTime::notSet() const
00734     {
00735         return d_.notSet();
00736     }
00737 
00738     // ********************************************************************
00739     // class DateTime::Offset
00740     inline DateTime::Offset::Offset()
00741       : status_(notSet), offGmt_(0), offSys_(0)
00742     {
00743         // empty
00744     }
00745 
00746     inline DateTime::Offset::Offset(long utcOff)
00747       : status_(gmtOnly), offGmt_(utcOff), offSys_(0)
00748     {
00749         // empty
00750     }
00751 
00752     inline bool DateTime::Offset::isSet() const
00753     {
00754         return notSet != status_;
00755     }
00756 
00757     inline long DateTime::Offset::utcOff() const
00758     {
00759         if (!isSet()) return -1;
00760         return offGmt_ + offSys_;
00761     }
00762 }                                       // namespace OSB_LIB
00763 #endif                                  // #ifndef _CHRONO_H_

Generated on Sat Sep 2 14:06:32 2006 for OSB Library by  doxygen 1.4.7