datetime.h

00001 /*
00002  * datetime.h
00003  * This file is part of DCL
00004  *
00005  * Copyright (c) 2009 Dennis Prochko <wolfsoft@mail.ru>
00006  *
00007  * DCL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation version 3.
00010  *
00011  * DCL is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with DCL; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301  USA
00020  */
00021 
00022 #ifndef _DATETIME_H_
00023 #define _DATETIME_H_
00024 
00025 #include <ctime>
00026 #include <string>
00027 
00028 namespace dbp {
00029 
00031 #define DEFAULT_DATETIME_FORMAT "%a, %d %b %Y %H:%M:%S %Z"
00032 
00034 
00037 class datetime {
00038 public:
00040         datetime(): _t(0) { };
00042         datetime(const std::string &src, const std::string &fmt =
00043           DEFAULT_DATETIME_FORMAT);
00045         datetime(const datetime &rhs) {
00046                 _t = rhs._t;
00047         }
00049         datetime& operator=(const datetime &rhs) {
00050                 _t = rhs._t;
00051                 return *this;
00052         }
00054         bool operator==(const datetime &rhs) const {
00055                 return (_t == rhs._t);
00056         }
00058         bool operator<(const datetime &rhs) const {
00059                 return _t < rhs._t;
00060         }
00062         bool operator>(const datetime &rhs) const {
00063                 return (rhs < *this);
00064         }
00066         bool operator<=(const datetime &rhs) const {
00067                 return !(rhs < *this);
00068         }
00070         bool operator>=(const datetime& rhs) const {
00071                 return !(*this < rhs);
00072         }
00074         bool operator!=(const datetime &rhs) const {
00075                 return !(*this == rhs);
00076         }
00078         bool operator!() const {
00079                 return empty();
00080         }
00082         int operator-(const datetime& rhs);
00083         typedef bool (datetime::*bool_type)() const;
00085         operator bool_type() const {
00086                 return empty() ? NULL : &datetime::empty;
00087         }
00089         datetime& now();
00091         int year() const;
00093         datetime& year(int val);
00095         int month() const;
00097         datetime& month(int val);
00099         int day() const;
00101         datetime& day(int val);
00103         int hour() const;
00105         datetime& hour(int val);
00107         int minute() const;
00109         datetime& minute(int val);
00111         int second() const;
00113         datetime& second(int val);
00115         void clear();
00117         bool empty() const;
00119 
00159         std::string str(const std::string &format = DEFAULT_DATETIME_FORMAT) const;
00162         std::string as_gmt() const;
00163 private:
00164         time_t _t;
00165 };
00166 
00167 }
00168 
00169 #endif /*_DATETIME_H_*/
00170 

 
Support This Project
SourceForge.net Logo