- Main Page
- Classes
- Files

- File List
- 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-2001 by MySQL AB, and
    00007  (c) 2004-2008 by Educational Technology Resources, Inc.  Others may
    00008  also hold copyrights on code in this file.  See the CREDITS file in
    00009  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 #if !defined(MYSQLPP_DATETIME_H)
    00030 #define MYSQLPP_DATETIME_H
    00031
    00032 #include "common.h"
    00033
    00034 #include "comparable.h"
    00035
    00036 #include <string>
    00037 #include <iostream>
    00038
    00039 namespace mysqlpp {
    00040
    00047
    00048 class MYSQLPP_EXPORT DateTime : public Comparable<DateTime>
    00049 {
    00050 public:
    00052         DateTime() :
    00053         Comparable<DateTime>(),
    00054         year_(0),
    00055         month_(0),
    00056         day_(0),
    00057         hour_(0),
    00058         minute_(0),
    00059         second_(0),
    00060         now_(true)
    00061         {
    00062         }
    00063
    00072         DateTime(unsigned short y, unsigned char mon, unsigned char d,
    00073                         unsigned char h, unsigned char min, unsigned char s) :
    00074         Comparable<DateTime>(),
    00075         year_(y),
    00076         month_(mon),
    00077         day_(d),
    00078         hour_(h),
    00079         minute_(min),
    00080         second_(s),
    00081         now_(false)
    00082         {
    00083         }
    00084
    00086         DateTime(const DateTime& other) :
    00087         Comparable<DateTime>(),
    00088         year_(other.year_),
    00089         month_(other.month_),
    00090         day_(other.day_),
    00091         hour_(other.hour_),
    00092         minute_(other.minute_),
    00093         second_(other.second_),
    00094         now_(other.now_)
    00095         {
    00096         }
    00097
    00103         explicit DateTime(const char* str) { convert(str); }
    00104
    00112         template <class Str>
    00113         explicit DateTime(const Str& str)
    00114         {
    00115                 convert(str.c_str());
    00116         }
    00117
    00119         explicit DateTime(time_t t);
    00120
    00125         int compare(const DateTime& other) const;
    00126
    00128         const char* convert(const char*);
    00129
    00131         unsigned char day() const { return day_; }
    00132
    00134         void day(unsigned char d) { day_ = d; now_ = false; }
    00135
    00137         unsigned char hour() const { return hour_; }
    00138
    00140         void hour(unsigned char h) { hour_ = h; now_ = false; }
    00141
    00144         bool is_now() const { return now_; }
    00145
    00147         unsigned char minute() const { return minute_; }
    00148
    00150         void minute(unsigned char m) { minute_ = m; now_ = false; }
    00151
    00153         unsigned char month() const { return month_; }
    00154
    00156         void month(unsigned char m) { month_ = m; now_ = false; }
    00157
    00162         static DateTime now() { return DateTime(); }
    00163
    00165         operator std::string() const;
    00166
    00168         operator time_t() const;
    00169
    00171         unsigned char second() const { return second_; }
    00172
    00174         void second(unsigned char s) { second_ = s; now_ = false; }
    00175
    00177         std::string str() const { return *this; }
    00178
    00183         unsigned short year() const { return year_; }
    00184
    00189         void year(unsigned short y) { year_ = y; now_ = false; }
    00190
    00191 private:
    00192         unsigned short year_;
    00193         unsigned char month_;
    00194         unsigned char day_;
    00195         unsigned char hour_;
    00196         unsigned char minute_;
    00197         unsigned char second_;
    00198
    00199         bool now_;
    00200 };
    00201
    00202
    00211 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
    00212                 const DateTime& dt);
    00213
    00214
    00219 class MYSQLPP_EXPORT Date : public Comparable<Date>
    00220 {
    00221 public:
    00223         Date() : year_(0), month_(0), day_(0) { }
    00224
    00226         Date(unsigned short y, unsigned char m, unsigned char d) :
    00227         Comparable<Date>(),
    00228         year_(y),
    00229         month_(m),
    00230         day_(d)
    00231         {
    00232         }
    00233
    00235         Date(const Date& other) :
    00236         Comparable<Date>(),
    00237         year_(other.year_),
    00238         month_(other.month_),
    00239         day_(other.day_)
    00240         {
    00241         }
    00242
    00244         Date(const DateTime& other) :
    00245         Comparable<Date>(),
    00246         year_(other.year()),
    00247         month_(other.month()),
    00248         day_(other.day())
    00249         {
    00250         }
    00251
    00256         explicit Date(const char* str) { convert(str); }
    00257
    00264         template <class Str>
    00265         explicit Date(const Str& str) { convert(str.c_str()); }
    00266
    00271         explicit Date(time_t t);
    00272
    00277         int compare(const Date& other) const;
    00278
    00280         const char* convert(const char*);
    00281
    00283         unsigned char day() const { return day_; }
    00284
    00286         void day(unsigned char d) { day_ = d; }
    00287
    00289         unsigned char month() const { return month_; }
    00290
    00292         void month(unsigned char m) { month_ = m; }
    00293
    00295         operator std::string() const;
    00296
    00300         operator time_t() const;
    00301
    00303         std::string str() const { return *this; }
    00304
    00309         unsigned short year() const { return year_; }
    00310
    00315         void year(unsigned short y) { year_ = y; }
    00316
    00317 private:
    00318         unsigned short year_;
    00319         unsigned char month_;
    00320         unsigned char day_;
    00321 };
    00322
    00329 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
    00330                 const Date& d);
    00331
    00332
    00337 class MYSQLPP_EXPORT Time : public Comparable<Time>
    00338 {
    00339 public:
    00341         Time() : hour_(0), minute_(0), second_(0) { }
    00342
    00344         Time(unsigned char h, unsigned char m, unsigned char s) :
    00345         hour_(h),
    00346         minute_(m),
    00347         second_(s)
    00348         {
    00349         }
    00350
    00352         Time(const Time& other) :
    00353         Comparable<Time>(),
    00354         hour_(other.hour_),
    00355         minute_(other.minute_),
    00356         second_(other.second_)
    00357         {
    00358         }
    00359
    00361         Time(const DateTime& other) :
    00362         Comparable<Time>(),
    00363         hour_(other.hour()),
    00364         minute_(other.minute()),
    00365         second_(other.second())
    00366         {
    00367         }
    00368
    00374         explicit Time(const char* str) { convert(str); }
    00375
    00383         template <class Str>
    00384         explicit Time(const Str& str) { convert(str.c_str()); }
    00385
    00390         explicit Time(time_t t);
    00391
    00396         int compare(const Time& other) const;
    00397
    00399         const char* convert(const char*);
    00400
    00402         unsigned char hour() const { return hour_; }
    00403
    00405         void hour(unsigned char h) { hour_ = h; }
    00406
    00408         unsigned char minute() const { return minute_; }
    00409
    00411         void minute(unsigned char m) { minute_ = m; }
    00412
    00414         operator std::string() const;
    00415
    00419         operator time_t() const;
    00420
    00422         unsigned char second() const { return second_; }
    00423
    00425         void second(unsigned char s) { second_ = s; }
    00426
    00428         std::string str() const { return *this; }
    00429
    00430 private:
    00431         unsigned char hour_;
    00432         unsigned char minute_;
    00433         unsigned char second_;
    00434 };
    00435
    00443 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
    00444                 const Time& t);
    00445
    00446
    00447 } // end namespace mysqlpp
    00448
    00449 #endif // !defined(MYSQLPP_DATETIME_H)