FACT++  1.0
Time.h
Go to the documentation of this file.
1 #ifndef FACT_Time
2 #define FACT_Time
3 
4 #include <boost/date_time/local_time/local_time.hpp>
5 
6 // **************************************************************************
16 // **************************************************************************
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);
21 private:
22  const char *ptr;
23 
24 public:
26  _time_format(const char *txt) : ptr(txt) { }
27  std::string str() const { return ptr; }
28 };
29 
30 class Time : public boost::posix_time::ptime
31 {
32 public:
34  static const Time None;
35 
37  static const _time_format fmt(const char *txt=0);
38 
39  static const _time_format reset;
40  static const _time_format def;
41  static const _time_format std;
42  static const _time_format sql;
43  static const _time_format ssql;
44  static const _time_format iso;
45  static const _time_format magic;
46  static const _time_format smagic;
47 
49  enum init_t
50  {
51  none,
52  utc,
53  local
54  };
55 
56 public:
58  const static boost::gregorian::date fUnixOffset;
59 
60 public:
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;
117  Time GetNextSunRise(double horizon) const;
118 
119  Time GetPrevSunRise() const;
120  Time GetNextSunRise() const;
121 
122  uint32_t NightAsInt() const;
123 };
124 
125 std::ostream &operator<<(std::ostream &out, const _time_format &f);
126 std::istream &operator>>(std::istream &in, const _time_format &f);
127 
128 #endif
unsigned short M() const
Definition: Time.h:95
unsigned int ms() const
Definition: Time.h:102
static const _time_format sql
set to format to the iso standard
Definition: Time.h:42
uint64_t JavaDate() const
Definition: Time.h:111
Initialize with UTC.
Definition: Time.h:52
Time(double mjd)
Definition: Time.h:70
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
const char * ptr
Definition: Time.h:22
static const _time_format iso
set to format to the sql format (without the fraction of seconds)
Definition: Time.h:44
Time(const std::string &str)
Definition: Time.h:71
friend std::ostream & operator<<(std::ostream &out, const _time_format &f)
Definition: Time.cc:440
static const _time_format reset
Definition: Time.h:39
unsigned short Y() const
Definition: Time.h:94
static const _time_format ssql
set to format to the sql format
Definition: Time.h:43
static const _time_format magic
set to format to the extended iso standard
Definition: Time.h:45
double JD() const
Definition: Time.h:87
_time_format(const char *txt)
pointer given to the iostreams
Definition: Time.h:26
Helper to manipulate the input and output format of a time in a stream.
Definition: Time.h:17
std::string str() const
Definition: Time.h:27
static const boost::gregorian::date fUnixOffset
Points to the famous 1/1/1970, the standard offset for unix times.
Definition: Time.h:58
int type
static const _time_format def
Remove the format description from the stream.
Definition: Time.h:40
Time(const ptime &pt)
Definition: Time.h:66
unsigned short s() const
Definition: Time.h:100
bool IsValid() const
Definition: Time.h:90
static const Time None
A none-time, this can be used as a simple representation of an invalid time.
Definition: Time.h:34
bool operator!() const
Definition: Time.h:91
unsigned short D() const
Definition: Time.h:96
static const _time_format std
set to format to the locale default
Definition: Time.h:41
unsigned int us() const
Definition: Time.h:103
unsigned short m() const
Definition: Time.h:99
friend std::istream & operator>>(std::istream &in, const _time_format &f)
Definition: Time.cc:473
static const _time_format smagic
set to format to the MAGIC report format
Definition: Time.h:46
Do not initialize the time.
Definition: Time.h:51
init_t
set to format to the MAGIC report format (without the fraction of seconds)
Definition: Time.h:49
unsigned short h() const
Definition: Time.h:98