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  }
202  ptime operator-(ptime const &other) const
203  {
204  return ptime(sec-other.sec,nsec-other.nsec);
205  }
206 
207  bool operator==(ptime const &other) const
208  {
209  return sec==other.sec && nsec == other.nsec;
210  }
211  bool operator!=(ptime const &other) const
212  {
213  return !((*this)==other);
214  }
215  bool operator<(ptime const &other) const
216  {
217  if(sec < other.sec)
218  return true;
219  if(sec > other.sec)
220  return false;
221  return nsec < other.nsec;
222  }
223  bool operator>(ptime const &other) const
224  {
225  return other < *this;
226  }
227  bool operator <= (ptime const &other) const
228  {
229  return !(*this > other);
230  }
231  bool operator >=(ptime const &other) const
232  {
233  return !(*this < other);
234  }
235 
239  static ptime local_time(std::tm const &v);
243  static ptime universal_time(std::tm const &v);
247  static std::tm local_time(ptime const &v);
251  static std::tm universal_time(ptime const &v);
252 
256  static ptime now();
257 
261  static ptime const zero;
262 
266  static void millisleep(long long v)
267  {
268  sleep(milliseconds(v));
269  }
273  static void nanosleep(long long v)
274  {
275  sleep(nanoseconds(v));
276  }
280  static void sleep(ptime const &v );
281 
282  private:
283  void normalize()
284  {
285  if(nsec > one_e9) {
286  sec += nsec / one_e9;
287  nsec = nsec % one_e9;
288  }
289  else if(nsec < 0) {
290  while(nsec < 0) {
291  nsec += one_e9;
292  sec -= 1;
293  }
294  }
295  }
296  static const int one_e3 = 1000;
297  static const int one_e6 = 1000000;
298  static const int one_e9 = 1000000000;
299  long long sec;
300  int nsec;
301  };
302 
307  BOOSTER_API std::ostream &operator<<(std::ostream &,ptime const &);
312  BOOSTER_API std::istream &operator>>(std::istream &,ptime &);
313 
314 }
315 
316 #endif
317 
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:266
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:273
static long long seconds(ptime const &p)
Definition: posix_time.h:71
BOOSTER_API std::istream & operator>>(std::istream &, ptime &)
static ptime const zero
Definition: posix_time.h:261
int get_microseconds() const
Definition: posix_time.h:63
This class represents POSIX time.
Definition: posix_time.h:26
bool operator<(string_key const &l, char const *r)
Definition: string_key.h:431
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) const
Definition: posix_time.h:202
bool operator>(string_key const &l, char const *r)
Definition: string_key.h:462
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