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

Last change on this file since 11323 was 11215, checked in by tbretz, 13 years ago
Added NightAsInt
File size: 3.9 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 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 // Conversion to and from MJD
75 void Mjd(double mjd);
76 double Mjd() const;
77
78 // Check validity
79 bool IsValid() const { return *this != boost::date_time::not_special; }
80 bool operator!() const { return *this == boost::date_time::not_special; }
81
82 // Getter
83 unsigned short Y() const { return date().year(); }
84 unsigned short M() const { return date().month(); }
85 unsigned short D() const { return date().day(); }
86
87 unsigned short h() const { return time_of_day().hours(); }
88 unsigned short m() const { return time_of_day().minutes(); }
89 unsigned short s() const { return time_of_day().seconds(); }
90
91 unsigned int ms() const { return time_of_day().total_milliseconds()%1000; }
92 unsigned int us() const { return time_of_day().total_microseconds()%1000000; }
93
94 double SecondsOfDay() const;
95
96 double UnixTime() const;
97 double RootTime() const;
98
99 int NightAsInt() const;
100};
101
102std::ostream &operator<<(std::ostream &out, const _time_format &f);
103std::istream &operator>>(std::istream &in, const _time_format &f);
104
105#endif
Note: See TracBrowser for help on using the repository browser.