source: trunk/FACT++/src/Time.h@ 11040

Last change on this file since 11040 was 10460, checked in by tbretz, 14 years ago
Moved common part of Mjd(), GetUnixTime() into SecondsOfDay(); renamed GetUnixTime to UnixTime() and added RootTime()
File size: 3.8 KB
Line 
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
11This class represents a stream manipulator. It is used to change the input
12or output format of a Time (or boost::posix_time) object to and from a
13stream.
14
15**/
16// **************************************************************************
17class _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);
21private:
22 const char *ptr; /// pointer given to the iostreams
23
24public:
25 /// initialize ptr with what should be passed to the iostreams
26 _time_format(const char *txt) : ptr(txt) { }
27};
28
29class Time : public boost::posix_time::ptime
30{
31public:
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
55private:
56 /// Points to the famous 1/1/1970, the standard offset for unix times
57 const static boost::gregorian::date fUnixOffset;
58
59public:
60 // Constructors
61 Time(enum init_t type=utc);
62 Time(const time_t &tm, const int &ms);
63 Time(const ptime &pt) : boost::posix_time::ptime(pt) { }
64 Time(short year, unsigned char month, unsigned char day,
65 unsigned char h=0, unsigned char m=0, unsigned char s=0,
66 unsigned int us=0);
67 Time(double mjd) { Mjd(mjd); }
68
69 // Convesion from and to a string
70 std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const;
71 void SetFromStr(const std::string &str, const char *fmt="%Y-%m-%d %H:%M:%S");
72
73 // Conversion to and from MJD
74 void Mjd(double mjd);
75 double Mjd() const;
76
77 // Check validity
78 bool IsValid() const { return *this != boost::date_time::not_special; }
79 bool operator!() const { return *this == boost::date_time::not_special; }
80
81 // Getter
82 unsigned short Y() const { return date().year(); }
83 unsigned short M() const { return date().month(); }
84 unsigned short D() const { return date().day(); }
85
86 unsigned short h() const { return time_of_day().hours(); }
87 unsigned short m() const { return time_of_day().minutes(); }
88 unsigned short s() const { return time_of_day().seconds(); }
89
90 unsigned int ms() const { return time_of_day().total_milliseconds()%1000; }
91 unsigned int us() const { return time_of_day().total_microseconds()%1000000; }
92
93 double SecondsOfDay() const;
94
95 double UnixTime() const;
96 double RootTime() const;
97};
98
99std::ostream &operator<<(std::ostream &out, const _time_format &f);
100std::istream &operator>>(std::istream &in, const _time_format &f);
101
102#endif
Note: See TracBrowser for help on using the repository browser.