Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

# datetime.h

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005 /***********************************************************************
    00006  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
    00007  MySQL AB, and (c) 2004, 2005 by Educational Technology Resources, Inc.
    00008  Others may also hold copyrights on code in this file.  See the CREDITS
    00009  file in the top directory of the distribution for details.
    00010
    00011  This file is part of MySQL++.
    00012
    00013  MySQL++ is free software; you can redistribute it and/or modify it
    00014  under the terms of the GNU Lesser General Public License as published
    00015  by the Free Software Foundation; either version 2.1 of the License, or
    00016  (at your option) any later version.
    00017
    00018  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
    00019  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00020  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    00021  License for more details.
    00022
    00023  You should have received a copy of the GNU Lesser General Public
    00024  License along with MySQL++; if not, write to the Free Software
    00025  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00026  USA
    00027 ***********************************************************************/
    00028
    00029 #ifndef MYSQLPP_DATETIME_H
    00030 #define MYSQLPP_DATETIME_H
    00031
    00032 #include "common.h"
    00033
    00034 #include "coldata.h"
    00035 #include "stream2string.h"
    00036 #include "tiny_int.h"
    00037
    00038 #include <string>
    00039 #include <sstream>
    00040 #include <iostream>
    00041
    00042 namespace mysqlpp {
    00043
    00053 template <class T> struct DTbase
    00054 {
    00056         virtual ~DTbase() { }
    00057
    00059         operator std::string() const
    00060         {
    00061                 return stream2string<std::string>(*this);
    00062         }
    00063
    00068         MYSQLPP_EXPORT virtual short compare(const T& other) const = 0;
    00069
    00071         bool operator ==(const T& other) const
    00072         {
    00073                 return !compare(other);
    00074         }
    00075
    00077         bool operator !=(const T& other) const
    00078         {
    00079                 return compare(other);
    00080         }
    00081
    00083         bool operator <(const T& other) const
    00084         {
    00085                 return compare(other) < 0;
    00086         }
    00087
    00089         bool operator <=(const T& other) const
    00090         {
    00091                 return compare(other) <= 0;
    00092         }
    00093
    00095         bool operator >(const T& other) const
    00096         {
    00097                 return compare(other) > 0;
    00098         }
    00099
    00101         bool operator >=(const T& other) const
    00102         {
    00103                 return compare(other) >= 0;
    00104         }
    00105 };
    00106
    00107
    00112 struct DateTime : public DTbase<DateTime>
    00113 {
    00117         short int year;
    00118
    00120         tiny_int month;
    00121
    00123         tiny_int day;
    00124
    00126         tiny_int hour;
    00127
    00129         tiny_int minute;
    00130
    00132         tiny_int second;
    00133
    00135         DateTime() :
    00136         DTbase<DateTime>(),
    00137         year(0),
    00138         month(0),
    00139         day(0),
    00140         hour(0),
    00141         minute(0),
    00142         second(0)
    00143         {
    00144         }
    00145
    00147         DateTime(const DateTime& other) :
    00148         DTbase<DateTime>(),
    00149         year(other.year),
    00150         month(other.month),
    00151         day(other.day),
    00152         hour(other.hour),
    00153         minute(other.minute),
    00154         second(other.second)
    00155         {
    00156         }
    00157
    00162         DateTime(cchar* str) { convert(str); }
    00163
    00167         DateTime(const ColData& str)
    00168         {
    00169                 convert(str.c_str());
    00170         }
    00171
    00175         DateTime(const std::string& str)
    00176         {
    00177                 convert(str.c_str());
    00178         }
    00179
    00181         DateTime(time_t t);
    00182
    00190         MYSQLPP_EXPORT short compare(const DateTime& other) const;
    00191
    00193         MYSQLPP_EXPORT cchar* convert(cchar*);
    00194
    00196         operator time_t() const;
    00197 };
    00198
    00199
    00208 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
    00209                 const DateTime& dt);
    00210
    00211
    00216 struct Date : public DTbase<Date>
    00217 {
    00221         short int year;
    00222
    00224         tiny_int month;
    00225
    00227         tiny_int day;
    00228
    00230         Date() : year(0), month(0), day(0) { }
    00231
    00233         Date(short int y, tiny_int m, tiny_int d) :
    00234         DTbase<Date>(),
    00235         year(y),
    00236         month(m),
    00237         day(d)
    00238         {
    00239         }
    00240
    00242         Date(const Date& other) :
    00243         DTbase<Date>(),
    00244         year(other.year),
    00245         month(other.month),
    00246         day(other.day)
    00247         {
    00248         }
    00249
    00251         Date(const DateTime& other) :
    00252         DTbase<Date>(),
    00253         year(other.year),
    00254         month(other.month),
    00255         day(other.day)
    00256         {
    00257         }
    00258
    00263         Date(cchar* str) { convert(str); }
    00264
    00268         Date(const ColData& str) { convert(str.c_str()); }
    00269
    00273         Date(const std::string& str)
    00274         {
    00275                 convert(str.c_str());
    00276         }
    00277
    00282         MYSQLPP_EXPORT short int compare(const Date& other) const;
    00283
    00285         MYSQLPP_EXPORT cchar* convert(cchar*);
    00286 };
    00287
    00294 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
    00295                 const Date& d);
    00296
    00297
    00302 struct Time : public DTbase<Time>
    00303 {
    00305         tiny_int hour;
    00306
    00308         tiny_int minute;
    00309
    00311         tiny_int second;
    00312
    00314         Time() : hour(0), minute(0), second(0) { }
    00315
    00317         Time(tiny_int h, tiny_int m, tiny_int s) :
    00318         hour(h),
    00319         minute(m),
    00320         second(s)
    00321         {
    00322         }
    00323
    00325         Time(const Time& other) :
    00326         DTbase<Time>(),
    00327         hour(other.hour),
    00328         minute(other.minute),
    00329         second(other.second)
    00330         {
    00331         }
    00332
    00334         Time(const DateTime& other) :
    00335         DTbase<Time>(),
    00336         hour(other.hour),
    00337         minute(other.minute),
    00338         second(other.second)
    00339         {
    00340         }
    00341
    00346         Time(cchar* str) { convert(str); }
    00347
    00351         Time(const ColData& str) { convert(str.c_str()); }
    00352
    00356         Time(const std::string& str)
    00357         {
    00358                 convert(str.c_str());
    00359         }
    00360
    00362         MYSQLPP_EXPORT cchar* convert(cchar*);
    00363
    00368         MYSQLPP_EXPORT short int compare(const Time& other) const;
    00369 };
    00370
    00378 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
    00379                 const Time& t);
    00380
    00381
    00382 } // end namespace mysqlpp
    00383
    00384 #endif // !defined(MYSQLPP_DATETIME_H)