Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

SimpleTime.h

Go to the documentation of this file.
00001 
00002 // simpleTime.h
00003 //
00004 // Purpose: SimpleTime is with nanosecond 
00005 // precision representing a point in time 
00006 // (1900-01-01 .. 2484-07-02). Various
00007 // arithmetic, conversion and comparison 
00008 // operations are supported.
00009 // 
00010 // Author: Erik Zeitler  Date: 2000-09-06
00011 //
00013 
00014 // TODO: 
00015 // - Make conversion operations to/from *NIX time_b structs (ms precision).
00016 // - Include exceptions when errors occur
00017 
00018 #ifndef SIMPLE_TIME_H
00019 #define SIMPLE_TIME_H
00020 
00021 #ifdef AIDA_STD
00022 # undef AIDA_STD
00023 #endif
00024 
00025 #ifdef AIDA_DONT_USE_STD
00026 # define AIDA_STD
00027 #else
00028 # define AIDA_STD std
00029 #endif
00030 
00031 // for time_t
00032 #include <time.h>
00033 
00034 // for NetLogger output
00035 #include <string>
00036 
00037 // SimpleTime
00038 // Erik Zeitler 2000
00039 //
00040 // __References__
00041 // Number of days per year (365.25637):
00042 // - Nordling, Österman:
00043 //   Physics Handbook For Science And Engineering,
00044 //   Studentlitteratur AB, 1999. ISBN 9144008236
00045 // For time_t:
00046 // - Kernighan, Ritchie: The C Programming Language,
00047 //   Second edition, Prentice Hall 1988.
00048 
00049 // Win32 does not support properly unsigned 64 bit integers
00050 #ifdef _WIN32
00051 typedef __int64 TimeT;
00052 #else
00053 //-ap, 17-may-2001: 
00054 //     On request from the ConditionsDB people (forwarded by Stefano Paoli)
00055 //     the type of "TimeT" has been changed from "unsigned long long" to "long long"
00056 //-ap end
00057 typedef long long TimeT;
00058 #endif
00059 
00060 
00061 //-ap 19-jul-2001:
00062 // using a struct here is unsafe, as people do not necc. update 
00063 // _all_ the fields and further processing gets confused by settings
00064 // for +- inf ... reported by Andrea Manara (Compass).
00065 // changed to become a class (with public attributes, yuck :-( ) with
00066 // proper initialization in the constructor.
00067 
00068 // typedef struct {
00069 //   unsigned short year;
00070 //   unsigned short month;
00071 //   unsigned short day;
00072 //   unsigned short hour;
00073 //   unsigned short min;
00074 //   unsigned short sec;
00075 //   unsigned long ns;
00076 //   bool plusInf;
00077 //   bool minusInf;
00078 // } timeAndDate_t;
00079 
00085 class timeAndDate_t {
00086 public:
00087   timeAndDate_t() : 
00088     year(1970), month(1), day(1), 
00089     hour(0), min(0), sec(0), ns(0), 
00090     plusInf(false), minusInf(false)
00091   { /* nop */ }
00092   // leave the others to the compiler ...
00093 public:
00094   unsigned short year;
00095   unsigned short month;
00096   unsigned short day;
00097   unsigned short hour;
00098   unsigned short min;
00099   unsigned short sec;
00100   unsigned long ns;
00101   bool plusInf;
00102   bool minusInf;
00103 };
00104 
00105 //-ap end
00106 
00107 class SimpleTimeDuration;
00108 class SimpleTimeInterval;
00109 
00110 
00111 
00112 
00125 class SimpleTime { 
00126  public:
00127   // Construction/Destruction
00129   SimpleTime();
00130   SimpleTime(const SimpleTime & t) {timeval=t.toTimeT();};
00131   SimpleTime(const time_t & unix_time);
00132   SimpleTime(const TimeT & TimeT_time);
00133   SimpleTime(const timeAndDate_t & calendarTime);
00134   SimpleTime(const unsigned short year,
00135              const unsigned short month,
00136              const unsigned short day,
00137              const unsigned short hour,
00138              const unsigned short min,
00139              const unsigned short sec,
00140              const unsigned long ns=0,
00141              const bool plusInf=0,
00142              const bool minusInf=0);
00143   SimpleTime(const AIDA_STD::string & netLoggerString);
00144   ~SimpleTime() {};
00145 
00146   // Operators
00147   void operator = (const SimpleTime &);
00148   void operator = (const TimeT &);
00149   void operator += (const SimpleTimeDuration &);
00150   void operator -= (const SimpleTimeDuration &);
00151 
00152   operator TimeT() const { return toTimeT(); }
00153 
00154   // Comparison
00155   bool operator == (const SimpleTime &other) { return timeval == other.toTimeT(); }
00156   bool isBefore(const SimpleTime&) const;
00157 
00160   TimeT time() const;
00161 
00162   // Auxiliary functions
00163   void setPlusInf();
00164   void setMinusInf();
00165   bool isPlusInf() const;
00166   bool isMinusInf() const;
00167 
00168   // Conversion and exchange
00169   TimeT  toTimeT() const;
00170   time_t toUnixTime() const;
00171   void fromUnixTime(const time_t &unix_time);
00173   AIDA_STD::string toNetLogger() const;
00174   void fromNetLogger(const AIDA_STD::string &);
00175   void fromCalendarTime(const timeAndDate_t &);
00177   void fromCalendarTime(const unsigned short year,
00178                         const unsigned short month,
00179                         const unsigned short day,
00180                         const unsigned short hour,
00181                         const unsigned short minute,
00182                         const unsigned short sec,
00183                         const unsigned long nanosec=0,
00184                         const bool plusInf=0,
00185                         const bool minusInf=0);
00187   timeAndDate_t toCalendarTime() const;
00188   unsigned short year() const;
00189   unsigned short month() const;
00190   unsigned short day() const;
00191   unsigned short hour() const;
00192   unsigned short minute() const;
00193   unsigned short second() const;
00194   unsigned long nanosec() const;
00195 
00196  private:
00197   TimeT timeval;
00198 };
00199 
00200 // Streaming __int64 is not supported on NT
00201 #ifndef _WIN32
00202 AIDA_STD::ostream & operator << (AIDA_STD::ostream & out, const SimpleTime &);
00203 #endif
00204 SimpleTime operator + (const SimpleTime &, const SimpleTimeDuration &);
00205 SimpleTime operator - (const SimpleTime &, const SimpleTimeDuration &);
00206 
00207 #endif

Generated on Tue May 20 14:50:25 2003 for HepUtilities by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002