cprover
timestamper.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Timestamps
4 
5 Author: Kareem Khazem <karkhaz@karkhaz.com>
6 
7 \*******************************************************************/
8 
9 #include "timestamper.h"
10 
11 #include <chrono>
12 #include <cstdlib>
13 #include <iomanip>
14 #include <sstream>
15 
16 #include "invariant.h"
17 
18 std::unique_ptr<const timestampert>
20 {
21 #ifdef _WIN32
22  return std::unique_ptr<const timestampert>(new timestampert());
23 #else
24  switch(clock_type)
25  {
27  return std::unique_ptr<const timestampert>(new timestampert());
29  return std::unique_ptr<const monotonic_timestampert>(
32  return std::unique_ptr<const wall_clock_timestampert>(
34  }
36 #endif
37 }
38 
39 #ifndef _WIN32
40 std::string monotonic_timestampert::stamp() const
41 {
42  std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>
43  time_stamp = std::chrono::time_point_cast<std::chrono::microseconds>(
44  std::chrono::steady_clock::now());
45 
46  auto cnt = time_stamp.time_since_epoch().count();
47  std::lldiv_t divmod = lldiv(cnt, 1000000);
48 
49  std::stringstream ss;
50  ss << divmod.quot << "." << std::setfill('0') << std::setw(6) << divmod.rem;
51  return ss.str();
52 }
53 
54 #define WALL_FORMAT "%Y-%m-%dT%H:%M:%S."
55 
56 std::string wall_clock_timestampert::stamp() const
57 {
58  std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>
59  time_stamp = std::chrono::time_point_cast<std::chrono::microseconds>(
60  std::chrono::system_clock::now());
61 
62  unsigned u_seconds = time_stamp.time_since_epoch().count() % 1000000;
63 
64  std::time_t tt = std::chrono::system_clock::to_time_t(time_stamp);
65  std::tm local = *std::localtime(&tt);
66 
67  std::stringstream ss;
68  ss << std::put_time(&local, WALL_FORMAT) << std::setfill('0') << std::setw(6)
69  << u_seconds;
70  return ss.str();
71 }
72 #endif
#define WALL_FORMAT
Definition: timestamper.cpp:54
virtual std::string stamp() const override
See HELP_TIMESTAMP in util/timestamper.h for time stamp format.
Definition: timestamper.cpp:40
Timestamp class hierarchy.
Definition: timestamper.h:41
monotonic_timestampert
wall_clock_timestampert
clockt
Derived types of timestampert.
Definition: timestamper.h:45
virtual std::string stamp() const override
See HELP_TIMESTAMP in util/timestamper.h for time stamp format.
Definition: timestamper.cpp:56
Emit timestamps.
#define UNREACHABLE
Definition: invariant.h:271
static std::unique_ptr< const timestampert > make(clockt clock_type)
Factory method to build timestampert subclasses.
Definition: timestamper.cpp:19