The Sparta Modeling Framework
Loading...
Searching...
No Matches
TimeManager.hpp
1// <TimeManager> -*- C++ -*-
2
3#pragma once
4
5#include <string>
6#include <sys/time.h>
7#include <unistd.h>
8#include <ctime>
9
10namespace sparta
11{
12
19 {
20 timeval tv_start_;
21
22 public:
23
28 tv_start_{0,0}
29 {
30 gettimeofday(&tv_start_, NULL);
31 }
32
38 static TimeManager tm;
39 return tm;
40 }
41
46 double getSecondsElapsed() const {
47 struct timeval t;
48 gettimeofday(&t, NULL);
49 return ((t.tv_sec - tv_start_.tv_sec)) + ((t.tv_usec - tv_start_.tv_usec) / 1000000.0);
50 }
51
55 double getAbsoluteSeconds() const {
56 struct timeval t;
57 gettimeofday(&t, NULL);
58 return (t.tv_sec) + (t.tv_usec / 1000000.0);
59 }
60
64 std::string getLocalTime() const {
65 std::time_t t = std::time(nullptr);
66 char mbstr[100];
67 if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&t))) {
68 return mbstr;
69 }
70 return "";
71 }
72
77 std::string getSortableLocalTime() const {
78 char buffer[512];
79 time_t rawtime;
80 struct tm* timeinfo;
81 time(&rawtime);
82 timeinfo = localtime(&rawtime);
83 strftime(buffer, sizeof(buffer), (const char*)"%Y-%m-%d_%a_%H-%M-%S", timeinfo);
84 return buffer;
85 }
86
90 std::string getTimestamp() const {
91 std::stringstream ss;
92 ss << std::time(0);
93 return ss.str();
94 }
95 };
96
97} // namespace sparta
98
Singleton which manages wall-clock time for simulations in SPARTA This is not a "timer" manager,...
double getAbsoluteSeconds() const
Gets the absolute second timestamp for this machine.
std::string getTimestamp() const
Returns a unix timestamp string.
TimeManager()
Default constructor.
std::string getSortableLocalTime() const
Returns a string representing local time in a format suitable for trival string sorting.
double getSecondsElapsed() const
Gets the number of seconds elapsed since the instantiation of SPARTA static and global vars.
std::string getLocalTime() const
Returns a string representing local time for the simulator.
static TimeManager & getTimeManager()
Returns the TimeManager singleton.
Macros for handling exponential backoff.