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

Last change on this file since 15470 was 15190, checked in by tbretz, 12 years ago
Implemented function to get the time of the previous and next sun-rise. Updated NightAsInt to use that as reference instead of noon, to get consistent with the changes in the datalogger.
File size: 4.5 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 int &ms);
65 Time(const ptime &pt) : boost::posix_time::ptime(pt) { }
66 Time(short year, unsigned char month, unsigned char day,
67 unsigned char h=0, unsigned char m=0, unsigned char s=0,
68 unsigned int us=0);
69 Time(double mjd) { Mjd(mjd); }
70 Time(const std::string &str)
71 {
72 std::stringstream stream;
73 stream << str;
74 stream >> Time::iso >> *this;
75 }
76
77 // Convesion from and to a string
78 std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const;
79 void SetFromStr(const std::string &str, const char *fmt="%Y-%m-%d %H:%M:%S");
80
81 std::string Iso() const;
82
83 // Conversion to and from MJD
84 void Mjd(double mjd);
85 double Mjd() const;
86 double JD() const { return Mjd()+2400000.5; }
87
88 // Check validity
89 bool IsValid() const { return *this != boost::date_time::not_special; }
90 bool operator!() const { return *this == boost::date_time::not_special; }
91
92 // Getter
93 unsigned short Y() const { return date().year(); }
94 unsigned short M() const { return date().month(); }
95 unsigned short D() const { return date().day(); }
96
97 unsigned short h() const { return time_of_day().hours(); }
98 unsigned short m() const { return time_of_day().minutes(); }
99 unsigned short s() const { return time_of_day().seconds(); }
100
101 unsigned int ms() const { return time_of_day().total_milliseconds()%1000; }
102 unsigned int us() const { return time_of_day().total_microseconds()%1000000; }
103
104 double SecondsOfDay() const;
105
106 time_t Time_t() const;
107 double UnixTime() const;
108 double UnixDate() const;
109 double RootTime() const;
110 uint64_t JavaDate() const { return IsValid() ? uint64_t(UnixTime()*1000) : 0; }
111
112 std::string MinutesTo(const Time & = Time()) const;
113 std::string SecondsTo(const Time & = Time()) const;
114
115 Time GetPrevSunRise(double horizon) const;
116 Time GetNextSunRise(double horizon) const;
117
118 Time GetPrevSunRise() const;
119 Time GetNextSunRise() const;
120
121 int NightAsInt() const;
122};
123
124std::ostream &operator<<(std::ostream &out, const _time_format &f);
125std::istream &operator>>(std::istream &in, const _time_format &f);
126
127#endif
Note: See TracBrowser for help on using the repository browser.