Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound 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 "defs.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
    00045 struct mysql_dt_base
    00046 {
    00047 #if !defined(DOXYGEN_IGNORE)
    00048 // Doxygen will not generate documentation for this section.
    00049         virtual std::ostream& out_stream(std::ostream&) const = 0;
    00050         operator std::string() const
    00051         {
    00052                 return stream2string<std::string>(*this);
    00053         }
    00054 #endif // !defined(DOXYGEN_IGNORE)
    00055 };
    00056
    00057
    00064 template <class T> struct DTbase
    00065 {
    00070         virtual short int compare(const T& other) const = 0;
    00071
    00073         bool operator ==(const T& other) const { return !compare(other); }
    00074
    00076         bool operator !=(const T& other) const { return compare(other); }
    00077
    00079         bool operator <(const T& other) const { return compare(other) < 0; }
    00080
    00082         bool operator <=(const T& other) const { return compare(other) <= 0; }
    00083
    00085         bool operator >(const T& other) const { return compare(other) > 0; }
    00086
    00088         bool operator >=(const T& other) const { return compare(other) >= 0; }
    00089 };
    00090
    00091
    00093 struct mysql_date : virtual public mysql_dt_base
    00094 {
    00098         short int year;
    00099
    00101         tiny_int month;
    00102
    00104         tiny_int day;
    00105
    00111         std::ostream& out_stream(std::ostream& os) const;
    00112
    00114         cchar* convert(cchar*);
    00115
    00116 protected:
    00124         short int compare(const mysql_date* other) const;
    00125 };
    00126
    00127
    00132
    00133 struct Date : public mysql_date, public DTbase<Date>
    00134 {
    00136         Date() { };
    00137
    00142         Date(cchar* str) { convert(str); }
    00143
    00147         Date(const ColData& str) { convert(str.c_str()); }
    00148
    00152         Date(const std::string& str) { convert(str.c_str()); }
    00153
    00157         short int compare(const Date& other) const
    00158         {
    00159                 return mysql_date::compare(&other);
    00160         }
    00161 };
    00162
    00163
    00166
    00167 inline std::ostream& operator <<(std::ostream& s, const Date& d)
    00168 {
    00169         return d.out_stream(s);
    00170 }
    00171
    00172
    00174
    00175 struct mysql_time : virtual public mysql_dt_base
    00176 {
    00178         tiny_int hour;
    00179
    00181         tiny_int minute;
    00182
    00184         tiny_int second;
    00185
    00191         std::ostream& out_stream(std::ostream& os) const;
    00192
    00194         cchar* convert(cchar*);
    00195
    00196 protected:
    00204         short int compare(const mysql_time* other) const;
    00205 };
    00206
    00207
    00212 struct Time : public mysql_time, public DTbase<Time>
    00213 {
    00215         Time() { };
    00216
    00221         Time(cchar* str)
    00222         {
    00223                 convert(str);
    00224         }
    00225
    00229         Time(const ColData& str);
    00230
    00234         Time(const std::string& str);
    00235
    00239         short int compare(const Time& other) const
    00240         {
    00241                 return mysql_time::compare(&other);
    00242         }
    00243 };
    00244
    00245
    00248
    00249 inline std::ostream& operator <<(std::ostream& s, const Time& d)
    00250 {
    00251         return d.out_stream(s);
    00252 }
    00253
    00254
    00260
    00261 struct DateTime : public mysql_date, public mysql_time,
    00262                 public DTbase<DateTime>
    00263 {
    00265         DateTime() { }
    00266
    00271         DateTime(cchar* str)
    00272         {
    00273                 convert(str);
    00274         }
    00275
    00279         DateTime(const ColData& str);
    00280
    00284         DateTime(const std::string& str);
    00285
    00293         short int compare(const DateTime& other) const;
    00294
    00301         std::ostream& out_stream(std::ostream& os) const;
    00302
    00304         cchar* convert(cchar*);
    00305 };
    00306
    00307
    00310 inline std::ostream& operator <<(std::ostream& s, const DateTime& d)
    00311 {
    00312         return d.out_stream(s);
    00313 }
    00314
    00315
    00316 inline Time::Time(const ColData& str)
    00317 {
    00318         convert(str.c_str());
    00319 }
    00320
    00321
    00322 inline Time::Time(const std::string& str)
    00323 {
    00324         convert(str.c_str());
    00325 }
    00326
    00327
    00328 inline DateTime::DateTime(const ColData& str)
    00329 {
    00330         convert(str.c_str());
    00331 }
    00332
    00333
    00334 inline DateTime::DateTime(const std::string& str)
    00335 {
    00336         convert(str.c_str());
    00337 }
    00338
    00339 } // end namespace mysqlpp
    00340
    00341 #endif // !defined(MYSQLPP_DATETIME_H)