PSTime/include/Duration.h

Go to the documentation of this file.
00001 #ifndef PSTIME_DURATION_H
00002 #define PSTIME_DURATION_H
00003 
00004 //--------------------------------------------------------------------------
00005 // File and Version Information:
00006 //      $Id: Duration.h 5166 2013-01-25 18:57:11Z salnikov@SLAC.STANFORD.EDU $
00007 //
00008 // Description:
00009 //      Class Duration.
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------
00014 // C/C++ Headers --
00015 //-----------------
00016 #include <iostream>
00017 #include <string>
00018 
00019 //              ---------------------
00020 //              -- Class Interface --
00021 //              ---------------------
00022 
00023 
00024 namespace PSTime {
00025 
00026 /**
00027  *  @ingroup PSTime
00028  * 
00029  *  @brief This class is intended to work with durations in the ISO8601 standard.
00030  *
00031  *  This software was developed for the LCLS project.  If you use all or 
00032  *  part of it, please give an appropriate acknowledgment.
00033  *
00034  *  @see Time
00035  *
00036  *  @version \$Id: Duration.h 5166 2013-01-25 18:57:11Z salnikov@SLAC.STANFORD.EDU $
00037  *
00038  *  @author Mikhail S. Dubrovin
00039  */
00040 
00041 class Duration  {
00042 public:
00043 
00044   enum ParseStatus {
00045     PARSE_IS_OK,
00046     DURATION_STRING_TOO_SHORT,
00047     DURATION_STRING_TOO_LONG,
00048     DURATION_STRING_WRONG_FORMAT_MISSING_P,
00049     DURATION_TOO_LONG_FIELD_FOR_YEARS,
00050     DURATION_TOO_LONG_FIELD_FOR_MONTHS,
00051     DURATION_TOO_LONG_FIELD_FOR_DAYS,
00052     DURATION_TOO_LONG_FIELD_FOR_HOURS,
00053     DURATION_TOO_LONG_FIELD_FOR_MINUTES,
00054     DURATION_TOO_LONG_FIELD_FOR_SECONDS,
00055     DURATION_TOO_LONG_FIELD_FOR_SECOND_FRACTION
00056   };
00057 
00058 /** Default constructor */
00059   Duration () ;
00060 
00061 /** Copy constructor */
00062   Duration( const Duration & d );
00063 
00064 /** Constructs from seconds and nanoseconds */
00065   Duration( time_t sec, time_t nsec = 0 );
00066 
00067 /** 
00068  * Constructs from human numbers 
00069  * In this implementation I ignore monthes, because they may have ambigous number of days 30 or 31.
00070  * The same problem exists for years 364 or 365 days, though I hope that so long duration is not
00071  * very practical in our applications.
00072  */
00073   Duration( time_t Years,  // Needs at least in 3 parameters to distinguish from previous constructor
00074             time_t Days, 
00075             time_t Hours, 
00076             time_t Mins  = 0, 
00077             time_t Secs  = 0,
00078             time_t Nsecs = 0 );
00079 
00080 /** Destructor */
00081   virtual ~Duration () {};
00082 
00083     /** Operators */
00084     Duration   operator  - ( const Duration & d1 ) const;            
00085 
00086     Duration   operator  + ( const Duration & d1 ) const;            
00087 
00088     Duration & operator  = ( const Duration & d1 );            
00089 
00090     Duration & operator += ( const Duration & d1 );            
00091 
00092     bool operator == ( const Duration & d ) const    
00093         { 
00094             return ( m_sec == d.m_sec && m_nsec == d.m_nsec );
00095         }
00096 
00097     bool operator != ( const Duration & d ) const 
00098         { 
00099             return !( *this == d ); 
00100         }
00101 
00102     bool operator <  ( const Duration & d ) const  
00103         { 
00104             return ( m_sec < d.m_sec ) || ( m_sec == d.m_sec && m_nsec < d.m_nsec );
00105         }
00106 
00107     bool operator > ( const Duration & d ) const
00108         { 
00109             return ( m_sec > d.m_sec ) || ( m_sec == d.m_sec && m_nsec > d.m_nsec );
00110         }
00111 
00112     bool operator <=  ( const Duration & d ) const 
00113         { 
00114             return !( *this > d );
00115         }
00116 
00117     bool operator >= ( const Duration & d ) const 
00118         { 
00119             return !( *this < d ); 
00120         }
00121 
00122     /** Selectors */
00123     time_t  getSec ( ) const { return m_sec;  }
00124     time_t  getNsec( ) const { return m_nsec; }
00125 
00126     /** Public methods */
00127     void Print() const;
00128 
00129     /**
00130      * Makes the duration string in the format: PnYnMnDTnHnMnS
00131      * from the object entity.
00132      */
00133     std::string strDurationBasic() const;
00134 
00135 
00136     /**
00137      * Splits the Duration object entity for
00138      * Years, DaysAfterY, HoursAfterD, MinsAfterH, SecsAfterM
00139      * Note, the month are not used because of 28,29,30,31 days ambiguity.
00140      */
00141     void splitDurationSecsForYDHMS(time_t &Years, 
00142                                    time_t &DaysAfterY, 
00143                                    time_t &HoursAfterD, 
00144                                    time_t &MinsAfterH, 
00145                                    time_t &SecsAfterM) const;
00146 
00147     /** Friends */
00148     friend std::ostream & operator << ( std::ostream & os, const Duration & d );  
00149 
00150     // Static data member starts with s_.
00151     //    static const time_t s_nsecInASec;   
00152 
00153     /** 
00154      * The parsification engine,
00155      * the string in standard format P[nY][nM][nD][T[nH][nM][n[.f]S]] is parsed in the Duration object.
00156      * Note, we assume that the monnth M has 30 days..., 
00157      * so it is better to escape this ambiguity using any number of days D.
00158      * Also note that seconds S may be fractional: n[.f]S, that is beyond the ISO8601 standard.
00159      */
00160     static int parseStringToDuration( const std::string & str_dur, Duration & d );
00161 
00162 private:
00163 
00164     /** Data members */
00165     time_t  m_sec;         // number of seconds
00166     time_t  m_nsec;        // number of nanoseconds
00167 
00168 }; // class Duration
00169 } // namespace PSTime
00170 
00171 #endif // PSTIME_DURATION_H

Generated on 19 Dec 2016 for PSANAclasses by  doxygen 1.4.7