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

Last change on this file since 19672 was 19430, checked in by tbretz, 6 years ago
Added the GetNext/Prev also for SunSet.
File size: 4.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 std::string str() const { return ptr; }
28};
29
30class Time : public boost::posix_time::ptime
31{
32public:
33 /// A none-time, this can be used as a simple representation of an invalid time
34 static const Time None;
35
36 /// A stream manipulator to set the output/input format
37 static const _time_format fmt(const char *txt=0);
38
39 static const _time_format reset; /// Remove the format description from the stream
40 static const _time_format def; /// set to format to the locale default
41 static const _time_format std; /// set to format to the iso standard
42 static const _time_format sql; /// set to format to the sql format
43 static const _time_format ssql; /// set to format to the sql format (without the fraction of seconds)
44 static const _time_format iso; /// set to format to the extended iso standard
45 static const _time_format magic; /// set to format to the MAGIC report format
46 static const _time_format smagic; /// set to format to the MAGIC report format (without the fraction of seconds)
47
48 /// Enum used in the instantisation of the class to change the inititalisation value
49 enum init_t
50 {
51 none, ///< Do not initialize the time
52 utc, ///< Initialize with UTC
53 local ///< Initialize with local time
54 };
55
56public:
57 /// Points to the famous 1/1/1970, the standard offset for unix times
58 const static boost::gregorian::date fUnixOffset;
59
60public:
61 // Constructors
62 Time(enum init_t type=utc);
63 Time(const boost::date_time::special_values &val);
64 Time(const time_t &tm, const suseconds_t &us);
65 Time(const timeval &tm);
66 Time(const ptime &pt) : boost::posix_time::ptime(pt) { }
67 Time(short year, unsigned char month, unsigned char day,
68 unsigned char h=0, unsigned char m=0, unsigned char s=0,
69 unsigned int us=0);
70 Time(double mjd) { Mjd(mjd); }
71 Time(const std::string &str)
72 {
73 std::stringstream stream;
74 stream << str;
75 stream >> Time::iso >> *this;
76 }
77
78 // Convesion from and to a string
79 std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const;
80 void SetFromStr(const std::string &str, const char *fmt="%Y-%m-%d %H:%M:%S");
81
82 std::string Iso() const;
83
84 // Conversion to and from MJD
85 void Mjd(double mjd);
86 double Mjd() const;
87 double JD() const { return Mjd()+2400000.5; }
88
89 // Check validity
90 bool IsValid() const { return *this != boost::date_time::not_special; }
91 bool operator!() const { return *this == boost::date_time::not_special; }
92
93 // Getter
94 unsigned short Y() const { return date().year(); }
95 unsigned short M() const { return date().month(); }
96 unsigned short D() const { return date().day(); }
97
98 unsigned short h() const { return time_of_day().hours(); }
99 unsigned short m() const { return time_of_day().minutes(); }
100 unsigned short s() const { return time_of_day().seconds(); }
101
102 unsigned int ms() const { return time_of_day().total_milliseconds()%1000; }
103 unsigned int us() const { return time_of_day().total_microseconds()%1000000; }
104
105 double SecondsOfDay() const;
106
107 time_t Time_t() const;
108 double UnixTime() const;
109 double UnixDate() const;
110 double RootTime() const;
111 uint64_t JavaDate() const { return IsValid() ? uint64_t(UnixTime()*1000) : 0; }
112
113 std::string MinutesTo(const Time & = Time()) const;
114 std::string SecondsTo(const Time & = Time()) const;
115
116 Time GetPrevSunRise(double horizon, const std::string &obs="") const;
117 Time GetNextSunRise(double horizon, const std::string &obs="") const;
118
119 Time GetPrevSunRise(const std::string &obs="") const;
120 Time GetNextSunRise(const std::string &obs="") const;
121
122 Time GetPrevSunSet(double horizon, const std::string &obs="") const;
123 Time GetNextSunSet(double horizon, const std::string &obs="") const;
124
125 Time GetPrevSunSet(const std::string &obs="") const;
126 Time GetNextSunSet(const std::string &obs="") const;
127
128 uint32_t NightAsInt(const std::string &obs="") const;
129};
130
131std::ostream &operator<<(std::ostream &out, const _time_format &f);
132std::istream &operator>>(std::istream &in, const _time_format &f);
133
134#endif
Note: See TracBrowser for help on using the repository browser.