rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timer.h
Go to the documentation of this file.
1 #ifndef __timer_h__
2 #define __timer_h__
3 
4 #ifdef HAVE_SYS_TYPES_H
5 #include <sys/types.h>
6 #endif
7 
8 #ifdef TIME_WITH_SYS_TIME
9  #include <sys/time.h>
10  #include <time.h>
11 #else
12  #ifdef HAVE_SYS_TIME_H
13  #include <sys/time.h>
14  #else
15  #include <time.h>
16  #endif
17 #endif
18 
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 
23 #include <string>
24 
25 #include "asserts.h"
26 #include "types.h"
27 
28 /** Used as a stopwatch */
29 class timer
30 {
31 public:
32  typedef time_t value_type;
33  typedef double duration_type;
34 
35  timer();
36  timer(const timer& a_timer);
37  timer(const value_type a_start);
38  timer(const value_type a_start, const value_type a_stop);
39 
40  void clear(void);
41  void start(void);
42  void stop(void);
43 
44  const value_type start_value(void) const;
45  const value_type stop_value(void) const;
46 
47  void assign(const timer& a_timer);
48  void assign(const value_type a_start);
49  void assign(const value_type a_start, const value_type a_stop);
50 
51  timer& operator=(const timer& a_timer);
52 
53  void use_localtime(const bool a_switch);
54  const bool use_localtime(void) const;
55 
56  const std::string started_at(void) const;
57  const std::string stopped_at(void) const;
58  const std::string duration(void) const;
59  const std::string eta(unsigned int a_percent_complete) const;
60  const std::string eta(unsigned int a_complete, unsigned int a_total) const;
61  const std::string bps(uint64 a_bytes) const;
62 
63  const bool is_started(void) const;
64  const bool is_stopped(void) const;
65 
66  const duration_type duration_secs(void) const;
67  const duration_type duration_mins(void) const;
68  const duration_type duration_hours(void) const;
69  const duration_type duration_days(void) const;
70  const duration_type duration_years(void) const;
71 
72 private:
75  bool m_started;
76  bool m_stopped;
79 
80  void mf_start_value(const value_type a_t);
81  void mf_stop_value(const value_type a_t);
83  const value_type a_start, const value_type a_stop) const;
84  const std::string mf_make_timer_string(const value_type a_t) const;
85  const std::string mf_make_string(const value_type a_t) const;
86  const std::string mf_make_string(const duration_type a_d) const;
87 };
88 
89 const std::string current_time(void);
90 
91 /** A null timer */
92 const timer null_timer(0,0);
93 
94 std::string stamp(const pid_t = 0, const int a_indention = 0);
95 
96 #endif
std::string stamp(const pid_t=0, const int a_indention=0)
Generate a timstamp string.
Definition: timer.cc:552
void mf_start_value(const value_type a_t)
Set the timer start value.
Definition: timer.cc:109
const duration_type duration_secs(void) const
Reutrn the duration in seconds.
Definition: timer.cc:411
const bool is_stopped(void) const
Return whether or not the timer has been stopped.
Definition: timer.cc:405
Basic types definitions and templates.
void mf_stop_value(const value_type a_t)
Set the timer stop value.
Definition: timer.cc:116
double duration_type
Definition: timer.h:33
const bool is_started(void) const
Return whether or not the timer has been started.
Definition: timer.cc:399
const std::string bps(uint64 a_bytes) const
Given a number of bytes, estimate the BPS.
Definition: timer.cc:522
void clear(void)
Clear the timer.
Definition: timer.cc:98
timer()
C'tor.
Definition: timer.cc:73
const duration_type duration_hours(void) const
Return the duration in hours.
Definition: timer.cc:432
bool m_started
Definition: timer.h:75
const duration_type mf_calculate_duration(const value_type a_start, const value_type a_stop) const
Calculate the duration between start and stop.
Definition: timer.cc:124
const timer null_timer(0, 0)
A null timer.
void assign(const timer &a_timer)
Assign timer values from another timer instance.
Definition: timer.cc:162
void start(void)
Start (or restart) the timer.
Definition: timer.cc:137
const std::string stopped_at(void) const
Generate a stopped-at string.
Definition: timer.cc:371
value_type m_stop
Definition: timer.h:74
const value_type start_value(void) const
Return the timer start value.
Definition: timer.cc:150
bool m_use_localtime
Definition: timer.h:78
const std::string mf_make_timer_string(const value_type a_t) const
Generate a string in a regular human-readable format.
Definition: timer.cc:212
const std::string started_at(void) const
Generate a started-at string.
Definition: timer.cc:359
time_t value_type
Definition: timer.h:32
const duration_type duration_years(void) const
Return the duration in years.
Definition: timer.cc:452
const value_type stop_value(void) const
Return the timer stop value.
Definition: timer.cc:156
const std::string current_time(void)
Return the current time.
Definition: timer.cc:542
const std::string duration(void) const
Generate a duration string.
Definition: timer.cc:385
const std::string eta(unsigned int a_percent_complete) const
Given a percent-complete for some unknown task, estimate a time to completion.
Definition: timer.cc:463
bool m_stopped
Definition: timer.h:76
const bool use_localtime(void) const
Return whether to use localtime or GMT.
Definition: timer.cc:206
const duration_type duration_days(void) const
Return the duration in days.
Definition: timer.cc:442
const duration_type duration_mins(void) const
Return the duration in minutes.
Definition: timer.cc:422
duration_type m_duration
Definition: timer.h:77
timer & operator=(const timer &a_timer)
Assignment.
Definition: timer.cc:192
void stop(void)
Stop the timer.
Definition: timer.cc:143
value_type m_start
Definition: timer.h:73
const std::string mf_make_string(const value_type a_t) const
Generate a string in a timestamp format.
Definition: timer.cc:273
Used as a stopwatch.
Definition: timer.h:29