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:
73 	value_type m_start;
74 	value_type m_stop;
75 	bool m_started;
76 	bool m_stopped;
77 	duration_type m_duration;
78 	bool m_use_localtime;
79 
80 	void mf_start_value(const value_type a_t);
81 	void mf_stop_value(const value_type a_t);
82 	const duration_type mf_calculate_duration(
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
97