00001 /* 00002 * Clock.cpp 00003 * 00004 * Created on: 24.06.2010 00005 * Author: morgenro 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 // if the quality of time is zero, return standard expire time 00034 if (Clock::quality == 0) return timestamp + lifetime; 00035 00036 // calculate sigma based on the quality of time and the original lifetime 00037 size_t sigma = lifetime * (1 - Clock::quality); 00038 00039 // expiration adjusted by quality of time 00040 return timestamp + lifetime + sigma; 00041 } 00042 00043 bool Clock::isExpired(size_t timestamp, size_t lifetime) 00044 { 00045 // if the quality of time is zero, then never expire a bundle 00046 if (Clock::quality == 0) return false; 00047 00048 if (lifetime > 0) 00049 { 00050 // calculate sigma based on the quality of time and the original lifetime 00051 size_t sigma = lifetime * (1 - Clock::quality); 00052 00053 // expiration adjusted by quality of time 00054 if ( Clock::getTime() > (timestamp + sigma)) return true; 00055 } 00056 else 00057 { 00058 // expiration based on the timestamp only 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 // timezone 00071 int offset = Clock::timezone * 3600; 00072 00073 // do we believe we are before the year 2000? 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 }
1.7.1