GT RoboCup SSL
Soccer software, robot firmware
time.hpp
1 #pragma once
2 
3 #include <sys/time.h>
4 #include <chrono>
5 #include <string>
6 #include <iostream>
7 
8 using namespace std::chrono_literals;
9 
10 namespace RJ {
11 
13 typedef std::chrono::system_clock::time_point Time;
14 typedef int64_t Timestamp; // Time in microseconds
15 typedef std::chrono::duration<double> Seconds;
18 template <class Duration>
19 constexpr int64_t numMicroseconds(Duration d) {
20  return std::chrono::duration_cast<std::chrono::microseconds>(d).count();
21 }
22 
23 inline Time now() {
24  return std::chrono::system_clock::now();
25  // struct timeval time;
26  // gettimeofday(&time, nullptr);
27  // return (Time)time.tv_sec * 1000000 + (Time)time.tv_usec;
28 }
29 
30 constexpr Timestamp timestamp(Time time) {
31  return numMicroseconds(time.time_since_epoch());
32 }
33 
34 inline Timestamp timestamp() { return timestamp(now()); }
35 
37 constexpr RJ::Timestamp SecsToTimestamp(double secs) {
38  return secs * 1000000.0f;
39 }
40 
41 template <class Duration>
42 constexpr double numSeconds(Duration d) {
43  return std::chrono::duration<double>(d).count();
44 }
45 
47 constexpr float TimestampToSecs(RJ::Timestamp timestamp) {
48  return (float)(timestamp / 1000000.0f);
49 }
50 
51 } // namespace RJ
52 
53 inline RJ::Time operator+(const RJ::Time& time, const RJ::Seconds& sec) {
54  return time + std::chrono::duration_cast<RJ::Time::duration>(sec);
55 }
56 
57 inline RJ::Time operator-(const RJ::Time& time, const RJ::Seconds& sec) {
58  return time - std::chrono::duration_cast<RJ::Time::duration>(sec);
59 }
60 
61 inline std::string to_string(RJ::Seconds seconds) {
62  return std::to_string(seconds.count()) + "(Seconds)";
63 }
64 
65 inline std::ostream& operator<<(std::ostream& os, RJ::Seconds seconds) {
66  os << to_string(seconds);
67  return os;
68 }
Definition: time.hpp:10