CppCMS
posix_time.h
1 //
2 // Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOSTER_POSIX_TIME_H
9 #define BOOSTER_POSIX_TIME_H
10 
11 #include <booster/config.h>
12 #include <ctime>
13 #include <iosfwd>
14 #include <math.h>
15 
16 namespace booster {
17 
26  class BOOSTER_API ptime {
27  public:
31  explicit
32  ptime(long long seconds=0,int nano=0) :
33  sec(seconds),
34  nsec(nano)
35  {
36  normalize();
37  }
38 
42  long long get_seconds() const
43  {
44  return sec;
45  }
49  int get_nanoseconds() const
50  {
51  return nsec;
52  }
56  int get_milliseconds() const
57  {
58  return nsec / one_e6;
59  }
63  int get_microseconds() const
64  {
65  return nsec / one_e3;
66  }
67 
71  static long long seconds(ptime const &p)
72  {
73  return p.sec;
74  }
78  static ptime seconds(long long v)
79  {
80  return ptime(v);
81  }
82 
87  static long long milliseconds(ptime const &p)
88  {
89  return p.get_seconds() * one_e3 + p.get_milliseconds();
90  }
94  static ptime milliseconds(long long v)
95  {
96  return ptime(v/one_e3,v%one_e3 * one_e6);
97  }
102  static long long microseconds(ptime const &p)
103  {
104  return p.get_seconds() * one_e6 + p.get_nanoseconds() / one_e3;
105  }
109  static ptime microseconds(long long v)
110  {
111  return ptime(v/one_e6,v%one_e6 * one_e3);
112  }
117  static long long nanoseconds(ptime const &p)
118  {
119  return p.get_seconds() * one_e9 + p.get_nanoseconds();
120  }
124  static ptime nanoseconds(long long v)
125  {
126  return ptime(v/one_e9,v%one_e9);
127  }
131  static ptime minutes(long long v)
132  {
133  return ptime(v*60);
134  }
138  static long long minutes(ptime const &p)
139  {
140  return p.get_seconds() / 60;
141  }
145  static long long hours(ptime const &p)
146  {
147  return p.get_seconds() / 3600;
148  }
152  static ptime hours(long long v)
153  {
154  return ptime(v*3600);
155  }
159  static long long days(ptime const &p)
160  {
161  return p.get_seconds() / (3600*24);
162  }
166  static ptime days(long long v)
167  {
168  return ptime(v*(3600*24));
169  }
170 
174  static double to_number(ptime const &t)
175  {
176  return double(t.sec) + double(t.nsec) * 1e-9;
177  }
181  static ptime from_number(double d)
182  {
183  double sec = floor(d);
184  double subsec = d-sec;
185  long long seconds = static_cast<long long>(sec);
186  int nano = static_cast<int>(floor(subsec * 1e9));
187  if(nano < 0) nano = 0;
188  if(nano >= one_e9) nano = one_e9-1;
189  return ptime(seconds,nano);
190  }
191 
195  ptime operator+(ptime const &other) const
196  {
197  return ptime(sec+other.sec,nsec+other.nsec);
198  }
199 
204  ptime& operator+=(ptime const &other)
205  {
206  *this = ptime(sec+other.sec,nsec+other.nsec);
207  return *this;
208  }
209 
213  ptime operator-(ptime const &other) const
214  {
215  return ptime(sec-other.sec,nsec-other.nsec);
216  }
217 
222  ptime& operator-=(ptime const &other)
223  {
224  *this = ptime(sec-other.sec,nsec-other.nsec);
225  return *this;
226  }
227 
228  bool operator==(ptime const &other) const
229  {
230  return sec==other.sec && nsec == other.nsec;
231  }
232  bool operator!=(ptime const &other) const
233  {
234  return !((*this)==other);
235  }
236  bool operator<(ptime const &other) const
237  {
238  if(sec < other.sec)
239  return true;
240  if(sec > other.sec)
241  return false;
242  return nsec < other.nsec;
243  }
244  bool operator>(ptime const &other) const
245  {
246  return other < *this;
247  }
248  bool operator <= (ptime const &other) const
249  {
250  return !(*this > other);
251  }
252  bool operator >=(ptime const &other) const
253  {
254  return !(*this < other);
255  }
256 
260  static ptime local_time(std::tm const &v);
264  static ptime universal_time(std::tm const &v);
268  static std::tm local_time(ptime const &v);
272  static std::tm universal_time(ptime const &v);
273 
277  static ptime now();
278 
282  static ptime const zero;
283 
287  static void millisleep(long long v)
288  {
289  sleep(milliseconds(v));
290  }
294  static void nanosleep(long long v)
295  {
296  sleep(nanoseconds(v));
297  }
301  static void sleep(ptime const &v );
302 
303  private:
304  void normalize()
305  {
306  if(nsec > one_e9) {
307  sec += nsec / one_e9;
308  nsec = nsec % one_e9;
309  }
310  else if(nsec < 0) {
311  while(nsec < 0) {
312  nsec += one_e9;
313  sec -= 1;
314  }
315  }
316  }
317  static const int one_e3 = 1000;
318  static const int one_e6 = 1000000;
319  static const int one_e9 = 1000000000;
320  long long sec;
321  int nsec;
322  };
323 
328  BOOSTER_API std::ostream &operator<<(std::ostream &,ptime const &);
333  BOOSTER_API std::istream &operator>>(std::istream &,ptime &);
334 
335 }
336 
337 #endif
338 
static ptime milliseconds(long long v)
Definition: posix_time.h:94
BOOSTER_API std::tm universal_time(time_t pt)
int get_milliseconds() const
Definition: posix_time.h:56
static long long nanoseconds(ptime const &p)
Definition: posix_time.h:117
static ptime from_number(double d)
Definition: posix_time.h:181
static ptime days(long long v)
Definition: posix_time.h:166
static double to_number(ptime const &t)
Definition: posix_time.h:174
std::basic_string< CharType > normalize(std::basic_string< CharType > const &str, norm_type n=norm_default, std::locale const &loc=std::locale())
Definition: conversion.h:159
long long get_seconds() const
Definition: posix_time.h:42
static long long milliseconds(ptime const &p)
Definition: posix_time.h:87
static void millisleep(long long v)
Definition: posix_time.h:287
static long long minutes(ptime const &p)
Definition: posix_time.h:138
static ptime microseconds(long long v)
Definition: posix_time.h:109
int get_nanoseconds() const
Definition: posix_time.h:49
ptime(long long seconds=0, int nano=0)
Definition: posix_time.h:32
static ptime nanoseconds(long long v)
Definition: posix_time.h:124
ptime operator+(ptime const &other) const
Definition: posix_time.h:195
static long long days(ptime const &p)
Definition: posix_time.h:159
static long long hours(ptime const &p)
Definition: posix_time.h:145
static void nanosleep(long long v)
Definition: posix_time.h:294
static long long seconds(ptime const &p)
Definition: posix_time.h:71
BOOSTER_API std::istream & operator>>(std::istream &, ptime &)
ptime & operator+=(ptime const &other)
Definition: posix_time.h:204
static ptime const zero
Definition: posix_time.h:282
int get_microseconds() const
Definition: posix_time.h:63
This class represents POSIX time.
Definition: posix_time.h:26
static long long microseconds(ptime const &p)
Definition: posix_time.h:102
static ptime minutes(long long v)
Definition: posix_time.h:131
ptime & operator-=(ptime const &other)
Definition: posix_time.h:222
ptime operator-(ptime const &other) const
Definition: posix_time.h:213
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
BOOSTER_API std::tm local_time(time_t pt)
static ptime seconds(long long v)
Definition: posix_time.h:78
static ptime hours(long long v)
Definition: posix_time.h:152