IBR-DTNSuite 0.6

ibrdtn/ibrdtn/data/DTNTime.cpp

Go to the documentation of this file.
00001 /*
00002  * DTNTime.cpp
00003  *
00004  *  Created on: 15.12.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrdtn/data/DTNTime.h"
00009 #include "ibrdtn/utils/Clock.h"
00010 #include <sys/time.h>
00011 
00012 namespace dtn
00013 {
00014         namespace data
00015         {
00016                 DTNTime::DTNTime()
00017                  : _seconds(0), _nanoseconds(0)
00018                 {
00019                         set();
00020                 }
00021 
00022                 DTNTime::DTNTime(size_t seconds, size_t nanoseconds)
00023                  : _seconds(seconds), _nanoseconds(nanoseconds)
00024                 {
00025                 }
00026 
00027                 DTNTime::DTNTime(SDNV seconds, SDNV nanoseconds)
00028                  : _seconds(seconds), _nanoseconds(nanoseconds)
00029                 {
00030                 }
00031 
00032                 DTNTime::~DTNTime()
00033                 {
00034                 }
00035 
00036                 void DTNTime::set()
00037                 {
00038                         _seconds = dtn::utils::Clock::getTime();
00039                         timeval tv;
00040 
00041                         if ( gettimeofday(&tv, NULL) )
00042                         {
00043                                 _nanoseconds = SDNV(tv.tv_usec * 1000);
00044                         }
00045                 }
00046 
00047                 size_t DTNTime::getLength() const
00048                 {
00049                         return _seconds.getLength() + _nanoseconds.getLength();
00050                 }
00051 
00052                 SDNV DTNTime::getTimestamp()
00053                 {
00054                         return _seconds;
00055                 }
00056 
00057                 void DTNTime::operator+=(const size_t value)
00058                 {
00059                         _seconds += value;
00060                 }
00061 
00062                 std::ostream& operator<<(std::ostream &stream, const dtn::data::DTNTime &obj)
00063                 {
00064                         stream << obj._seconds << obj._nanoseconds;
00065                         return stream;
00066                 }
00067 
00068                 std::istream& operator>>(std::istream &stream, dtn::data::DTNTime &obj)
00069                 {
00070                         stream >> obj._seconds;
00071                         stream >> obj._nanoseconds;
00072                         return stream;
00073                 }
00074         }
00075 }