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

Last change on this file since 10218 was 10183, checked in by tbretz, 11 years ago
New import.
File size: 3.4 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 iso; /// set to format to the extended iso standard
43 static const _time_format magic; /// set to format to the MAGIC report format
44
45 /// Enum used in the instantisation of the class to change the inititalisation value
46 enum init_t
47 {
48 none, ///< Do not initialize the time
49 utc, ///< Initialize with UTC
50 local ///< Initialize with local time
51 };
52
53private:
54 /// Points to the famous 1/1/1970, the standard offset for unix times
55 const static boost::gregorian::date fUnixOffset;
56
57public:
58 // Constructors
59 Time(enum init_t type=utc);
60 Time(const time_t &tm, const int &ms);
61 Time(const ptime &pt) : boost::posix_time::ptime(pt) { }
62 Time(short year, unsigned char month, unsigned char day,
63 unsigned char h=0, unsigned char m=0, unsigned char s=0,
64 unsigned int us=0);
65
66 // Convesion from and to a string
67 std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S.%f") const;
68 void SetFromStr(const std::string &str, const char *fmt="%Y-%m-%d %H:%M:%S.%f");
69
70 // Conversion to and from MJD
71 void Mjd(double mjd);
72 double Mjd() const;
73
74 // Check validity
75 bool IsValid() const { return *this != boost::date_time::not_special; }
76 bool operator!() const { return *this == boost::date_time::not_special; }
77
78 // Getter
79 unsigned short Y() const { return date().year(); }
80 unsigned short M() const { return date().month(); }
81 unsigned short D() const { return date().day(); }
82
83 unsigned short h() const { return time_of_day().hours(); }
84 unsigned short m() const { return time_of_day().minutes(); }
85 unsigned short s() const { return time_of_day().seconds(); }
86
87 unsigned int ms() const { return time_of_day().total_milliseconds()%1000; }
88 unsigned int us() const { return time_of_day().total_microseconds()%1000000; }
89};
90
91std::ostream &operator<<(std::ostream &out, const _time_format &f);
92std::istream &operator>>(std::istream &in, const _time_format &f);
93
94#endif
Note: See TracBrowser for help on using the repository browser.