00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00184 unsigned short year() const { return year_; }
00185
00190 void year(unsigned short y) { year_ = y; now_ = false; }
00191
00192 private:
00193 unsigned short year_;
00194 unsigned char month_;
00195 unsigned char day_;
00196 unsigned char hour_;
00197 unsigned char minute_;
00198 unsigned char second_;
00199
00200 bool now_;
00201 };
00202
00203
00206 inline DateTime NOW() { return DateTime(); }
00207
00208
00217 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
00218 const DateTime& dt);
00219
00220
00225 class MYSQLPP_EXPORT Date : public Comparable<Date>
00226 {
00227 public:
00229 Date() : year_(0), month_(0), day_(0) { }
00230
00236 Date(unsigned short y, unsigned char m, unsigned char d) :
00237 Comparable<Date>(),
00238 year_(y),
00239 month_(m),
00240 day_(d)
00241 {
00242 }
00243
00245 Date(const Date& other) :
00246 Comparable<Date>(),
00247 year_(other.year_),
00248 month_(other.month_),
00249 day_(other.day_)
00250 {
00251 }
00252
00254 Date(const DateTime& other) :
00255 Comparable<Date>(),
00256 year_(other.year()),
00257 month_(other.month()),
00258 day_(other.day())
00259 {
00260 }
00261
00266 explicit Date(const char* str) { convert(str); }
00267
00274 template <class Str>
00275 explicit Date(const Str& str) { convert(str.c_str()); }
00276
00281 explicit Date(time_t t);
00282
00287 int compare(const Date& other) const;
00288
00290 const char* convert(const char*);
00291
00293 unsigned char day() const { return day_; }
00294
00296 void day(unsigned char d) { day_ = d; }
00297
00299 unsigned char month() const { return month_; }
00300
00302 void month(unsigned char m) { month_ = m; }
00303
00305 operator std::string() const;
00306
00310 operator time_t() const;
00311
00313 std::string str() const { return *this; }
00314
00319 unsigned short year() const { return year_; }
00320
00325 void year(unsigned short y) { year_ = y; }
00326
00327 private:
00328 unsigned short year_;
00329 unsigned char month_;
00330 unsigned char day_;
00331 };
00332
00339 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
00340 const Date& d);
00341
00342
00347 class MYSQLPP_EXPORT Time : public Comparable<Time>
00348 {
00349 public:
00351 Time() : hour_(0), minute_(0), second_(0) { }
00352
00357 Time(unsigned char h, unsigned char m, unsigned char s) :
00358 hour_(h),
00359 minute_(m),
00360 second_(s)
00361 {
00362 }
00363
00365 Time(const Time& other) :
00366 Comparable<Time>(),
00367 hour_(other.hour_),
00368 minute_(other.minute_),
00369 second_(other.second_)
00370 {
00371 }
00372
00374 Time(const DateTime& other) :
00375 Comparable<Time>(),
00376 hour_(other.hour()),
00377 minute_(other.minute()),
00378 second_(other.second())
00379 {
00380 }
00381
00387 explicit Time(const char* str) { convert(str); }
00388
00396 template <class Str>
00397 explicit Time(const Str& str) { convert(str.c_str()); }
00398
00403 explicit Time(time_t t);
00404
00409 int compare(const Time& other) const;
00410
00412 const char* convert(const char*);
00413
00415 unsigned char hour() const { return hour_; }
00416
00418 void hour(unsigned char h) { hour_ = h; }
00419
00421 unsigned char minute() const { return minute_; }
00422
00424 void minute(unsigned char m) { minute_ = m; }
00425
00427 operator std::string() const;
00428
00432 operator time_t() const;
00433
00435 unsigned char second() const { return second_; }
00436
00438 void second(unsigned char s) { second_ = s; }
00439
00441 std::string str() const { return *this; }
00442
00443 private:
00444 unsigned char hour_;
00445 unsigned char minute_;
00446 unsigned char second_;
00447 };
00448
00456 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
00457 const Time& t);
00458
00459
00460 }
00461
00462 #endif // !defined(MYSQLPP_DATETIME_H)