xtime.h
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 #ifndef XTIME_H_
15 #define XTIME_H_
16 
17 #include "support.h"
18 #include <math.h>
19 #if !defined USE_QTHREAD
20  #include <chrono>
21  //#include <thread>
22  using namespace std::chrono;
23  //using namespace std::this_thread;
24 #endif
25 
26 //! Sleeps in ms
27 DECLSPEC_KAME void msecsleep(unsigned int ms) noexcept; //<!\todo {std::this_thread::sleep_for(std::chrono::milliseconds(ms));}
28 
29 //! Fetches CPU counter.
30 using timestamp_t = uint64_t;
31 DECLSPEC_KAME timestamp_t timeStamp() noexcept;
32 DECLSPEC_KAME timestamp_t timeStampCountsPerMilliSec() noexcept;
33 
34 class DECLSPEC_KAME XTime {
35 public:
36  XTime() noexcept : tv_sec(0), tv_usec(0) {}
37  XTime(long sec, long usec) noexcept : tv_sec(sec), tv_usec(usec) {}
38  double operator-(const XTime &x) const noexcept {
39  return (tv_sec - x.tv_sec) + (tv_usec - x.tv_usec) * 1e-6;
40  }
41  long diff_usec(const XTime &x) const noexcept {
42  return (tv_sec - x.tv_sec) * 1000000L + ((tv_usec - x.tv_usec));
43  }
44  long diff_msec(const XTime &x) const noexcept {
45  return (tv_sec - x.tv_sec) * 1000L + ((tv_usec - x.tv_usec) / 1000L);
46  }
47  long diff_sec(const XTime &x) const noexcept {
48  return tv_sec - x.tv_sec;
49  }
50  XTime &operator+=(double sec_d) noexcept {
51  long sec = floor(sec_d + tv_sec + 1e-6 * tv_usec);
52  long usec = (lrint(1e6 * (tv_sec - sec + sec_d) + tv_usec));
53  tv_sec = sec;
54  tv_usec = usec;
55  assert((tv_usec >= 0) && (tv_usec < 1000000));
56  return *this;
57  }
58  XTime &operator-=(double sec) noexcept {
59  *this += -sec;
60  return *this;
61  }
62  bool operator==(const XTime &x) const noexcept {
63  return (tv_sec == x.tv_sec) && (tv_usec == x.tv_usec);
64  }
65  bool operator!=(const XTime &x) const noexcept {
66  return (tv_sec != x.tv_sec) || (tv_usec != x.tv_usec);
67  }
68  bool operator<(const XTime &x) const noexcept {
69  return (tv_sec < x.tv_sec) || ((tv_sec == x.tv_sec) && (tv_usec < x.tv_usec));
70  }
71  bool operator<=(const XTime &x) const noexcept {
72  return (tv_sec <= x.tv_sec) || ((tv_sec == x.tv_sec) && (tv_usec <= x.tv_usec));
73  }
74  bool operator>(const XTime &x) const noexcept {
75  return (tv_sec > x.tv_sec) || ((tv_sec == x.tv_sec) && (tv_usec > x.tv_usec));
76  }
77  bool operator>=(const XTime &x) const noexcept {
78  return (tv_sec >= x.tv_sec) || ((tv_sec == x.tv_sec) && (tv_usec >= x.tv_usec));
79  }
80  bool operator!() const noexcept {
81  return (tv_sec == 0) && (tv_usec == 0);
82  }
83  operator bool() const noexcept {
84  return (tv_sec != 0) || (tv_usec != 0);
85  }
86  long sec() const noexcept {return tv_sec;}
87  long usec() const noexcept {return tv_usec;}
88  static XTime now() noexcept;
89  XString getTimeStr(bool subsecond = true) const;
90  XString getTimeFmtStr(const char *fmt, bool subsecond = true) const
91 #if defined __GNUC__ || defined __clang__
92  __attribute__ ((format(strftime,2, 0)))
93 #endif
94  ;
95 private:
96  long tv_sec;
97  long tv_usec;
98 };
99 
100 #endif /*XTIME_H_*/

Generated for KAME4 by  doxygen 1.8.3