CBMC
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  (void)clock_type; // unused parameter
23  return std::unique_ptr<const timestampert>(new timestampert());
24 #else
25  switch(clock_type)
26  {
28  return std::unique_ptr<const timestampert>(new timestampert());
30  return std::unique_ptr<const monotonic_timestampert>(
33  return std::unique_ptr<const wall_clock_timestampert>(
35  }
37 #endif
38 }
39 
40 #ifndef _WIN32
41 std::string monotonic_timestampert::stamp() const
42 {
43  std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>
44  time_stamp = std::chrono::time_point_cast<std::chrono::microseconds>(
45  std::chrono::steady_clock::now());
46 
47  auto cnt = time_stamp.time_since_epoch().count();
48  std::lldiv_t divmod = lldiv(cnt, 1000000);
49 
50  std::stringstream ss;
51  ss << divmod.quot << "." << std::setfill('0') << std::setw(6) << divmod.rem;
52  return ss.str();
53 }
54 
55 #define WALL_FORMAT "%Y-%m-%dT%H:%M:%S."
56 
57 std::string wall_clock_timestampert::stamp() const
58 {
59  std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>
60  time_stamp = std::chrono::time_point_cast<std::chrono::microseconds>(
61  std::chrono::system_clock::now());
62 
63  unsigned u_seconds = time_stamp.time_since_epoch().count() % 1000000;
64 
65  std::time_t tt = std::chrono::system_clock::to_time_t(time_stamp);
66  std::tm local = *std::localtime(&tt);
67 
68  std::stringstream ss;
69  ss << std::put_time(&local, WALL_FORMAT) << std::setfill('0') << std::setw(6)
70  << u_seconds;
71  return ss.str();
72 }
73 #endif
virtual std::string stamp() const override
See HELP_TIMESTAMP in util/timestamper.h for time stamp format.
Definition: timestamper.cpp:41
Timestamp class hierarchy.
Definition: timestamper.h:42
clockt
Derived types of timestampert.
Definition: timestamper.h:46
@ MONOTONIC
monotonic_timestampert
@ WALL_CLOCK
wall_clock_timestampert
@ NONE
timestampert
static std::unique_ptr< const timestampert > make(clockt clock_type)
Factory method to build timestampert subclasses.
Definition: timestamper.cpp:19
virtual std::string stamp() const override
See HELP_TIMESTAMP in util/timestamper.h for time stamp format.
Definition: timestamper.cpp:57
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
struct tm * localtime(const time_t *clock)
Definition: time.c:112
#define WALL_FORMAT
Definition: timestamper.cpp:55
Emit timestamps.