MySQL++  3.3.0
datetime.h
Go to the documentation of this file.
1 
5 /***********************************************************************
6  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
7  (c) 2004-2008 by Educational Technology Resources, Inc. Others may
8  also hold copyrights on code in this file. See the CREDITS.txt file
9  in the top directory of the distribution for details.
10 
11  This file is part of MySQL++.
12 
13  MySQL++ is free software; you can redistribute it and/or modify it
14  under the terms of the GNU Lesser General Public License as published
15  by the Free Software Foundation; either version 2.1 of the License, or
16  (at your option) any later version.
17 
18  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21  License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with MySQL++; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
26  USA
27 ***********************************************************************/
28 
29 #if !defined(MYSQLPP_DATETIME_H)
30 #define MYSQLPP_DATETIME_H
31 
32 #include "common.h"
33 
34 #include "comparable.h"
35 
36 #include <string>
37 #include <iostream>
38 
39 namespace mysqlpp {
40 
47 
48 class MYSQLPP_EXPORT DateTime : public Comparable<DateTime>
49 {
50 public:
54  year_(0),
55  month_(0),
56  day_(0),
57  hour_(0),
58  minute_(0),
59  second_(0),
60  now_(true)
61  {
62  }
63 
72  DateTime(unsigned short y, unsigned char mon, unsigned char d,
73  unsigned char h, unsigned char min, unsigned char s) :
75  year_(y),
76  month_(mon),
77  day_(d),
78  hour_(h),
79  minute_(min),
80  second_(s),
81  now_(false)
82  {
83  }
84 
86  DateTime(const DateTime& other) :
88  year_(other.year_),
89  month_(other.month_),
90  day_(other.day_),
91  hour_(other.hour_),
92  minute_(other.minute_),
93  second_(other.second_),
94  now_(other.now_)
95  {
96  }
97 
103  explicit DateTime(const char* str) { convert(str); }
104 
112  template <class Str>
113  explicit DateTime(const Str& str)
114  {
115  convert(str.c_str());
116  }
117 
119  explicit DateTime(time_t t);
120 
125  int compare(const DateTime& other) const;
126 
128  const char* convert(const char*);
129 
131  unsigned char day() const { return day_; }
132 
134  void day(unsigned char d) { day_ = d; now_ = false; }
135 
137  unsigned char hour() const { return hour_; }
138 
140  void hour(unsigned char h) { hour_ = h; now_ = false; }
141 
144  bool is_now() const { return now_; }
145 
147  unsigned char minute() const { return minute_; }
148 
150  void minute(unsigned char m) { minute_ = m; now_ = false; }
151 
153  unsigned char month() const { return month_; }
154 
156  void month(unsigned char m) { month_ = m; now_ = false; }
157 
162  static DateTime now() { return DateTime(); }
163 
165  operator std::string() const;
166 
168  operator time_t() const;
169 
171  unsigned char second() const { return second_; }
172 
174  void second(unsigned char s) { second_ = s; now_ = false; }
175 
177  std::string str() const { return *this; }
178 
184  unsigned short year() const { return year_; }
185 
190  void year(unsigned short y) { year_ = y; now_ = false; }
191 
192 private:
193  unsigned short year_;
194  unsigned char month_;
195  unsigned char day_;
196  unsigned char hour_;
197  unsigned char minute_;
198  unsigned char second_;
199 
200  bool now_;
201 };
202 
203 
206 inline DateTime NOW() { return DateTime(); }
207 
208 
217 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
218  const DateTime& dt);
219 
220 
225 class MYSQLPP_EXPORT Date : public Comparable<Date>
226 {
227 public:
229  Date() : year_(0), month_(0), day_(0) { }
230 
236  Date(unsigned short y, unsigned char m, unsigned char d) :
237  Comparable<Date>(),
238  year_(y),
239  month_(m),
240  day_(d)
241  {
242  }
243 
245  Date(const Date& other) :
246  Comparable<Date>(),
247  year_(other.year_),
248  month_(other.month_),
249  day_(other.day_)
250  {
251  }
252 
254  Date(const DateTime& other) :
255  Comparable<Date>(),
256  year_(other.year()),
257  month_(other.month()),
258  day_(other.day())
259  {
260  }
261 
266  explicit Date(const char* str) { convert(str); }
267 
274  template <class Str>
275  explicit Date(const Str& str) { convert(str.c_str()); }
276 
281  explicit Date(time_t t);
282 
287  int compare(const Date& other) const;
288 
290  const char* convert(const char*);
291 
293  unsigned char day() const { return day_; }
294 
296  void day(unsigned char d) { day_ = d; }
297 
299  unsigned char month() const { return month_; }
300 
302  void month(unsigned char m) { month_ = m; }
303 
305  operator std::string() const;
306 
310  operator time_t() const;
311 
313  std::string str() const { return *this; }
314 
319  unsigned short year() const { return year_; }
320 
325  void year(unsigned short y) { year_ = y; }
326 
327 private:
328  unsigned short year_;
329  unsigned char month_;
330  unsigned char day_;
331 };
332 
339 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
340  const Date& d);
341 
342 
347 class MYSQLPP_EXPORT Time : public Comparable<Time>
348 {
349 public:
351  Time() : hour_(0), minute_(0), second_(0) { }
352 
357  Time(unsigned char h, unsigned char m, unsigned char s) :
358  hour_(h),
359  minute_(m),
360  second_(s)
361  {
362  }
363 
365  Time(const Time& other) :
366  Comparable<Time>(),
367  hour_(other.hour_),
368  minute_(other.minute_),
369  second_(other.second_)
370  {
371  }
372 
374  Time(const DateTime& other) :
375  Comparable<Time>(),
376  hour_(other.hour()),
377  minute_(other.minute()),
378  second_(other.second())
379  {
380  }
381 
387  explicit Time(const char* str) { convert(str); }
388 
396  template <class Str>
397  explicit Time(const Str& str) { convert(str.c_str()); }
398 
403  explicit Time(time_t t);
404 
409  int compare(const Time& other) const;
410 
412  const char* convert(const char*);
413 
415  unsigned char hour() const { return hour_; }
416 
418  void hour(unsigned char h) { hour_ = h; }
419 
421  unsigned char minute() const { return minute_; }
422 
424  void minute(unsigned char m) { minute_ = m; }
425 
427  operator std::string() const;
428 
432  operator time_t() const;
433 
435  unsigned char second() const { return second_; }
436 
438  void second(unsigned char s) { second_ = s; }
439 
441  std::string str() const { return *this; }
442 
443 private:
444  unsigned char hour_;
445  unsigned char minute_;
446  unsigned char second_;
447 };
448 
456 MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
457  const Time& t);
458 
459 
460 } // end namespace mysqlpp
461 
462 #endif // !defined(MYSQLPP_DATETIME_H)
Mix-in that gives its subclass a full set of comparison operators.
Definition: comparable.h:41
C++ form of SQL's DATETIME type.
Definition: datetime.h:49
void month(unsigned char m)
Change the date/time value's month part, 1-12.
Definition: datetime.h:156
unsigned char day() const
Get the date/time value's day part, 1-31.
Definition: datetime.h:131
void second(unsigned char s)
Change the date/time value's second part, 0-59.
Definition: datetime.h:174
DateTime(unsigned short y, unsigned char mon, unsigned char d, unsigned char h, unsigned char min, unsigned char s)
Initialize object from discrete y/m/d h:m:s values.
Definition: datetime.h:72
void minute(unsigned char m)
Change the date/time value's minute part, 0-59.
Definition: datetime.h:150
unsigned char minute() const
Get the date/time value's minute part, 0-59.
Definition: datetime.h:147
DateTime()
Default constructor.
Definition: datetime.h:52
unsigned char month() const
Get the date/time value's month part, 1-12.
Definition: datetime.h:153
DateTime(const DateTime &other)
Initialize object as a copy of another Date.
Definition: datetime.h:86
void day(unsigned char d)
Change the date/time value's day part, 1-31.
Definition: datetime.h:134
bool is_now() const
Returns true if object will evaluate to SQL "NOW()" on conversion to string.
Definition: datetime.h:144
std::string str() const
Return our value in std::string form.
Definition: datetime.h:177
unsigned char hour() const
Get the date/time value's hour part, 0-23.
Definition: datetime.h:137
DateTime(const char *str)
Initialize object from a C string containing a SQL date-and-time string.
Definition: datetime.h:103
DateTime(const Str &str)
Initialize object from a C++ string containing a SQL date-and-time string.
Definition: datetime.h:113
unsigned short year() const
Get the date/time value's year part.
Definition: datetime.h:184
unsigned char second() const
Get the date/time value's second part, 0-59.
Definition: datetime.h:171
static DateTime now()
Factory to create an object instance that will convert to SQL "NOW()" on insertion into a query.
Definition: datetime.h:162
void year(unsigned short y)
Change the date/time value's year part.
Definition: datetime.h:190
void hour(unsigned char h)
Change the date/time value's hour part, 0-23.
Definition: datetime.h:140
C++ form of SQL's DATE type.
Definition: datetime.h:226
Date(const char *str)
Initialize object from a C string containing a date.
Definition: datetime.h:266
void day(unsigned char d)
Change the date's day part, 1-31.
Definition: datetime.h:296
Date(const Date &other)
Initialize object as a copy of another Date.
Definition: datetime.h:245
unsigned char month() const
Get the date's month part, 1-12.
Definition: datetime.h:299
void year(unsigned short y)
Change the date's year part.
Definition: datetime.h:325
Date(unsigned short y, unsigned char m, unsigned char d)
Initialize object.
Definition: datetime.h:236
unsigned char day() const
Get the date's day part, 1-31.
Definition: datetime.h:293
Date(const Str &str)
Initialize object from a C++ string containing a date.
Definition: datetime.h:275
Date(const DateTime &other)
Initialize object from date part of date/time object.
Definition: datetime.h:254
std::string str() const
Return our value in std::string form.
Definition: datetime.h:313
unsigned short year() const
Get the date's year part.
Definition: datetime.h:319
Date()
Default constructor.
Definition: datetime.h:229
void month(unsigned char m)
Change the date's month part, 1-12.
Definition: datetime.h:302
C++ form of SQL's TIME type.
Definition: datetime.h:348
unsigned char hour() const
Get the time's hour part, 0-255.
Definition: datetime.h:415
unsigned char second() const
Get the time's second part, 0-59.
Definition: datetime.h:435
Time(const Time &other)
Initialize object as a copy of another Time.
Definition: datetime.h:365
Time(const char *str)
Initialize object from a C string containing a SQL time string.
Definition: datetime.h:387
Time(unsigned char h, unsigned char m, unsigned char s)
Initialize object.
Definition: datetime.h:357
std::string str() const
Return our value in std::string form.
Definition: datetime.h:441
Time(const Str &str)
Initialize object from a C++ string containing a SQL time string.
Definition: datetime.h:397
void hour(unsigned char h)
Change the time's hour part, 0-255.
Definition: datetime.h:418
Time()
Default constructor.
Definition: datetime.h:351
unsigned char minute() const
Get the time's minute part, 0-59.
Definition: datetime.h:421
Time(const DateTime &other)
Initialize object from time part of date/time object.
Definition: datetime.h:374
void minute(unsigned char m)
Change the time's minute part, 0-59.
Definition: datetime.h:424
void second(unsigned char s)
Change the time's second part, 0-59.
Definition: datetime.h:438
This file includes top-level definitions for use both internal to the library, and outside it....
Declares the Comparable<T> mixin.
DateTime NOW()
Returns a DateTime object that, when inserted into query will yield a SQL "NOW()" function call.
Definition: datetime.h:206