User Manual, Developers Guide and API Documentation

DateTime.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 // Dependencies
00029 
00030 #include <WNS/module/DateTime.hpp>
00031 #include <WNS/Exception.hpp>
00032 
00033 #include <string>
00034 #include <sstream>
00035 #include <iomanip>
00036 #include <ctime>
00037 
00038 using namespace std;
00039 using namespace wns::module;
00040 
00041 // Constructor
00042 
00043 // default constructor
00044 DateTime::DateTime()
00045   : time(0), empty(true)
00046 {
00047 }
00048 
00049 // time_t constructor
00050 DateTime::DateTime(const time_t t)
00051   : time(t), empty(false)
00052 {
00053     if(t == 0) empty=true;
00054 }
00055 
00056 // Operators
00057 
00058 bool DateTime::operator==(const DateTime b) const
00059 {
00060   return empty || b.empty || (time == b.time);
00061 }
00062 
00063 bool DateTime::operator!=(const DateTime b) const
00064 {
00065   return empty || b.empty || (time != b.time);
00066 }
00067 
00068 bool DateTime::operator>(const DateTime b) const
00069 {
00070   return empty || b.empty || (time > b.time);
00071 }
00072 
00073 bool DateTime::operator<(const DateTime b) const
00074 {
00075   return empty || b.empty || (time < b.time);
00076 }
00077 
00078 bool DateTime::operator>=(const DateTime b) const
00079 {
00080   return empty || b.empty || (time >= b.time);
00081 }
00082 
00083 bool DateTime::operator<=(const DateTime b) const
00084 {
00085   return empty || b.empty || (time <= b.time);
00086 }
00087 
00088 // Accessors
00089 time_t DateTime::getTime() const
00090 {
00091   return time;
00092 }
00093 
00094 string DateTime::getString() const
00095 {
00096   if(empty)
00097     return "";
00098 
00099   struct tm tm;
00100   if(NULL == localtime_r(&time, &tm)) {
00101               // i can hardly think of any error causing this function call
00102               // to fail.
00103     throw Exception("Please report this exception to osz@illator.de.");
00104   }
00105 
00106   std::stringstream ss;
00107   ss << 1900 + tm.tm_year;
00108   ss << "-" << std::setfill('0') << std::setw(2) << 1 + tm.tm_mon;
00109   ss << "-" << std::setfill('0') << std::setw(2) << tm.tm_mday;
00110   ss << " " << std::setfill('0') << std::setw(2) << tm.tm_hour;
00111   ss << ":" << std::setfill('0') << std::setw(2) << tm.tm_min;
00112   ss << ":" << std::setfill('0') << std::setw(2) << tm.tm_sec;
00113 
00114   return ss.str();
00115 }
00116 
00117 bool DateTime::isEmpty() const
00118 {
00119   return empty;
00120 }
00121 

Generated on Wed May 23 03:31:44 2012 for openWNS by  doxygen 1.5.5