[2654] | 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 <string>
|
---|
| 14 | #include <iostream>
|
---|
| 15 |
|
---|
| 16 | namespace SOPHYA {
|
---|
| 17 |
|
---|
| 18 | //! Class representing date and time
|
---|
| 19 | class TimeStamp {
|
---|
| 20 | public:
|
---|
| 21 | /* Pour utilisation ulterieure
|
---|
| 22 | //! GMT / local time enum
|
---|
| 23 | enum {kGMTTime=0, kLocalTime=1};
|
---|
| 24 | */
|
---|
| 25 | //! Month name enum
|
---|
| 26 | enum Month {month_January=1, month_February, month_March, month_April, month_May, month_June, month_July,
|
---|
| 27 | month_August, month_September, month_October, month_November, month_December};
|
---|
| 28 |
|
---|
| 29 | //! Default Constructor : current date and time (GMT)
|
---|
| 30 | TimeStamp();
|
---|
| 31 | TimeStamp(TimeStamp const & ts);
|
---|
| 32 | //! Constructor with specification of number of days since 0 Jan 1901 0h TU
|
---|
| 33 | TimeStamp(double days);
|
---|
[2826] | 34 | //! Constructor from number of days (since 0/01/1901) and number of seconds
|
---|
| 35 | TimeStamp(int_8 days, r_8 seconds);
|
---|
[2654] | 36 | //! Constructor with specification of day,month,year,hour,minutes,seconds
|
---|
| 37 | TimeStamp(int year, int month, int day, int hour, int min, double sec);
|
---|
| 38 | //! Constructor with specification of date & time in the format YYYY/MM/DD hh:mm:ss
|
---|
| 39 | TimeStamp(string& date, string& hour);
|
---|
| 40 | TimeStamp(const char* date, const char* hour);
|
---|
| 41 |
|
---|
| 42 | //! Set the value of the time stamp copying from "ts"
|
---|
| 43 | void Set(TimeStamp const & ts);
|
---|
| 44 | //! Sets the value of the time stamp from the number of days since 0 Jan 1901 0h TU
|
---|
| 45 | void Set(double days);
|
---|
[2826] | 46 | //! Sets the value of the time stamp from the number of days and seconds
|
---|
| 47 | void Set(int_8 days, r_8 seconds);
|
---|
[2654] | 48 | //! initialize with current date and time (GMT)
|
---|
| 49 | void SetNow();
|
---|
| 50 |
|
---|
| 51 | //! The equal (set) operator
|
---|
| 52 | inline TimeStamp& operator= (TimeStamp const & ts)
|
---|
| 53 | { Set(ts); return(*this); }
|
---|
| 54 |
|
---|
| 55 | //! Sets the value of the date (days)
|
---|
| 56 | void SetDate(int year, int month, int day);
|
---|
| 57 | //! Sets the value of the date (format: DD/MM/YYYY)
|
---|
| 58 | void SetDate(const char* date);
|
---|
| 59 | //! Sets the value of the date (format: DD/MM/YYYY)
|
---|
| 60 | inline void SetDate(string const& date) { SetDate(date.c_str()); }
|
---|
| 61 |
|
---|
| 62 | //! Sets the value of the time of day (hours)
|
---|
| 63 | void SetHour(int hour, int min, double sec);
|
---|
| 64 | //! Sets the value of the time of day (format: hh:mm:ss[.ss])
|
---|
| 65 | void SetHour(const char* hour);
|
---|
| 66 | //! Sets the value of the time of day (format: hh:mm:ss[.ss])
|
---|
| 67 | inline void SetHour(string const& hour) { SetHour(hour.c_str()); }
|
---|
| 68 |
|
---|
| 69 | //! Return the date (Year, Month, Day)
|
---|
| 70 | void GetDate(int& year, int& month, int& day) const;
|
---|
| 71 | //! Return the time of the day
|
---|
| 72 | void GetHour(int& hour, int& min, double& sec) const;
|
---|
| 73 |
|
---|
| 74 | //! Return the value of the TimeStamp as days.fraction_days since 0 Jan 1901
|
---|
| 75 | double ToDays() const;
|
---|
[2826] | 76 | //! Conversion operator to double - return ToDays()
|
---|
[2654] | 77 | inline operator double() const { return ToDays(); }
|
---|
| 78 |
|
---|
| 79 | //! Return the timestamp in string format
|
---|
[2826] | 80 | string ToString(bool fgday=true, bool fghour=true) const;
|
---|
[2654] | 81 |
|
---|
[2826] | 82 | //! Return the integral number of days since 0 Jan 1901
|
---|
| 83 | inline int_8 DaysPart() { return mDays; }
|
---|
| 84 | //! Return the fractional number of days in seconds ( 0 < nsec < 86400. )
|
---|
| 85 | inline r_8 SecondsPart() { return mSeconds; }
|
---|
[2654] | 86 |
|
---|
[2826] | 87 | //! Prints the date/time in string format on \b cout
|
---|
| 88 | inline void Print(bool fgday=true, bool fghour=true) const
|
---|
| 89 | { Print(cout, fgday, fghour); }
|
---|
| 90 | //! Prints the date/time in string format on stream \b os
|
---|
| 91 | virtual void Print(ostream& os, bool fgday=true, bool fghour=true) const;
|
---|
| 92 |
|
---|
[2654] | 93 | //! Number of days in a given month
|
---|
| 94 | static int MonthDays(int year, int month);
|
---|
| 95 | //! Number of days in a given year
|
---|
| 96 | static int YearDays(int annee);
|
---|
| 97 | //! Number of days since 0 janvier 1901 0h TU
|
---|
| 98 | static int_8 ConvertToDays(int year, int month, int day);
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | protected:
|
---|
| 102 | r_8 mSeconds; // Number of seconds since 00:00:00
|
---|
| 103 | int_8 mDays; // Number of days since 0 Jan 1901
|
---|
| 104 | };
|
---|
| 105 |
|
---|
| 106 | /*! operator << overloading - Prints date/time in string format on \b os*/
|
---|
| 107 | inline ostream& operator << (ostream& s, TimeStamp const & ts)
|
---|
| 108 | { ts.Print(s); return(s); }
|
---|
| 109 |
|
---|
| 110 | } // namespace SOPHYA
|
---|
| 111 |
|
---|
| 112 | #endif
|
---|