Fleet  0.0.9
Inference in the LOT
Timing.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include <chrono>
9 #include "Errors.h"
10 
11 using time_ms = unsigned long;
12 using timept = std::chrono::high_resolution_clock::time_point;
13 
14 timept now() {
20  return std::chrono::high_resolution_clock::now();
21 }
22 
23 time_ms time_since(timept x) {
29  return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()-x).count();
30 }
31 
32 
33 // this is just handy for timing stuff -- put blocks of code between tic() and tic()
34 timept tic_start;
35 time_ms tic_elapsed;
36 void tic() {
41  tic_elapsed = time_since(tic_start);
42  tic_start = now();
43 }
44 double elapsed_seconds() {
50  return tic_elapsed / 1000.0;
51 }
52 
53 
54 std::string datestring() {
60  //https://stackoverflow.com/questions/40100507/how-do-i-get-the-current-date-in-c
61 
62  time_t rawtime;
63  struct tm * timeinfo;
64  char buffer[80];
65 
66  time (&rawtime);
67  timeinfo = localtime(&rawtime);
68 
69  strftime(buffer,80,"%Y-%m-%d %I:%M:%S",timeinfo);
70  return std::string(buffer);
71 }
72 
73 
74 
78 
79 time_t convert_time(const std::string& s) {
87  // specila case of s="0" will be allowed
88  if(s == "0") return 0;
89 
90  // else we must specify a unit
91  double multiplier; // for default multiplier of 1 is seconds
92  switch(s.at(s.length()-1)) {
93  case 'q': multiplier = 1; break; // q=ms
94  case 's': multiplier = 1000; break;
95  case 'm': multiplier = 60*1000; break;
96  case 'h': multiplier = 60*60*1000; break;
97  case 'd': multiplier = 60*60*24*1000; break;
98  default:
99  std::cout << "*** Unknown time specifier: " << s.at(s.length()-1) << " in " << s << ". Did you forget a unit?" << std::endl;
100  throw YouShouldNotBeHereError();
101  }
102 
103  double t = std::stod(s.substr(0,s.length()-1)); // all but the last character
104 
105  return (unsigned long)(t*multiplier); // note this effectively rounds to the nearest second
106 
107 }
size_t count(const std::string &str, const std::string &sub)
Definition: Strings.h:344
Definition: Errors.h:18
time_t convert_time(const std::string &s)
Give a timepoint for the current time.
Definition: Timing.h:79