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

/home/tommi/tntdb/include/tntdb/time.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_TIME_H
00030 #define TNTDB_TIME_H
00031 
00032 #include <string>
00033 
00034 namespace tntdb
00035 {
00039   class Time
00040   {
00041       short hour;
00042       unsigned short minute;
00043       unsigned short second;
00044       unsigned short millis;
00045 
00046     public:
00048       Time()
00049         : hour(-1),
00050           minute(0),
00051           second(0),
00052           millis(0)
00053           { }
00054 
00057       Time(unsigned short hour_,
00058            unsigned short minute_,
00059            unsigned short second_,
00060            unsigned short millis_ = 0)
00061         : hour(hour_),
00062           minute(minute_),
00063           second(second_),
00064           millis(millis_)
00065           { }
00066 
00067       static Time localtime();
00068       static Time gmtime();
00069 
00071       unsigned short getHour() const     { return static_cast<unsigned short>(hour); }
00073       unsigned short getMinute() const   { return minute; }
00075       unsigned short getSecond() const   { return second; }
00077       unsigned short getMillis() const   { return millis; }
00078 
00079       bool isNull() const  { return hour < 0; }
00080 
00083       void set(unsigned short hour_,
00084                unsigned short minute_,
00085                unsigned short second_,
00086                unsigned short millis_ = 0)
00087       {
00088         hour = static_cast<short>(hour_);
00089         minute = minute_;
00090         second = second_;
00091         millis = millis_;
00092       }
00093 
00095       std::string getIso() const;
00098       static Time fromIso(const std::string& s);
00099 
00100       bool operator== (const Time& dt) const
00101       { return hour == dt.hour
00102             && minute == dt.minute
00103             && second == dt.second
00104             && millis == dt.millis; }
00105 
00106       bool operator!= (const Time& dt) const
00107         { return !operator==(dt); }
00108 
00109       bool operator< (const Time& dt) const
00110       {
00111         return hour   < dt.hour   ? true
00112              : hour   > dt.hour   ? false
00113              : minute < dt.minute ? true
00114              : minute > dt.minute ? false
00115              : second < dt.second ? true
00116              : second > dt.second ? false
00117              : millis < dt.millis;
00118       }
00119 
00120       bool operator> (const Time& dt) const
00121         { return dt < *this; }
00122 
00123       bool operator<= (const Time& dt) const
00124         { return !(*this > dt); }
00125 
00126       bool operator>= (const Time& dt) const
00127         { return !(*this < dt); }
00128   };
00129 }
00130 
00131 #endif // TNTDB_TIME_H
00132 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6