CppCMS
booster/posix_time.h
00001 //
00002 //  Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
00003 //
00004 //  Distributed under the Boost Software License, Version 1.0. (See
00005 //  accompanying file LICENSE_1_0.txt or copy at
00006 //  http://www.boost.org/LICENSE_1_0.txt)
00007 //
00008 #ifndef BOOSTER_POSIX_TIME_H
00009 #define BOOSTER_POSIX_TIME_H
00010 
00011 #include <booster/config.h>
00012 #include <ctime>
00013 #include <iosfwd>
00014 #include <math.h>
00015 
00016 namespace booster {
00017 
00026         class BOOSTER_API ptime {
00027         public:
00031                 explicit 
00032                 ptime(long long seconds=0,int nano=0) : 
00033                         sec(seconds),
00034                         nsec(nano) 
00035                 {
00036                         normalize();
00037                 }
00038 
00042                 long long get_seconds() const
00043                 {
00044                         return sec;
00045                 }
00049                 int get_nanoseconds() const
00050                 {
00051                         return nsec;
00052                 }
00056                 int get_milliseconds() const
00057                 {
00058                         return nsec / one_e6;
00059                 }
00063                 int get_microseconds() const
00064                 {
00065                         return nsec / one_e3;
00066                 }
00067 
00071                 static long long seconds(ptime const &p)
00072                 {
00073                         return p.sec;
00074                 }
00078                 static ptime seconds(long long v)
00079                 {
00080                         return ptime(v);
00081                 }
00082 
00087                 static long long milliseconds(ptime const &p) 
00088                 {
00089                         return p.get_seconds() * one_e3 + p.get_milliseconds();
00090                 }
00094                 static ptime milliseconds(long long v)
00095                 {
00096                         return ptime(v/one_e3,v%one_e3 * one_e6);
00097                 }
00102                 static long long microseconds(ptime const &p)
00103                 {
00104                         return p.get_seconds() * one_e6 + p.get_nanoseconds() / one_e3;
00105                 }
00109                 static ptime microseconds(long long v)
00110                 {
00111                         return ptime(v/one_e6,v%one_e6 * one_e3);
00112                 }
00117                 static long long nanoseconds(ptime const &p)
00118                 {
00119                         return p.get_seconds() * one_e9 + p.get_nanoseconds();
00120                 }
00124                 static ptime nanoseconds(long long v)
00125                 {
00126                         return ptime(v/one_e9,v%one_e9);
00127                 }
00131                 static ptime minutes(long long v)
00132                 {
00133                         return ptime(v*60);
00134                 }
00138                 static long long minutes(ptime const &p)
00139                 {
00140                         return p.get_seconds() / 60;
00141                 }
00145                 static long long hours(ptime const &p)
00146                 {
00147                         return p.get_seconds() / 3600;
00148                 }
00152                 static ptime hours(long long v)
00153                 {
00154                         return ptime(v*3600);
00155                 }
00159                 static long long days(ptime const &p)
00160                 {
00161                         return p.get_seconds() / (3600*24);
00162                 }
00166                 static ptime days(long long v)
00167                 {
00168                         return ptime(v*(3600*24));
00169                 }
00170         
00174                 static double to_number(ptime const &t)
00175                 {
00176                         return double(t.sec) + double(t.nsec) * 1e-9;
00177                 }
00181                 static ptime from_number(double d)
00182                 {
00183                         double sec = floor(d);
00184                         double subsec = d-sec;
00185                         long long seconds = static_cast<long long>(sec);
00186                         int nano = static_cast<int>(floor(subsec * 1e9));
00187                         if(nano < 0) nano = 0;
00188                         if(nano >= one_e9) nano = one_e9-1;
00189                         return ptime(seconds,nano);
00190                 }
00191                 
00195                 ptime operator+(ptime const &other) const
00196                 {
00197                         return ptime(sec+other.sec,nsec+other.nsec);
00198                 }
00202                 ptime operator-(ptime const &other) const
00203                 {
00204                         return ptime(sec-other.sec,nsec-other.nsec);
00205                 }
00206 
00207                 bool operator==(ptime const &other) const
00208                 {
00209                         return sec==other.sec && nsec == other.nsec;
00210                 }
00211                 bool operator!=(ptime const &other) const
00212                 {
00213                         return !((*this)==other);
00214                 }
00215                 bool operator<(ptime const &other) const
00216                 {
00217                         if(sec < other.sec)
00218                                 return true;
00219                         if(sec > other.sec)
00220                                 return false;
00221                         return nsec < other.nsec;
00222                 }
00223                 bool operator>(ptime const &other) const
00224                 {
00225                         return other < *this;
00226                 }
00227                 bool operator <= (ptime const &other) const
00228                 {
00229                         return !(*this > other);
00230                 }
00231                 bool operator >=(ptime const &other) const
00232                 {
00233                         return !(*this < other);
00234                 }
00235                 
00239                 static ptime local_time(std::tm const &v);
00243                 static ptime universal_time(std::tm const &v);
00247                 static std::tm local_time(ptime const &v);
00251                 static std::tm universal_time(ptime const &v);
00252                 
00256                 static ptime now();
00257 
00261                 static ptime const zero;
00262 
00266                 static void millisleep(long long v)
00267                 {
00268                         sleep(milliseconds(v));
00269                 }
00273                 static void nanosleep(long long v)
00274                 {
00275                         sleep(nanoseconds(v));
00276                 }
00280                 static void sleep(ptime const &v );
00281         
00282         private:
00283                 void normalize()
00284                 {
00285                         if(nsec > one_e9) {
00286                                 sec += nsec / one_e9;
00287                                 nsec = nsec % one_e9;
00288                         }
00289                         else if(nsec < 0) {
00290                                 while(nsec < 0) {
00291                                         nsec += one_e9;
00292                                         sec -= 1;
00293                                 }
00294                         }
00295                 }
00296                 static const int one_e3 = 1000;
00297                 static const int one_e6 = 1000000;
00298                 static const int one_e9 = 1000000000;
00299                 long long sec;
00300                 int nsec;
00301         };
00302 
00307         BOOSTER_API std::ostream &operator<<(std::ostream &,ptime const &);
00312         BOOSTER_API std::istream &operator>>(std::istream &,ptime &);
00313 
00314 }
00315 
00316 #endif
00317