00001 #include "Time.h"
00002
00003 #include <stdlib.h>
00004
00005 namespace FCam {
00006
00007 Time Time::now() {
00008 Time t2;
00009 gettimeofday(&t2.t, NULL);
00010 return t2;
00011 }
00012
00013 int Time::operator-(const Time &other) const {
00014 return (((int)(s()) - (int)(other.s()))*1000000 +
00015 ((int)(us()) - (int)(other.us())));
00016 }
00017
00018 bool Time::operator<(const Time &other) const {
00019 return (s() < other.s() ||
00020 (s() == other.s() && us() < other.us()));
00021 }
00022
00023 bool Time::operator>(const Time &other) const {
00024 return (s() > other.s() ||
00025 (s() == other.s() && us() > other.us()));
00026 }
00027
00028 bool Time::operator>=(const Time &other) const {
00029 return (s() > other.s() ||
00030 (s() == other.s() && us() >= other.us()));
00031 }
00032
00033 bool Time::operator<=(const Time &other) const {
00034 return (s() < other.s() ||
00035 (s() == other.s() && us() <= other.us()));
00036 }
00037
00038 bool Time::operator==(const Time &other) const {
00039 return (s() == other.s() &&
00040 us() == other.us());
00041 }
00042
00043 bool Time::operator!=(const Time &other) const {
00044 return (us() != other.us() ||
00045 s() != other.s());
00046 }
00047
00048
00049 Time Time::operator+=(int usecs) {
00050 int newUsecs = us() + usecs;
00051 int dSec = 0;
00052 while (newUsecs < 0) {
00053 dSec--;
00054 newUsecs += 1000000;
00055 }
00056 while (newUsecs > 1000000) {
00057 dSec++;
00058 newUsecs -= 1000000;
00059 }
00060 t.tv_usec = newUsecs;
00061 t.tv_sec += dSec;
00062 return *this;
00063 }
00064
00065 Time Time::operator+(int usecs) const {
00066 Time t2 = *this;
00067 t2 += usecs;
00068 return t2;
00069 }
00070
00071 Time::operator timeval() {
00072 return t;
00073 }
00074
00075 Time::operator struct timespec() {
00076 struct timespec t_;
00077 t_.tv_sec = t.tv_sec;
00078 t_.tv_nsec = t.tv_usec*1000;
00079 return t_;
00080 }
00081
00082 }
00083