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

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