1 | // **************************************************************************
|
---|
2 | /** @class Time
|
---|
3 |
|
---|
4 | @brief Adds some functionality to boost::posix_time::ptime for our needs
|
---|
5 |
|
---|
6 | This is basically a wrapper around boost::posix_time::ptime which is made
|
---|
7 | to adapt the functionality to our needs. Time can store the current
|
---|
8 | data and time with a precision up to nanoseconds if provided by the
|
---|
9 | undrlaying system, otherwise microsecond precision is used.
|
---|
10 |
|
---|
11 | It main purpose is to provide needed constructors and simplyfy the
|
---|
12 | conversion of dates and times from and to a string/stream.
|
---|
13 |
|
---|
14 | Note that posix_time (as Posix times have) has a limited range. You cannot
|
---|
15 | use it for example for very early years of the last century.
|
---|
16 |
|
---|
17 | @section Examples
|
---|
18 |
|
---|
19 | - An example can be found in \ref time.cc
|
---|
20 |
|
---|
21 | @section References
|
---|
22 |
|
---|
23 | - <A HREF="http://www.boost.org/doc/libs/1_45_0/doc/html/date_time.html">BOOST++ date_time (V1.45.0)</A>
|
---|
24 |
|
---|
25 | **/
|
---|
26 | // **************************************************************************
|
---|
27 | #include "Time.h"
|
---|
28 |
|
---|
29 | using namespace std;
|
---|
30 | using namespace boost::posix_time;
|
---|
31 |
|
---|
32 | const boost::gregorian::date Time::fUnixOffset(1970, 1, 1);
|
---|
33 |
|
---|
34 | const Time Time::None(Time::none);
|
---|
35 |
|
---|
36 | // strftime
|
---|
37 | const _time_format Time::reset = 0;
|
---|
38 | const _time_format Time::def = "%c";
|
---|
39 | const _time_format Time::std = "%x %X%F";
|
---|
40 | const _time_format Time::sql = "%Y-%m-%d %H:%M:%S.%f";
|
---|
41 | const _time_format Time::ssql = "%Y-%m-%d %H:%M:%S";
|
---|
42 | const _time_format Time::iso = "%Y%m%dT%H%M%S%F%q";
|
---|
43 | const _time_format Time::magic = "%Y %m %d %H %M %S %f";
|
---|
44 | const _time_format Time::smagic = "%Y %m %d %H %M %S";
|
---|
45 |
|
---|
46 | /*
|
---|
47 | using date_time::special_values;
|
---|
48 | using date_time::not_special;
|
---|
49 | using date_time::neg_infin;
|
---|
50 | using date_time::pos_infin;
|
---|
51 | using date_time::not_a_date_time;
|
---|
52 | using date_time::max_date_time;
|
---|
53 | using date_time::min_date_time;
|
---|
54 | */
|
---|
55 |
|
---|
56 | // --------------------------------------------------------------------------
|
---|
57 | //
|
---|
58 | //! Construct a Time object with either UTC or local time, or without any
|
---|
59 | //! particular time.
|
---|
60 | //!
|
---|
61 | //! @param typ
|
---|
62 | //! enum as defined in Time::init_t
|
---|
63 | //
|
---|
64 | Time::Time(enum init_t typ)
|
---|
65 | {
|
---|
66 | switch (typ)
|
---|
67 | {
|
---|
68 | case utc:
|
---|
69 | *this = microsec_clock::universal_time();
|
---|
70 | break;
|
---|
71 | case local:
|
---|
72 | *this = microsec_clock::local_time();
|
---|
73 | break;
|
---|
74 | case none:
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | // --------------------------------------------------------------------------
|
---|
80 | //
|
---|
81 | //! Construct a Time object from seconds since 1970/1/1 and number of
|
---|
82 | //! milliseconds, as for example returned by gettimeofday()
|
---|
83 | //!
|
---|
84 | //! @param tm
|
---|
85 | //! seconds since 1970/1/1
|
---|
86 | //!
|
---|
87 | //! @param millisec
|
---|
88 | //! number of milliseconds
|
---|
89 | //
|
---|
90 | Time::Time(const time_t &tm, const int &millisec)
|
---|
91 | : ptime(fUnixOffset, time_duration(0, 0, tm, millisec))
|
---|
92 | {
|
---|
93 | }
|
---|
94 |
|
---|
95 | // --------------------------------------------------------------------------
|
---|
96 | //
|
---|
97 | //! Construct a Time from a date and time.
|
---|
98 | //!
|
---|
99 | //! @param year, month, day, hh, mm, ss, microsec
|
---|
100 | //! A full date and time down to microsecond precision. From the end
|
---|
101 | //! arguments can be omitted.
|
---|
102 | //!
|
---|
103 | Time::Time(short year, unsigned char month, unsigned char day,
|
---|
104 | unsigned char hh, unsigned char mm, unsigned char ss, unsigned int microsec)
|
---|
105 | // Last argument is fractional_seconds ( correct with num_fractional_digits() )
|
---|
106 | : ptime(boost::gregorian::date(year, month, day),
|
---|
107 | time_duration(hh, mm, ss, microsec*pow(10, time_of_day().num_fractional_digits()-6)))
|
---|
108 | {
|
---|
109 | }
|
---|
110 |
|
---|
111 | // --------------------------------------------------------------------------
|
---|
112 | //
|
---|
113 | //! Set the Time object to a given MJD. Note that this involves
|
---|
114 | //! conversion from double. So converting forth and back many many
|
---|
115 | //! times might results in drifts.
|
---|
116 | //!
|
---|
117 | //! @param mjd
|
---|
118 | //! Modified Julian Date
|
---|
119 | //!
|
---|
120 | void Time::Mjd(double mjd)
|
---|
121 | {
|
---|
122 | // Convert MJD to seconds since offset
|
---|
123 | mjd -= 40587;
|
---|
124 | mjd *= 24*60*60;
|
---|
125 |
|
---|
126 | const int exp = time_of_day().num_fractional_digits();
|
---|
127 | const double frac = fmod(mjd, 1)*pow(10, exp);
|
---|
128 |
|
---|
129 | *this = ptime(fUnixOffset, time_duration(0, 0, mjd, frac));
|
---|
130 | }
|
---|
131 |
|
---|
132 | double_t Time::SecondsOfDay() const
|
---|
133 | {
|
---|
134 | const time_duration tod = time_of_day();
|
---|
135 |
|
---|
136 | const int exp = tod.num_fractional_digits();
|
---|
137 |
|
---|
138 | const double frac = tod.fractional_seconds()/pow(10, exp);
|
---|
139 | const double sec = tod.total_seconds()+frac;
|
---|
140 |
|
---|
141 | return sec;
|
---|
142 | }
|
---|
143 |
|
---|
144 | // --------------------------------------------------------------------------
|
---|
145 | //
|
---|
146 | //! Get the current MJD. Note that this involves
|
---|
147 | //! conversion to double. So converting forth and back many many
|
---|
148 | //! times might results in drifts.
|
---|
149 | //!
|
---|
150 | //! @returns
|
---|
151 | //! Modified Julian Date
|
---|
152 | //!
|
---|
153 | double Time::Mjd() const
|
---|
154 | {
|
---|
155 | return date().modjulian_day()+SecondsOfDay()/(24*60*60);
|
---|
156 |
|
---|
157 | /*
|
---|
158 | const time_duration mjd = *this - ptime(fUnixOffset);
|
---|
159 | const double sec = mjd.total_seconds()+mjd.fractional_seconds()/1e6;
|
---|
160 | return sec/(24*60*60)+40587;
|
---|
161 | */
|
---|
162 | }
|
---|
163 |
|
---|
164 | // --------------------------------------------------------------------------
|
---|
165 | //
|
---|
166 | // Return seconds since 1970/1/1
|
---|
167 | //
|
---|
168 | double Time::UnixTime() const
|
---|
169 | {
|
---|
170 | return (date().modjulian_day()-40587)*24*60*60 + SecondsOfDay();
|
---|
171 | }
|
---|
172 |
|
---|
173 | double Time::RootTime() const
|
---|
174 | {
|
---|
175 | return (date().modjulian_day()-49718)*24*60*60 + SecondsOfDay();
|
---|
176 | }
|
---|
177 |
|
---|
178 | // --------------------------------------------------------------------------
|
---|
179 | //
|
---|
180 | //! Returns a string with the contents of the Time object formated
|
---|
181 | //! as defined in format.
|
---|
182 | //!
|
---|
183 | //! @param format
|
---|
184 | //! format description of the string to be returned. For details
|
---|
185 | //! see the boost documentation or the man page of strftime
|
---|
186 | //!
|
---|
187 | //! @returns
|
---|
188 | //! A string with the time formatted as requested. Note some special
|
---|
189 | //! strings might be returned in case the time is invalid.
|
---|
190 | //
|
---|
191 | string Time::GetAsStr(const char *format) const
|
---|
192 | {
|
---|
193 | stringstream out;
|
---|
194 | out << Time::fmt(format) << *this;
|
---|
195 | return out.str();
|
---|
196 | }
|
---|
197 |
|
---|
198 | // --------------------------------------------------------------------------
|
---|
199 | //
|
---|
200 | //! Sets the time of the Time object to a time corresponding to
|
---|
201 | //! the one given as argument. It is evaluated according to the given
|
---|
202 | //! format.
|
---|
203 | //!
|
---|
204 | //! @param str
|
---|
205 | //! The time as a string which should be converted to the Time object
|
---|
206 | //!
|
---|
207 | //! @param format
|
---|
208 | //! format description of the string to be returned. For details
|
---|
209 | //! see the boost documentation or the man page of strftime
|
---|
210 | //!
|
---|
211 | void Time::SetFromStr(const string &str, const char *format)
|
---|
212 | {
|
---|
213 | // FIXME: exception handline
|
---|
214 | stringstream stream;
|
---|
215 | stream << str;
|
---|
216 | stream >> Time::fmt(format) >> *this;
|
---|
217 | }
|
---|
218 |
|
---|
219 | // --------------------------------------------------------------------------
|
---|
220 | //
|
---|
221 | //! A stream manipulator which sets the streams Time output format
|
---|
222 | //! as defined in the argument.
|
---|
223 | //!
|
---|
224 | //! @param format
|
---|
225 | //! format description of the manipulator be returned. For details
|
---|
226 | //! see the boost documentation or the man page of strftime
|
---|
227 | //!
|
---|
228 | //! @returns
|
---|
229 | //! a stream manipulator for the given format
|
---|
230 | //!
|
---|
231 | const _time_format Time::fmt(const char *format)
|
---|
232 | {
|
---|
233 | return format;
|
---|
234 | }
|
---|
235 |
|
---|
236 | // --------------------------------------------------------------------------
|
---|
237 | //
|
---|
238 | //! Sets the locale discription of the stream (the way how a time is
|
---|
239 | //! output) to the format defined by the given manipulator.
|
---|
240 | //!
|
---|
241 | //! Example:
|
---|
242 | //! \code
|
---|
243 | //! Time t();
|
---|
244 | //! cout << Time::fmt("%Y:%m:%d %H:%M:%S.%f") << t << endl;
|
---|
245 | //! \endcode
|
---|
246 | //!
|
---|
247 | //! @param out
|
---|
248 | //! Reference to the stream
|
---|
249 | //!
|
---|
250 | //! @param f
|
---|
251 | //! Time format described by a manipulator
|
---|
252 | //!
|
---|
253 | //! @returns
|
---|
254 | //! A reference to the stream
|
---|
255 | //!
|
---|
256 | ostream &operator<<(ostream &out, const _time_format &f)
|
---|
257 | {
|
---|
258 | const locale loc(locale::classic(),
|
---|
259 | f.ptr==0 ? 0 : new time_facet(f.ptr));
|
---|
260 |
|
---|
261 | out.imbue(loc);
|
---|
262 |
|
---|
263 | return out;
|
---|
264 | }
|
---|
265 |
|
---|
266 | // --------------------------------------------------------------------------
|
---|
267 | //
|
---|
268 | //! Sets the locale discription of the stream (the way how a time is
|
---|
269 | //! input) to the format defined by the given manipulator.
|
---|
270 | //!
|
---|
271 | //! Example:
|
---|
272 | //! \code
|
---|
273 | //! stringstream s;
|
---|
274 | //! s << "09.09.1974 21:59";
|
---|
275 | //!
|
---|
276 | //! Time t;
|
---|
277 | //! s >> Time::fmt("%d.%m.%Y %H:%M") >> t;
|
---|
278 | //! \endcode
|
---|
279 | //!
|
---|
280 | //! @param in
|
---|
281 | //! Reference to the stream
|
---|
282 | //!
|
---|
283 | //! @param f
|
---|
284 | //! Time format described by a manipulator
|
---|
285 | //!
|
---|
286 | //! @returns
|
---|
287 | //! A reference to the stream
|
---|
288 | //!
|
---|
289 | istream &operator>>(istream &in, const _time_format &f)
|
---|
290 | {
|
---|
291 | const locale loc(locale::classic(),
|
---|
292 | f.ptr==0 ? 0 : new time_input_facet(f.ptr));
|
---|
293 |
|
---|
294 | in.imbue(loc);
|
---|
295 |
|
---|
296 | return in;
|
---|
297 | }
|
---|
298 |
|
---|