NCBI C++ ToolKit
ns_precise_time.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef NS_PRECISE_TIME__HPP
2 #define NS_PRECISE_TIME__HPP
3 /* $Id: ns_precise_time.hpp 84641 2018-11-26 13:25:46Z satskyse $
4  * ===========================================================================
5  *
6  * PUBLIC DOMAIN NOTICE
7  * National Center for Biotechnology Information
8  *
9  * This software/database is a "United States Government Work" under the
10  * terms of the United States Copyright Act. It was written as part of
11  * the author's official duties as a United States Government employee and
12  * thus cannot be copyrighted. This software/database is freely available
13  * to the public for use. The National Library of Medicine and the U.S.
14  * Government have not placed any restriction on its use or reproduction.
15  *
16  * Although all reasonable efforts have been taken to ensure the accuracy
17  * and reliability of the software and data, the NLM and the U.S.
18  * Government do not and cannot warrant the performance or results that
19  * may be obtained by using this software or data. The NLM and the U.S.
20  * Government disclaim all warranties, express or implied, including
21  * warranties of performance, merchantability or fitness for any particular
22  * purpose.
23  *
24  * Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Authors: Pavel Ivanov
29  * Slightly modified by Sergey Satskiy to fit NetSchedule
30  *
31  * File Description: Precise time handling
32  */
33 
34 
35 #include <corelib/ncbi_limits.hpp>
36 #include <corelib/ncbitime.hpp>
37 #include <sys/time.h>
38 
39 
41 
42 
43 enum {
45  kUSecsPerMSec = 1000,
46  kNSecsPerUSec = 1000,
50 };
51 
52 
53 class CNSPreciseTime : public timespec
54 {
55  public:
56  static CNSPreciseTime Current(void)
58  clock_gettime(CLOCK_REALTIME, &result);
59  return result; }
60  static CNSPreciseTime Never(void)
63  result.tv_nsec = kNSecsPerSecond - 1;
64  return result;
65  }
66 
68  { tv_sec = 0;
69  tv_nsec = 0; }
70  CNSPreciseTime(time_t sec)
71  { tv_sec = sec;
72  tv_nsec = 0; }
73  CNSPreciseTime(double time)
74  { tv_sec = int(time);
75  tv_nsec = int( (time - tv_sec) * kNSecsPerSecond ); }
76  CNSPreciseTime(unsigned int sec, unsigned int nsec)
77  { tv_sec = sec;
78  tv_nsec = nsec; }
79 
81  { tv_sec = other.tv_sec;
82  tv_nsec = other.tv_nsec; }
84  { tv_sec = rhs.tv_sec;
85  tv_nsec = rhs.tv_nsec;
86  return *this; }
89 
90  time_t & Sec(void)
91  { return tv_sec; }
92  time_t Sec(void) const
93  { return tv_sec; }
94  long & NSec(void)
95  { return tv_nsec; }
96  long NSec(void) const
97  { return tv_nsec; }
98  int Compare(const CNSPreciseTime & t) const
99  {
100  if (tv_sec < t.tv_sec)
101  return -1;
102  else if (tv_sec > t.tv_sec)
103  return 1;
104  else if (tv_nsec < t.tv_nsec)
105  return -1;
106  else if (tv_nsec > t.tv_nsec)
107  return 1;
108  return 0;
109  }
110 
112  {
113  tv_sec += t.tv_sec;
114  tv_nsec += t.tv_nsec;
115  if (tv_nsec >= kNSecsPerSecond) {
116  ++tv_sec;
117  tv_nsec -= kNSecsPerSecond;
118  }
119  return *this;
120  }
122  {
123  tv_sec -= t.tv_sec;
124  if (tv_nsec >= t.tv_nsec) {
125  tv_nsec -= t.tv_nsec;
126  }
127  else {
128  --tv_sec;
129  tv_nsec += kNSecsPerSecond;
130  tv_nsec -= t.tv_nsec;
131  }
132  return *this;
133  }
134  bool operator> (const CNSPreciseTime & t) const
135  { return Compare(t) > 0; }
136  bool operator>= (const CNSPreciseTime & t) const
137  { return Compare(t) >= 0; }
138  bool operator< (const CNSPreciseTime & t) const
139  { return Compare(t) < 0; }
140  bool operator<= (const CNSPreciseTime & t) const
141  { return Compare(t) <= 0; }
142 
143  operator double () const
144  { return (double)tv_sec + ((double)tv_nsec / (double)kNSecsPerSecond); }
145 
146 };
147 
148 inline
150  const CNSPreciseTime & rhs)
151 {
152  CNSPreciseTime result(lhs);
153  return result += rhs;
154 }
155 
156 inline
158  const CNSPreciseTime & rhs)
159 {
160  CNSPreciseTime result(lhs);
161  return result -= rhs;
162 }
163 
164 inline
165 bool operator== (const CNSPreciseTime & lhs,
166  const CNSPreciseTime & rhs)
167 {
168  // Faster, than to use <
169  return lhs.tv_sec == rhs.tv_sec &&
170  lhs.tv_nsec == rhs.tv_nsec;
171 }
172 
173 inline
174 bool operator!= (const CNSPreciseTime & lhs,
175  const CNSPreciseTime & rhs)
176 {
177  return !(lhs == rhs);
178 }
179 
180 
181 inline
183 {
184  CTime converted(t.Sec());
185  long usec = t.NSec() / kNSecsPerUSec;
186 
187  converted.ToLocalTime();
188  if (usec == 0)
189  return converted.AsString() + ".0";
190 
191  char buffer[32];
192  sprintf(buffer, ".%06lu", usec);
193  return converted.AsString() + buffer;
194 }
195 
196 inline
198 {
199  long usec = t.NSec() / kNSecsPerUSec;
200  char buffer[32];
201 
202  if (usec == 0)
203  sprintf(buffer, "%lu.0", t.Sec());
204  else
205  sprintf(buffer, "%lu.%06lu", t.Sec(), usec);
206  return buffer;
207 }
208 
209 
212 
214 
215 #endif /* NS_PRECISE_TIME__HPP */
216 
bool operator<=(const CNSPreciseTime &t) const
long & NSec(void)
CNSPreciseTime(time_t sec)
CNSPreciseTime & operator=(CNSPreciseTime &&)=default
static CNSPreciseTime Never(void)
CNSPreciseTime(const CNSPreciseTime &other)
bool operator>(const CNSPreciseTime &t) const
time_t Sec(void) const
time_t & Sec(void)
int Compare(const CNSPreciseTime &t) const
CNSPreciseTime & operator+=(const CNSPreciseTime &t)
CNSPreciseTime & operator=(const CNSPreciseTime &rhs)
CNSPreciseTime & operator-=(const CNSPreciseTime &t)
CNSPreciseTime(double time)
long NSec(void) const
bool operator<(const CNSPreciseTime &t) const
CNSPreciseTime(CNSPreciseTime &&)=default
CNSPreciseTime(unsigned int sec, unsigned int nsec)
bool operator>=(const CNSPreciseTime &t) const
static CNSPreciseTime Current(void)
CTime –.
Definition: ncbitime.hpp:296
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
CTime & ToLocalTime(void)
Convert the time into local time.
Definition: ncbitime.hpp:2465
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1511
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
EIPRangeType t
Definition: ncbi_localip.c:101
Defines: CTimeFormat - storage class for time format.
T max(T x_, T y_)
CNSPreciseTime operator-(const CNSPreciseTime &lhs, const CNSPreciseTime &rhs)
const CNSPreciseTime kTimeZero
@ kUSecsPerSecond
@ kNSecsPerMSec
@ kNSecsPerSecond
@ kNSecsPerUSec
@ kUSecsPerMSec
@ kMSecsPerSecond
CNSPreciseTime operator+(const CNSPreciseTime &lhs, const CNSPreciseTime &rhs)
bool operator!=(const CNSPreciseTime &lhs, const CNSPreciseTime &rhs)
bool operator==(const CNSPreciseTime &lhs, const CNSPreciseTime &rhs)
const CNSPreciseTime kTimeNever
string NS_FormatPreciseTimeAsSec(const CNSPreciseTime &t)
string NS_FormatPreciseTime(const CNSPreciseTime &t)
static pcre_uint8 * buffer
Definition: pcretest.c:1051
else result
Definition: token2.c:20
Modified on Sat Dec 09 04:44:39 2023 by modify_doxy.py rev. 669887