watch.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <utils/time/watch.h>
00025 #include <utils/time/clock.h>
00026 #include <utils/time/time.h>
00027
00028 namespace fawkes {
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 Watch::Watch(Clock *clock)
00041 {
00042 this->clock = clock;
00043
00044 is_running = false;
00045 is_paused = false;
00046
00047 }
00048
00049
00050
00051 Watch::~Watch()
00052 {
00053 }
00054
00055
00056
00057
00058
00059
00060
00061 void
00062 Watch::start(Time* t)
00063 {
00064 timeval now;
00065 clock->get_time(&now);
00066
00067 if (is_running && is_paused)
00068 {
00069 pause_stop.set_time(&now);
00070 is_paused = false;
00071
00072 pause_time += pause_stop - pause_start;
00073 }
00074 else if (!is_running)
00075 {
00076 pause_time.set_time( 0.0f );
00077 is_running = true;
00078
00079 watch_start.set_time(&now);
00080 }
00081 else
00082 {
00083
00084 }
00085
00086 if (0 != t) {
00087 t->set_time(&now);
00088 }
00089 }
00090
00091
00092
00093
00094
00095
00096 void
00097 Watch::stop(Time* t)
00098 {
00099 timeval now;
00100 clock->get_time(&now);
00101 watch_stop.set_time(&now);
00102 is_running = false;
00103
00104 if (is_paused)
00105 {
00106 pause_stop.set_time(&now);
00107 pause_time += pause_stop - pause_start;
00108
00109 is_paused = false;
00110 }
00111
00112 if (0 != t) {
00113 t->set_time(&now);
00114 }
00115 }
00116
00117
00118
00119
00120
00121
00122 void
00123 Watch::pause(Time* t)
00124 {
00125 timeval now;
00126 clock->get_time(&now);
00127
00128 if (!is_paused) {
00129 pause_start.set_time(&now);
00130 is_paused = true;
00131 }
00132
00133 if (0 != t) {
00134 t->set_time(&now);;
00135 }
00136 }
00137
00138
00139
00140 void
00141 Watch::reset()
00142 {
00143 timeval now;
00144 clock->get_time(&now);
00145 watch_start.set_time(&now);
00146 }
00147
00148
00149
00150
00151
00152 Time
00153 Watch::watch_time()
00154 {
00155 timeval now;
00156 clock->get_time(&now);
00157
00158 Time ret(&now);
00159
00160 if (is_running && !is_paused)
00161 {
00162 ret -= watch_start + pause_time;
00163 }
00164 else if (is_running && is_paused)
00165 {
00166 Time cur_pause;
00167 cur_pause = ret - pause_start;
00168 ret -= watch_start + pause_time + cur_pause;
00169 }
00170 else
00171 {
00172 ret = watch_stop - watch_start - pause_time;
00173 }
00174
00175 return ret;
00176 }
00177
00178
00179
00180
00181
00182 Time
00183 Watch::clock_time()
00184 {
00185 timeval now;
00186 clock->get_time(&now);
00187 Time t(&now);
00188 return t;
00189 }
00190
00191 }