| 1 | #ifndef FACT_Time | 
|---|
| 2 | #define FACT_Time | 
|---|
| 3 |  | 
|---|
| 4 | #include <boost/date_time/local_time/local_time.hpp> | 
|---|
| 5 |  | 
|---|
| 6 | // ************************************************************************** | 
|---|
| 7 | /** @class _time_format | 
|---|
| 8 |  | 
|---|
| 9 | @brief Helper to manipulate the input and output format of a time in a stream | 
|---|
| 10 |  | 
|---|
| 11 | This class represents a stream manipulator. It is used to change the input | 
|---|
| 12 | or output format of a Time (or boost::posix_time) object to and from a | 
|---|
| 13 | stream. | 
|---|
| 14 |  | 
|---|
| 15 | **/ | 
|---|
| 16 | // ************************************************************************** | 
|---|
| 17 | class _time_format | 
|---|
| 18 | { | 
|---|
| 19 | friend std::ostream &operator<<(std::ostream &out, const _time_format &f); | 
|---|
| 20 | friend std::istream &operator>>(std::istream &in,  const _time_format &f); | 
|---|
| 21 | private: | 
|---|
| 22 | const char *ptr; /// pointer given to the iostreams | 
|---|
| 23 |  | 
|---|
| 24 | public: | 
|---|
| 25 | /// initialize ptr with what should be passed to the iostreams | 
|---|
| 26 | _time_format(const char *txt) : ptr(txt) { } | 
|---|
| 27 | }; | 
|---|
| 28 |  | 
|---|
| 29 | class Time : public boost::posix_time::ptime | 
|---|
| 30 | { | 
|---|
| 31 | public: | 
|---|
| 32 | /// A none-time, this can be used as a simple representation of an invalid time | 
|---|
| 33 | static const Time None; | 
|---|
| 34 |  | 
|---|
| 35 | /// A stream manipulator to set the output/input format | 
|---|
| 36 | static const _time_format fmt(const char *txt=0); | 
|---|
| 37 |  | 
|---|
| 38 | static const _time_format reset;   /// Remove the format description from the stream | 
|---|
| 39 | static const _time_format def;     /// set to format to the locale default | 
|---|
| 40 | static const _time_format std;     /// set to format to the iso standard | 
|---|
| 41 | static const _time_format sql;     /// set to format to the sql format | 
|---|
| 42 | static const _time_format ssql;    /// set to format to the sql format (without the fraction of seconds) | 
|---|
| 43 | static const _time_format iso;     /// set to format to the extended iso standard | 
|---|
| 44 | static const _time_format magic;   /// set to format to the MAGIC report format | 
|---|
| 45 | static const _time_format smagic;  /// set to format to the MAGIC report format (without the fraction of seconds) | 
|---|
| 46 |  | 
|---|
| 47 | /// Enum used in the instantisation of the class to change the inititalisation value | 
|---|
| 48 | enum init_t | 
|---|
| 49 | { | 
|---|
| 50 | none,  ///< Do not initialize the time | 
|---|
| 51 | utc,   ///< Initialize with UTC | 
|---|
| 52 | local  ///< Initialize with local time | 
|---|
| 53 | }; | 
|---|
| 54 |  | 
|---|
| 55 | private: | 
|---|
| 56 | /// Points to the famous 1/1/1970, the standard offset for unix times | 
|---|
| 57 | const static boost::gregorian::date fUnixOffset; | 
|---|
| 58 |  | 
|---|
| 59 | public: | 
|---|
| 60 | // Constructors | 
|---|
| 61 | Time(enum init_t type=utc); | 
|---|
| 62 | Time(const boost::date_time::special_values &val); | 
|---|
| 63 | Time(const time_t &tm, const int &ms); | 
|---|
| 64 | Time(const ptime &pt) : boost::posix_time::ptime(pt) { } | 
|---|
| 65 | Time(short year, unsigned char month, unsigned char day, | 
|---|
| 66 | unsigned char h=0, unsigned char m=0, unsigned char s=0, | 
|---|
| 67 | unsigned int us=0); | 
|---|
| 68 | Time(double mjd) { Mjd(mjd); } | 
|---|
| 69 |  | 
|---|
| 70 | // Convesion from and to a string | 
|---|
| 71 | std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const; | 
|---|
| 72 | void SetFromStr(const std::string &str, const char *fmt="%Y-%m-%d %H:%M:%S"); | 
|---|
| 73 |  | 
|---|
| 74 | std::string Iso() const; | 
|---|
| 75 |  | 
|---|
| 76 | // Conversion to and from MJD | 
|---|
| 77 | void Mjd(double mjd); | 
|---|
| 78 | double Mjd() const; | 
|---|
| 79 |  | 
|---|
| 80 | // Check validity | 
|---|
| 81 | bool IsValid() const   { return *this != boost::date_time::not_special; } | 
|---|
| 82 | bool operator!() const { return *this == boost::date_time::not_special; } | 
|---|
| 83 |  | 
|---|
| 84 | // Getter | 
|---|
| 85 | unsigned short Y() const  { return date().year(); } | 
|---|
| 86 | unsigned short M() const  { return date().month(); } | 
|---|
| 87 | unsigned short D() const  { return date().day(); } | 
|---|
| 88 |  | 
|---|
| 89 | unsigned short h() const  { return time_of_day().hours(); } | 
|---|
| 90 | unsigned short m() const  { return time_of_day().minutes(); } | 
|---|
| 91 | unsigned short s() const  { return time_of_day().seconds(); } | 
|---|
| 92 |  | 
|---|
| 93 | unsigned int   ms() const { return time_of_day().total_milliseconds()%1000; } | 
|---|
| 94 | unsigned int   us() const { return time_of_day().total_microseconds()%1000000; } | 
|---|
| 95 |  | 
|---|
| 96 | double SecondsOfDay() const; | 
|---|
| 97 |  | 
|---|
| 98 | time_t Time_t() const; | 
|---|
| 99 | double UnixTime() const; | 
|---|
| 100 | double RootTime() const; | 
|---|
| 101 |  | 
|---|
| 102 | int NightAsInt() const; | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | std::ostream &operator<<(std::ostream &out, const _time_format &f); | 
|---|
| 106 | std::istream &operator>>(std::istream &in,  const _time_format &f); | 
|---|
| 107 |  | 
|---|
| 108 | #endif | 
|---|