00001
00002
00003
00004
00005
00006
00007
00008 #include "ibrdtn/utils/Clock.h"
00009 #include <sys/time.h>
00010
00011 namespace dtn
00012 {
00013 namespace utils
00014 {
00015 int Clock::timezone = 0;
00016 float Clock::quality = 0;
00017
00021 u_int32_t Clock::TIMEVAL_CONVERSION = 946684800;
00022
00023 Clock::Clock()
00024 {
00025 }
00026
00027 Clock::~Clock()
00028 {
00029 }
00030
00031 size_t Clock::getExpireTime(size_t timestamp, size_t lifetime)
00032 {
00033
00034 if (Clock::quality == 0) return timestamp + lifetime;
00035
00036
00037 size_t sigma = lifetime * (1 - Clock::quality);
00038
00039
00040 return timestamp + lifetime + sigma;
00041 }
00042
00043 bool Clock::isExpired(size_t timestamp, size_t lifetime)
00044 {
00045
00046 if (Clock::quality == 0) return false;
00047
00048 if (lifetime > 0)
00049 {
00050
00051 size_t sigma = lifetime * (1 - Clock::quality);
00052
00053
00054 if ( Clock::getTime() > (timestamp + sigma)) return true;
00055 }
00056 else
00057 {
00058
00059 if ( Clock::getTime() > timestamp) return true;
00060 }
00061
00062 return false;
00063 }
00064
00065 size_t Clock::getTime()
00066 {
00067 struct timeval now;
00068 ::gettimeofday(&now, 0);
00069
00070
00071 int offset = Clock::timezone * 3600;
00072
00073
00074 if ((u_int)now.tv_sec < TIMEVAL_CONVERSION)
00075 {
00076 return 0;
00077 }
00078
00079 return (now.tv_sec - TIMEVAL_CONVERSION) + offset;
00080 }
00081 }
00082 }