You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.9 KiB

  1. //
  2. // Created by zhaohe on 19-6-2.
  3. //
  4. #pragma once
  5. #include <chrono>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <list>
  9. #include <map>
  10. #include <memory>
  11. #include <set>
  12. #include <sstream>
  13. #include <string>
  14. #include <vector>
  15. namespace iflytop {
  16. namespace core {
  17. using namespace std;
  18. using namespace chrono;
  19. template <class T>
  20. class T_TimeUtils {
  21. public:
  22. time_point<T> zero() { return time_point<T>(nanoseconds(0)); }
  23. time_point<T> now() { return move(T::now()); }
  24. int64_t getus() { return duration_cast<microseconds>(T::now().time_since_epoch()).count(); }
  25. int64_t gets() { return duration_cast<seconds>(T::now().time_since_epoch()).count(); }
  26. int64_t getms() { return duration_cast<milliseconds>(T::now().time_since_epoch()).count(); }
  27. int64_t geth() { return duration_cast<hours>(T::now().time_since_epoch()).count(); }
  28. int64_t tpToMs(time_point<T> t) { return duration_cast<milliseconds>(t.time_since_epoch()).count(); }
  29. int64_t tpToUs(time_point<T> t) { return duration_cast<microseconds>(t.time_since_epoch()).count(); }
  30. int64_t tpToS(time_point<T> t) { return duration_cast<seconds>(t.time_since_epoch()).count(); }
  31. int64_t dToMs(nanoseconds ns) { return duration_cast<milliseconds>(ns).count(); }
  32. int64_t dToUs(nanoseconds ns) { return duration_cast<microseconds>(ns).count(); }
  33. int64_t dToS(nanoseconds ns) { return duration_cast<seconds>(ns).count(); }
  34. int64_t dToNs(nanoseconds ns) { return ns.count(); }
  35. inline time_point<T> msToTp(int64_t ms) {
  36. time_point<T> tp = time_point<T>(milliseconds(ms));
  37. return move(tp);
  38. }
  39. // 时间操作
  40. inline time_point<T> addh(time_point<T> point, int value) { return point + hours(value); }
  41. inline time_point<T> adds(time_point<T> point, int value) { return point + seconds(value); }
  42. inline time_point<T> addms(time_point<T> point, int value) { return point + milliseconds(value); }
  43. inline time_point<T> addus(time_point<T> point, int value) { return point + microseconds(value); }
  44. inline time_point<T> addh(int value) { return addh(T::now(), value); }
  45. inline time_point<T> adds(int value) { return adds(T::now(), value); }
  46. inline time_point<T> addms(int value) { return addms(T::now(), value); }
  47. inline time_point<T> addus(int value) { return addus(T::now(), value); }
  48. inline int64_t ms2us(int64_t ms) { return ms * 1000; }
  49. /**
  50. * @brief
  51. */
  52. int64_t elapsedTimeS(time_point<T> begin) { return dToS(T::now() - begin); }
  53. int64_t elapsedTimeMs(time_point<T> begin) { return dToMs(T::now() - begin); }
  54. int64_t elapsedTimeUs(time_point<T> begin) { return dToUs(T::now() - begin); }
  55. int64_t elapsedTimeS(int64_t ms) { return (getms() - ms) / 1000; }
  56. int64_t inline elapsedTimeMs(int64_t ms) { return (getms() - ms); }
  57. int64_t inline elapsedTimeMs(int64_t now, int64_t ms) { return (now - ms); }
  58. int64_t inline elapsedTimeUs(int64_t ms) { return (getms() - ms) / 1000 / 1000; }
  59. /**
  60. * @brief  
  61. */
  62. int64_t countdownTimeS(time_point<T> endtime) { return dToS(endtime - T::now()); }
  63. int64_t countdownTimeMs(time_point<T> endtime) { return dToMs(endtime - T::now()); }
  64. int64_t countdownTimeUs(time_point<T> endtime) { return dToUs(endtime - T::now()); }
  65. int64_t countdownTimeNs(time_point<T> endtime) { return dToNs(endtime - T::now()); }
  66. };
  67. typedef T_TimeUtils<system_clock> tu_sys; // not use in future
  68. typedef T_TimeUtils<steady_clock> tu_steady; // not use in future
  69. typedef time_point<system_clock> tp_sys; // not use in future
  70. typedef time_point<steady_clock> tp_steady; // not use in future
  71. // new api name
  72. typedef T_TimeUtils<system_clock> zsystem_clock;
  73. typedef T_TimeUtils<steady_clock> zsteady_clock;
  74. typedef time_point<system_clock> zsystem_tp;
  75. typedef time_point<steady_clock> zsteady_tp;
  76. }; // namespace core
  77. } // namespace iflytop