| 1 | // This may look like C code, but it is really -*- C++ -*-
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #ifndef TIMESTAMP_H_SEEN
 | 
|---|
| 4 | #define TIMESTAMP_H_SEEN
 | 
|---|
| 5 | 
 | 
|---|
| 6 | // Classe TimeStamp (Date+heure)
 | 
|---|
| 7 | //
 | 
|---|
| 8 | // R. Ansari      UPS+LAL IN2P3/CNRS  2005
 | 
|---|
| 9 | // Code recupere en partie ds dates.h dates.cc (classe Date) / E. Aubourg 1995-2000
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | #include "machdefs.h"
 | 
|---|
| 13 | #include "objfio.h"
 | 
|---|
| 14 | #include <string> 
 | 
|---|
| 15 | #include <iostream> 
 | 
|---|
| 16 | 
 | 
|---|
| 17 | namespace SOPHYA {
 | 
|---|
| 18 |   
 | 
|---|
| 19 | //! Class representing date and time
 | 
|---|
| 20 | class TimeStamp : public AnyDataObj {
 | 
|---|
| 21 | public:
 | 
|---|
| 22 |   /*  Pour utilisation ulterieure
 | 
|---|
| 23 |   //! GMT / local time enum 
 | 
|---|
| 24 |   enum {kGMTTime=0, kLocalTime=1};
 | 
|---|
| 25 |   */
 | 
|---|
| 26 |   //! Month name enum
 | 
|---|
| 27 |   enum Month {month_January=1, month_February, month_March, month_April, month_May, month_June, month_July,
 | 
|---|
| 28 |              month_August, month_September, month_October, month_November, month_December};  
 | 
|---|
| 29 | 
 | 
|---|
| 30 |   enum StrFmt { FmtPackedDateTime , FmtDateOnly, FmtTimeOnly, FmtDateTime };
 | 
|---|
| 31 | 
 | 
|---|
| 32 |   //! Default Constructor : current date and time (GMT)
 | 
|---|
| 33 |   TimeStamp();
 | 
|---|
| 34 |   TimeStamp(TimeStamp const & ts);
 | 
|---|
| 35 |   //! Constructor with specification of number of days since 0 Jan 1901 0h TU 
 | 
|---|
| 36 |   TimeStamp(double days);
 | 
|---|
| 37 |   //! Constructor from number of days (since 0/01/1901) and number of seconds 
 | 
|---|
| 38 |   TimeStamp(int_8 days, r_8 seconds);
 | 
|---|
| 39 |   //! Constructor with specification of day,month,year,hour,minutes,seconds 
 | 
|---|
| 40 |   TimeStamp(int year, int month, int day, int hour, int min, double sec);
 | 
|---|
| 41 |   //! Constructor with specification of date & time in the format YYYY/MM/DD hh:mm:ss  
 | 
|---|
| 42 |   TimeStamp(string const & date, string const & hour);
 | 
|---|
| 43 |   //! Constructor with specification of date & time in the format YYYY/MM/DD hh:mm:ss  
 | 
|---|
| 44 |   TimeStamp(const char* date, const char* hour);
 | 
|---|
| 45 |   //! Constructor with specification of date & time in the format YYYY-MM-DDThh:mm:ss 
 | 
|---|
| 46 |   TimeStamp(string& datim);
 | 
|---|
| 47 |   //! Constructor with specification of date & time in the format YYYY-MM-DDThh:mm:ss 
 | 
|---|
| 48 |   TimeStamp(const char* datim);
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   //! Set the value of the time stamp copying from "ts"
 | 
|---|
| 51 |   void Set(TimeStamp const & ts);
 | 
|---|
| 52 |   //! Sets the value of the time stamp from the number of days since 0 Jan 1901 0h TU 
 | 
|---|
| 53 |   void Set(double days);
 | 
|---|
| 54 |   //! Sets the value of the time stamp from the number of days and seconds
 | 
|---|
| 55 |   void Set(int_8 days, r_8 seconds);
 | 
|---|
| 56 |   //! Sets the value of the time stamp from a string in the format YYYY-MM-DDThh:mm:ss 
 | 
|---|
| 57 |   void Set(const char * datim);
 | 
|---|
| 58 |  //! Sets the value of the time stamp from a string in the format YYYY-MM-DDThh:mm:ss 
 | 
|---|
| 59 |   inline void Set(string const & datim)  { Set(datim.c_str()); }
 | 
|---|
| 60 |   //! initialize with current date and time (GMT)
 | 
|---|
| 61 |   void SetNow();
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   //! The equal (set) operator 
 | 
|---|
| 64 |   inline TimeStamp& operator= (TimeStamp const & ts) 
 | 
|---|
| 65 |     { Set(ts); return(*this); }
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   //! Sets the value of the date (days) 
 | 
|---|
| 68 |   void SetDate(int year, int month, int day);
 | 
|---|
| 69 |   //! Sets the value of the date (format: DD/MM/YYYY) 
 | 
|---|
| 70 |   void SetDate(const char* date); 
 | 
|---|
| 71 |   //! Sets the value of the date (format: DD/MM/YYYY) 
 | 
|---|
| 72 |   inline void SetDate(string const& date) { SetDate(date.c_str()); }
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   //! Sets the value of the time of day (hours) 
 | 
|---|
| 75 |   void SetHour(int hour, int min, double sec);
 | 
|---|
| 76 |   //! Sets the value of the time of day (format: hh:mm:ss[.ss]) 
 | 
|---|
| 77 |   void SetHour(const char* hour); 
 | 
|---|
| 78 |   //! Sets the value of the time of day (format: hh:mm:ss[.ss]) 
 | 
|---|
| 79 |   inline void SetHour(string const& hour) { SetHour(hour.c_str()); }
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   //! Return the date (Year, Month, Day)
 | 
|---|
| 82 |   void GetDate(int& year, int& month, int& day) const;
 | 
|---|
| 83 |   //! Return the time of the day
 | 
|---|
| 84 |   void GetHour(int& hour, int& min, double& sec) const;
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   //! Return the value of the TimeStamp as days.fraction_days since 0 Jan 1901
 | 
|---|
| 87 |   double ToDays() const;
 | 
|---|
| 88 |   //! Conversion operator to double - return ToDays() 
 | 
|---|
| 89 |   inline operator double() const { return ToDays(); }
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   //! Return the timestamp in string format 
 | 
|---|
| 92 |   string ToString(StrFmt fmt=FmtPackedDateTime) const;
 | 
|---|
| 93 |   
 | 
|---|
| 94 |   //! Return the integral number of days since 0 Jan 1901
 | 
|---|
| 95 |   inline int_8 DaysPart() { return mDays; }
 | 
|---|
| 96 |   //! Return the fractional number of days in seconds ( 0 < nsec < 86400. )
 | 
|---|
| 97 |   inline r_8 SecondsPart() { return mSeconds; }
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   //! Prints the date/time in string format on \b cout 
 | 
|---|
| 100 |   inline  void    Print(StrFmt fmt=FmtDateTime) const  
 | 
|---|
| 101 |      { Print(cout, fmt); }  
 | 
|---|
| 102 |   //! Prints the date/time in string format on stream \b os 
 | 
|---|
| 103 |   virtual void    Print(ostream& os, StrFmt fmt=FmtDateTime)  const;
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   //! Number of days in a given month  
 | 
|---|
| 106 |   static int   MonthDays(int year, int month);
 | 
|---|
| 107 |   //! Number of days in a given year
 | 
|---|
| 108 |   static int YearDays(int annee);
 | 
|---|
| 109 |   //! Number of  days since 0 janvier 1901 0h TU
 | 
|---|
| 110 |   static int_8 ConvertToDays(int year, int month, int day);
 | 
|---|
| 111 |   
 | 
|---|
| 112 | 
 | 
|---|
| 113 | protected:
 | 
|---|
| 114 |   r_8 mSeconds;     // Number of seconds since 00:00:00 
 | 
|---|
| 115 |   int_8 mDays;      // Number of days since 0 Jan 1901
 | 
|---|
| 116 | };
 | 
|---|
| 117 | 
 | 
|---|
| 118 | /*! operator << overloading - Prints date/time in string format on \b os*/
 | 
|---|
| 119 | inline ostream& operator << (ostream& s, TimeStamp const & ts)
 | 
|---|
| 120 |   {  ts.Print(s);  return(s);  }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | /*! Writes the object in the POutPersist stream \b os */
 | 
|---|
| 123 | inline POutPersist& operator << (POutPersist& os, TimeStamp & obj)
 | 
|---|
| 124 | { ObjFileIO<TimeStamp> fio(&obj);  fio.Write(os);  return(os); }
 | 
|---|
| 125 | /*! Reads the object from the PInPersist stream \b is */
 | 
|---|
| 126 | inline PInPersist& operator >> (PInPersist& is, TimeStamp & obj)
 | 
|---|
| 127 | { ObjFileIO<TimeStamp> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | } // namespace SOPHYA
 | 
|---|
| 130 | 
 | 
|---|
| 131 | #endif
 | 
|---|