13 #include <sys/types.h>
35 TRY_nomem(es =
"Could not convert time_t to localtime: \"");
38 throw(
ERROR(errno,es));
42 "Error generating string from year");
47 "Error generating string from month");
52 "Error generating string from day");
57 "Error generating string from hour");
62 "Error generating string from minute");
67 "Error generating string from seconds");
131 duration = difftime(a_stop,a_start);
217 const char* month_names[] = {
218 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
219 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
225 TRY_nomem(es =
"Could not convert time_t to localtime: \"");
228 throw(
ERROR(errno,es));
234 TRY_nomem(es =
"Could not convert time_t to GMT: \"");
237 throw(
ERROR(errno,es));
242 "Error generating string from year");
247 "Error generating string from month");
252 "Error generating string from day");
257 "Error generating string from hour");
262 "Error generating string from minute");
267 "Error generating string from seconds");
285 uint64 deciseconds = 0;
292 bool negative =
false;
296 deciseconds =
static_cast<uint64
>(-a_d * 10.0);
299 deciseconds =
static_cast<uint64
>(a_d * 10.0);
301 if (deciseconds >= 10) {
302 seconds = deciseconds / 10;
306 minutes = seconds / 60;
310 hours = minutes / 60;
331 if ((years > 0) || (days > 0)) {
336 if ((years > 0) || (days > 0) || (hours > 0)) {
338 "Error generating string from hours");
343 "Error generating string from minutes");
348 "Error generating string from seconds");
353 "Error generating string from deciseconds");
364 throw(
INTERNAL_ERROR(0,
"Request for start time from an unstarted timer"));
376 throw(
INTERNAL_ERROR(0,
"Request for stop time from an unstarted timer"));
378 throw(
INTERNAL_ERROR(0,
"Request for stop time from a running timer"));
390 throw(
INTERNAL_ERROR(0,
"Request for stop time from an unstarted timer"));
392 throw(
INTERNAL_ERROR(0,
"Request for duration from a running timer"));
414 throw(
INTERNAL_ERROR(0,
"Request for duration from an unstarted timer"));
416 throw(
INTERNAL_ERROR(0,
"Request for duration from a running timer"));
463 const std::string
timer::eta(
unsigned int a_percent_complete)
const
471 throw(
INTERNAL_ERROR(0,
"Attempt to calculate ETA for unstarted timer"));
473 throw(
INTERNAL_ERROR(0,
"Attempt to calculate ETA for a running timer"));
474 if (a_percent_complete == 0) {
479 TRY_nomem(es =
"Could not calculate ETA");
482 total_duration *= 100;
483 total_duration /= a_percent_complete;
494 unsigned int a_complete,
unsigned int a_total)
const
501 throw(
INTERNAL_ERROR(0,
"Attempt to calculate ETA for unstarted timer"));
503 throw(
INTERNAL_ERROR(0,
"Attempt to calculate ETA for a running timer"));
509 TRY_nomem(es =
"Could not calculate ETA");
511 percent_complete = a_complete;
512 percent_complete *= 100;
513 percent_complete /= a_total;
528 throw(
INTERNAL_ERROR(0,
"Attempt to calculate bps for unstarted timer"));
530 throw(
INTERNAL_ERROR(0,
"Attempt to calculate bps for a running timer"));
535 bps =
static_cast<uint64
>(a_bytes /
m_duration);
552 std::string
stamp(
const pid_t a_pid,
const int a_indention)
558 indent = a_indention;
577 estring(static_cast<unsigned long>(this_pid)).fmt_str(
584 for (; indent > 0; str +=
" ", --indent);
const pid_t pid(void)
Return the PID of this process.
void mf_start_value(const value_type a_t)
Set the timer start value.
Safely manipulate numbers without worryiung about over/underflow error.
const std::string make_time_string_(timer::value_type a_t)
Given a timer value, return a string in the form of "YYYY.MM.DD HH:MM:SS".
const duration_type duration_secs(void) const
Reutrn the duration in seconds.
const bool is_stopped(void) const
Return whether or not the timer has been stopped.
Basic types definitions and templates.
void mf_stop_value(const value_type a_t)
Set the timer stop value.
An extended string class.
const std::string current_time(void)
Return the current time.
const bool is_started(void) const
Return whether or not the timer has been started.
const T value(void) const
Return the value.
const std::string bps(uint64 a_bytes) const
Given a number of bytes, estimate the BPS.
void clear(void)
Clear the timer.
const duration_type duration_hours(void) const
Return the duration in hours.
std::string stamp(const pid_t a_pid, const int a_indention)
Generate a timstamp string.
const duration_type mf_calculate_duration(const value_type a_start, const value_type a_stop) const
Calculate the duration between start and stop.
std::string throughput_to_string(T a_t, const std::string &a_base_size_unit="b", const std::string &a_base_time_unit="s", uint16 a_width=10, uint16 a_precision=1, uint16 a_kilo=1024)
Given an integral number, return a string with the number formated as a number of machine-type size p...
void assign(const timer &a_timer)
Assign timer values from another timer instance.
void start(void)
Start (or restart) the timer.
const std::string stopped_at(void) const
Generate a stopped-at string.
const value_type start_value(void) const
Return the timer start value.
value_type fmt_str(void)
Generate a formatted string.
const std::string mf_make_timer_string(const value_type a_t) const
Generate a string in a regular human-readable format.
#define INTERNAL_ERROR(e, s)
const std::string started_at(void) const
Generate a started-at string.
const duration_type duration_years(void) const
Return the duration in years.
const value_type stop_value(void) const
Return the timer stop value.
const std::string duration(void) const
Generate a duration string.
const std::string eta(unsigned int a_percent_complete) const
Given a percent-complete for some unknown task, estimate a time to completion.
const bool use_localtime(void) const
Return whether to use localtime or GMT.
const duration_type duration_days(void) const
Return the duration in days.
const duration_type duration_mins(void) const
Return the duration in minutes.
timer & operator=(const timer &a_timer)
Assignment.
void stop(void)
Stop the timer.
const std::string mf_make_string(const value_type a_t) const
Generate a string in a timestamp format.