Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGDate.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #include <iostream>
00040 #include <iomanip>
00041 
00042 #include <time.h>
00043 
00044 #include "OSGDate.h"
00045 
00046 OSG_USING_NAMESPACE
00047 
00048 
00049 Date::Date(void) :
00050     _second(0), 
00051     _minute(0), 
00052     _hour  (0), 
00053     _day   (0), 
00054     _month (0), 
00055     _year  (0)
00056 {
00057 }
00058 
00059 
00060 Date::Date(const Date &obj) : 
00061     _second(obj._second), 
00062     _minute(obj._minute), 
00063     _hour  (obj._hour  ),
00064     _day   (obj._day   ), 
00065     _month (obj._month ), 
00066     _year  (obj._year  )
00067 {
00068 }
00069 
00070 
00071 Date::~Date(void)
00072 {
00073 }
00074 
00075 
00076 void Date::setSystemDate(void)
00077 {
00078            time_t  clock;
00079     struct tm     *tm_time;
00080 
00081     time(&clock);
00082 
00083     tm_time = localtime(&clock);
00084 
00085     _second = tm_time->tm_sec;
00086     _minute = tm_time->tm_min;
00087     _hour   = tm_time->tm_hour;
00088     _day    = tm_time->tm_mday;
00089     _month  = tm_time->tm_mon + 1;
00090     _year   = tm_time->tm_year + 1900;
00091 }
00092 
00093 
00094 void Date::set(const Char8 *stringP)
00095 {
00096     if(stringP != NULL&& *stringP != '\0')
00097     {
00098 //        istrstream is(string);
00099         // is >> *this;
00100     }
00101 }
00102 
00103 
00104 bool Date::isLeapYear(void)
00105 {
00106     if(_year & 3)
00107     {
00108         return false;
00109     }
00110 
00111     if((_year % 100) || !(_year % 400))
00112     {
00113         return true;
00114     }
00115 
00116     return false;
00117 }
00118 
00119 
00120 bool Date::valid(void)
00121 {
00122     static const UInt32 daysPerMonth[12] =
00123     { 
00124         31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31 
00125     };
00126 
00127     if((_hour > 23) || (_minute > 59) || (_second > 59))
00128     {
00129         return false;
00130     }
00131 
00132     if((_month < 1) || (_day < 1) || (_month > 12))
00133     {
00134         return false;
00135     }
00136 
00137     if(_day <= daysPerMonth[_month - 1])
00138     {
00139         return true;
00140     }
00141 
00142     if((_month == 2) && (_day == 29))
00143     {
00144         return isLeapYear();
00145     }
00146 
00147     return false;
00148 }
00149 
00150 
00151 void Date::set(UInt32 day,  
00152                UInt32 month,  
00153                 Int32 year,
00154                UInt32 hour, 
00155                UInt32 minute, 
00156                UInt32 sec   )
00157 {
00158     _day    = day;
00159     _month  = month;
00160     _year   = year;
00161     _hour   = hour;
00162     _minute = minute;
00163     _second = sec;
00164 }
00165 
00166 
00167 bool Date::operator ==(const Date &other)
00168 {
00169     return ((_day    == other._day   ) &&
00170             (_month  == other._month ) &&
00171             (_year   == other._year  ) &&
00172             (_hour   == other._hour  ) &&
00173             (_minute == other._minute) &&
00174             (_second == other._second)  );
00175 }
00176 
00177 
00178 bool Date::operator <(const Date &other)
00179 {
00180     if(_year < other._year)
00181         return true;
00182 
00183     if(_year > other._year)
00184         return false;
00185 
00186     // _year == other._year
00187 
00188     if(_month < other._month)
00189         return true;
00190 
00191     if(_month > other._month)
00192         return false;
00193 
00194     // _month == other._month
00195 
00196     if(_day < other._day)
00197         return true;
00198     if(_day > other._day)
00199         return false;
00200 
00201     // _day == other._day
00202 
00203     if(_hour < other._hour)
00204         return true;
00205     if(_hour > other._hour)
00206         return false;
00207 
00208     // _hour == other._hour
00209 
00210     if(_minute < other._minute)
00211         return true;
00212     if(_minute > other._minute)
00213         return false;
00214 
00215     // _minute == other._minute
00216 
00217     if(_second < other._second)
00218         return true;
00219 
00220     return false;
00221 }
00222 
00223 
00224 std::ostream &OSG::operator <<(std::ostream &outStream, const Date &obj)
00225 {
00226   return  outStream << std::setfill('0')
00227                     << std::setw(2) << obj._day    << '.'
00228                     << std::setw(2) << obj._month  << '.'
00229                     << obj._year                   << ' '
00230                     << std::setw(2) << obj._hour   << ':'
00231                     << std::setw(2) << obj._minute << ':'
00232                     << std::setw(2) << obj._second;
00233 }
00234 
00235 
00236 std::istream &OSG::operator >>(std::istream &inStream, Date &obj)
00237 {
00238     Char8 c;
00239     
00240     return inStream >> obj._day  >> c >> obj._month  >> c >> obj._year
00241                     >> obj._hour >> c >> obj._minute >> c >> obj._second;
00242 }
00243 

Generated on Thu Aug 25 04:02:31 2005 for OpenSG by  doxygen 1.4.3