34 clock_gettime(CLOCK_MONOTONIC, &tm);
35 return tm.tv_sec*1000000000LL + tm.tv_nsec;
42 if (!CheckLinuxTSC()) {
50 double sum1(0.), sum2(0.), coef(0.);
56 for (
int n=0;n<num;n++) {
58 slow1 = GetSlowClock();
59 fast1 = GetFastClock();
64 slow2 = GetSlowClock();
65 fast2 = GetFastClock();
72 coef = (slow2-slow1) * 1e-9 / (fast2 - fast1);
81 double aver = num/sum1;
83 for (
int n=0;n<num;n++) {
84 sum2 += (1/values[n] - aver)*(1/values[n] - aver);
87 double deviat = sqrt(sum2/num);
91 if (deviat/aver>0.0001) {
104 FILE* f = fopen(
"/proc/cpuinfo",
"r");
105 if (!f)
return false;
107 bool can_use_tsc =
false;
108 bool isgsiamd =
false;
112 while(fgets(buf,
sizeof(buf), f)) {
113 if (strstr(buf,
"AMD Opteron(tm) Processor 248")!=0) {
119 if (strstr(buf,
"constant_tsc")!=0) {
126 return can_use_tsc || isgsiamd;
134 t.tv_sec = lrint(tm);
135 if (t.tv_sec > tm) t.tv_sec--;
136 t.tv_nsec = lrint((tm - t.tv_sec)*1e9);
139 usleep(t.tv_nsec / 1000);
150 clock_gettime(CLOCK_REALTIME, &tm);
153 tv_nsec = tm.tv_nsec;
160 return ((uint64_t) tv_sec) * 1000 + tv_nsec / 1000000;
166 if (
null())
return std::string();
171 if ((ndecimal>0) && (tv_nsec>0)) {
172 if (ndecimal>9) ndecimal = 9;
174 int maxfrac = 1000000000;
175 int dec_cnt = ndecimal;
176 while (dec_cnt++<9) {
178 frac = frac / 10 + ((frac % 10 >= 5) ? 1 : 0);
179 maxfrac = maxfrac / 10;
182 if (frac >= maxfrac) { src++; frac = 0; }
188 localtime_r(&src, &res);
190 gmtime_r(&src, &res);
194 strftime(sbuf,
sizeof(sbuf),
"%Y-%m-%d %H:%M:%S", &res);
197 int rlen = strlen(sbuf);
198 int remain = (int)
sizeof(sbuf) - rlen;
199 if (remain > ndecimal + 1)
200 snprintf(sbuf+rlen, remain,
".%0*d", ndecimal, frac);
203 return std::string(sbuf);
208 return tv_sec + tv_nsec*1e-9;
214 if (
null())
return std::string();
220 if ((ndecimal>0) && (tv_nsec>0)) {
221 if (ndecimal>9) ndecimal = 9;
223 int maxfrac = 1000000000;
224 int dec_cnt = ndecimal;
225 while (dec_cnt++<9) {
227 frac = frac / 10 + ((frac % 10 >= 5) ? 1 : 0);
228 maxfrac = maxfrac / 10;
231 if (frac >= maxfrac) { src++; frac = 0; }
236 gmtime_r(&src, &res);
238 strftime(sbuf,
sizeof(sbuf),
"%Y-%m-%dT%H:%M:%S", &res);
241 int rlen = strlen(sbuf);
242 int remains = (int)
sizeof(sbuf) - rlen;
243 if (remains - rlen > ndecimal + 1)
244 snprintf(sbuf+rlen, remains-1,
".%0*d", ndecimal, frac);
247 int rlen = strlen(sbuf);
248 if (rlen >= (
int)
sizeof(sbuf)-1)
return std::string();
253 return std::string(sbuf);
259 if (
null())
return std::string();
263 localtime_r(&src, &res);
265 gmtime_r(&src, &res);
266 if (!separ) separ =
"-";
267 std::string fmt = std::string(
"%Y") + separ +
"%m" + separ +
"%d";
269 strftime(sbuf,
sizeof(sbuf), fmt.c_str(), &res);
270 return std::string(sbuf);
275 if (
null())
return std::string();
279 localtime_r(&src, &res);
281 gmtime_r(&src, &res);
282 if (!separ) separ =
":";
283 std::string fmt = std::string(
"%H") + separ +
"%M" + separ +
"%S";
285 strftime(sbuf,
sizeof(sbuf), fmt.c_str(), &res);
286 return std::string(sbuf);
291 if ((sbuf==0) || (strlen(sbuf)!=10))
return false;
293 unsigned year(0), month(0), day(0);
294 if (sscanf(sbuf,
"%4u-%02u-%02u", &year, &month, &day)!=3)
return false;
295 if ((year<1970) || (year>2100) || (month>12) || (month==0) || (day>31) || (day==0))
return false;
299 gmtime_r(&src, &res);
300 res.tm_year = year - 1900;
301 res.tm_mon = month - 1;
304 tv_sec = timegm(&res);
310 if ((sbuf==0) || (strlen(sbuf)!=8))
return false;
312 unsigned hour(0), min(0), sec(0);
313 if (sscanf(sbuf,
"%02u:%02u:%02u", &hour, &min, &sec)!=3)
return false;
314 if ((hour>23) || (min>59) || (sec>59))
return false;
318 memset(&res, 0,
sizeof(res));
321 gmtime_r(&src, &res);
328 tv_sec = timegm(&res);
335 if (pnt.
null())
return -1. * AsDouble();
339 double res = (pnt.
tv_sec >= tv_sec) ? 1.*(pnt.
tv_sec - tv_sec) : -1. * (tv_sec - pnt.
tv_sec);
342 else res -= 1e-9 * (tv_nsec - pnt.
tv_nsec);
Class for holding GMT time with precision of nanoseconds.
bool SetOnlyTime(const char *sbuf)
Set only time part of DateTime.
std::string OnlyTimeAsString(const char *separ=nullptr, bool localtime=false) const
Fills only time as string.
std::string AsJSString(int ndecimal=3) const
convert string into sec.frac format, can be interpret directly in JavaScript ISO 8601 standard is use...
double AsDouble() const
Return date and time in seconds since 1.1.1970.
uint64_t AsJSDate() const
Return date and time in JS format - number of millisecond since 1.1.1970.
std::string AsString(int ndecimal=0, bool localtime=false) const
convert string into human-readable format, cannot be interpret directly in JavaScript
double DistanceTo(const DateTime &src) const
Return distance in seconds to provided date.
bool SetOnlyDate(const char *sbuf)
Set only date part from the string.
std::string OnlyDateAsString(const char *separ=nullptr, bool localtime=false) const
Fills only date as string.
static slowclock_t GetSlowClock()
static double gFastClockMult
static double CalculateFastClockMult()
static bool gFast
indicates if fast or slow method is used for time measurement
static slowclock_t gSlowClockZero
static fastclock_t GetFastClock()
static fastclock_t gFastClockZero
static bool CheckLinuxTSC()
long long int slowclock_t