Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/tntdb/include/tntdb/datetime.h

00001 /*
00002  * Copyright (C) 2005 Tommi Maekitalo
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 
00029 #ifndef TNTDB_DATETIME_H
00030 #define TNTDB_DATETIME_H
00031 
00032 #include <string>
00033 #include <tntdb/date.h>
00034 #include <tntdb/time.h>
00035 
00036 namespace tntdb
00037 {
00041   class Datetime
00042   {
00043       unsigned short year;
00044       unsigned short month;
00045       unsigned short day;
00046       unsigned short hour;
00047       unsigned short minute;
00048       unsigned short second;
00049       unsigned short millis;
00050 
00051     public:
00053       Datetime()
00054         : year(0),
00055           month(0),
00056           day(0),
00057           hour(0),
00058           minute(0),
00059           second(0),
00060           millis(0)
00061           { }
00062 
00063       Datetime(const Date& date, const Time& time)
00064         : year(date.getYear()),
00065           month(date.getMonth()),
00066           day(date.getDay()),
00067           hour(time.getHour()),
00068           minute(time.getMinute()),
00069           second(time.getSecond()),
00070           millis(time.getMillis())
00071           { }
00072 
00075       Datetime(unsigned short year_,
00076                unsigned short month_,
00077                unsigned short day_,
00078                unsigned short hour_,
00079                unsigned short minute_,
00080                unsigned short second_,
00081                unsigned short millis_ = 0)
00082         : year(year_),
00083           month(month_),
00084           day(day_),
00085           hour(hour_),
00086           minute(minute_),
00087           second(second_),
00088           millis(millis_)
00089           { }
00090 
00091       static Datetime localtime();
00092       static Datetime gmtime();
00093 
00095       unsigned short getYear() const     { return year; }
00097       unsigned short getMonth() const    { return month; }
00099       unsigned short getDay() const      { return day; }
00101       unsigned short getHour() const     { return hour; }
00103       unsigned short getMinute() const   { return minute; }
00105       unsigned short getSecond() const   { return second; }
00107       unsigned short getMillis() const   { return millis; }
00108 
00109       Date getDate() const  { return Date(year, month, day); }
00110       Time getTime() const  { return Time(hour, minute, second, millis); }
00111 
00112       bool isNull() const  { return month == 0; }
00113 
00115       std::string getIso() const;
00118       static Datetime fromIso(const std::string& s);
00119 
00122       void set(unsigned short year_,
00123                unsigned short month_,
00124                unsigned short day_,
00125                unsigned short hour_,
00126                unsigned short minute_,
00127                unsigned short second_,
00128                unsigned short millis_ = 0)
00129       {
00130         year = year_;
00131         month = month_;
00132         day = day_;
00133         hour = hour_;
00134         minute = minute_;
00135         second = second_;
00136         millis = millis_;
00137       }
00138 
00139       bool operator== (const Datetime& dt) const
00140       { return year == dt.year
00141             && month == dt.month
00142             && day == dt.day
00143             && hour == dt.hour
00144             && minute == dt.minute
00145             && second == dt.second
00146             && millis == dt.millis; }
00147 
00148       bool operator!= (const Datetime& dt) const
00149         { return !operator==(dt); }
00150 
00151       bool operator< (const Datetime& dt) const
00152       {
00153         return year   < dt.year   ? true
00154              : year   > dt.year   ? false
00155              : month  < dt.month  ? true
00156              : month  > dt.month  ? false
00157              : day    < dt.day    ? true
00158              : day    > dt.day    ? false
00159              : hour   < dt.hour   ? true
00160              : hour   > dt.hour   ? false
00161              : minute < dt.minute ? true
00162              : minute > dt.minute ? false
00163              : second < dt.second ? true
00164              : second > dt.second ? false
00165              : millis < dt.millis;
00166       }
00167 
00168       bool operator> (const Datetime& dt) const
00169         { return dt < *this; }
00170 
00171       bool operator<= (const Datetime& dt) const
00172         { return !(*this > dt); }
00173 
00174       bool operator>= (const Datetime& dt) const
00175         { return !(*this < dt); }
00176   };
00177 }
00178 
00179 #endif // TNTDB_DATETIME_H
00180 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6